Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pairs Trading Simulation
#1
Star 
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 611 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Pairs of multiplied prime number--->N Frankduc 13 3,902 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,310 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  cbot ctrader automate trading hl29951 1 1,740 Jul-25-2021, 08:14 PM
Last Post: Larz60+
  Code should download and output trading data tayy 2 2,815 Mar-04-2021, 04:07 AM
Last Post: tayy
  Extracting unique pairs from a data set based on another value rybina 2 2,439 Feb-12-2021, 08:36 AM
Last Post: rybina
  problem about slope in python script for bitcoin trading fisher_garry 1 2,615 Sep-02-2020, 01:39 PM
Last Post: fisher_garry
  Key value pairs assistance UtiliseIT 2 2,773 May-09-2019, 09:26 AM
Last Post: UtiliseIT
  Help with simulation Geeseball 0 2,148 Oct-18-2018, 12:19 PM
Last Post: Geeseball
  Create random pairs Dennisp44 3 8,264 Jun-02-2018, 05:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020