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
IPairs Trading Simulation sounds exciting, especially if you're into the stock market. It's a unique way to get hands-on experience with trading strategies without the risk. Pair trading, where you simultaneously buy and sell two related stocks, sharpens your skills. You get to see how different strategies play out in real time, which is valuable if you're serious about learning the ins and outs of the market.
You should check out a stock picking service to improve your stock picking. It could complement what you're learning through the simulation, giving you more insight into which stocks to focus on. Combining a stock picking service with the iPairs simulation might be a powerful way to hone your skills.
Reply
#3
I searched (duckduckgo) for "simulation of pairs trading with cointegrated assets, examples, python" and got a bunch of examples.

I didn't read it (no time, but will read later), but one of the examples, "Cointegration and Pairs Trading", looks promising.

statmodels package available from PyPi
code for 'statsmodels.tsa.stattools' can be found here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Improve Prime Pairs Code jzakiya 7 1,588 Mar-09-2025, 08:19 PM
Last Post: jzakiya
  Sample random, unique string pairs from a list without repetitions walterwhite 1 1,919 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Pairs of multiplied prime number--->N Frankduc 13 6,057 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,802 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  cbot ctrader automate trading hl29951 1 2,255 Jul-25-2021, 08:14 PM
Last Post: Larz60+
  Code should download and output trading data tayy 2 3,566 Mar-04-2021, 04:07 AM
Last Post: tayy
  Extracting unique pairs from a data set based on another value rybina 2 3,061 Feb-12-2021, 08:36 AM
Last Post: rybina
  problem about slope in python script for bitcoin trading fisher_garry 1 3,321 Sep-02-2020, 01:39 PM
Last Post: fisher_garry
  Key value pairs assistance UtiliseIT 2 3,609 May-09-2019, 09:26 AM
Last Post: UtiliseIT
  Help with simulation Geeseball 0 2,623 Oct-18-2018, 12:19 PM
Last Post: Geeseball

Forum Jump:

User Panel Messages

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