Python Forum

Full Version: Pairs Trading Simulation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am a student in finance and I have a project to do about pairs trading strategy. The fact is that my program works but gives me outputs that are not quite trustable (too high like +100% of original balance). I need to code a simulation of pairs trading with cointegrated assets and I don't know how to code a simulation which comes close to real conditions of trading. I use 10 years daily data of FOREX.
I use the residual's z score of linear regression in order to know when to start a trade. It's the first time I ask for help in this forum so I don't know so much how to share my python program so you will see it below :
If you have any question please ask me, thank you for your help in advance. Smile

def simulation_trade2(ISOs):
    ISO1, ISO2, ISO3, ISO4 = get_data.convert_ISO(ISOs)
    z_score = modelisation(ISOs)
    z_score_std = z_score.std()
    dates, data1, dates_2, data2 = get_data.match_size(ISO1, ISO2, ISO3, ISO4)

    z_score_range = 1.2
    balance = 10000
    q = 0.01
    lock_up = 0
    lock_down = 0
    fees = 0.01

    short_pair1 = []
    long_pair1 = []
    short_pair2 = []
    long_pair2 = []
    balance_list = []

    #Vente de l'actif 1 et achat de l'actif 2 si dépassement seuil haut
    for i in range(len(data1)):

        if z_score[i] >= z_score_std and z_score[i] <= z_score_std * z_score_range and balance > (2 * q * balance) and lock_up == 0:
            short_pair1.append(((q * balance) * data1[i] * (1 - fees)))
            long_pair2.append(((q * balance) * data2[i]))
            balance = balance - (q * balance * (1 + fees))
            lock_up = 1
        if z_score[i] <= -z_score_std and z_score[i] >= -z_score_std * z_score_range and balance > (2 * q * balance) and lock_down == 0:
            long_pair1.append(((q * balance) * data1[i]))
            short_pair2.append(((q * balance) * data2[i] * (1 - fees)))
            balance = balance - (q * balance * (1 + fees))
            lock_down = 1
        if z_score[i] > 0 and z_score[i] < z_score_std and not lock_up == 0:
            for j in short_pair1:
                balance = balance + (((j - data1[i]) / data1[i]))
            for j in long_pair2:
                balance = balance + (j / data2[i])
            short_pair1.clear()
            long_pair2.clear()
            lock_up = 0
        if z_score[i] < 0 and z_score[i] > -z_score_std and not lock_down == 0:
            for j in short_pair2:
                balance = balance + (((j - data2[i]) / data2[i]))
            for j in long_pair1:
                balance = balance + (j / data1[i])
            short_pair2.clear()
            long_pair1.clear()
            lock_down = 0


        balance_list.append(balance)
        print(balance)
I think it's great that you're working on a pairs trading strategy project in finance. Using residual z-scores for trade signals sounds like a solid approach. When it comes to simulating trading conditions realistically, it can be tricky with historical data alone. Have you considered using Monte Carlo simulations to account for various market conditions and uncertainties? It might help refine your model's outputs closer to real trading scenarios. Also, if you're exploring advanced tools, you could look into quantum AI trading tools on quantumai.com for deeper insights into market behavior.