Pairs Trading Script Example
Sameer_Kackar
Posts: 1
Hello All,
I am wondering if someone has an example of how you would code a basic pairs trading strategy.
Specifically, I am wondering how can we have two symbols and create a long/short pairs between them.
Any help would be appreciated.
Thanks
Tagged:
Comments
these scripts can work for pairs and baskets. It's broken into two scripts (the entry and exit). Be sure to add the exit as a dependency to the entry.
Hi everyone,
I am new to both cloudquant and Python, so please bear with me.
I am trying to implement a strategy that involves trading a basket of stocks. The pair trading script could be a good initial template for me to start experimenting with the various features on cloudquant. So I gave it a try.
I tried to backtest on a single day using the entry and exit scripts. However, I bumped into a couple of issues and hope that you can give me some guidance. The issues are:
(1) The entry script seems to have finished running, trying to call the exit script. But that triggered an error. The message on the console is:
I have set up the exit script as a dependency to the entry script. It is not clear to me what caused the error.
(2) I noticed that the values inside the symbols dictionary are identical across all symbols. i.e. even though the entry script tries to assign half the symbols as long, and half the symbols as short, the 'side' of all symbols were set to short. Similarly for the 'base shares', which should be different for each symbol, but it ended up becoming identical across all symbols.
I added some print statements to check how the content of the symbols dictionary changed when new values were assigned into individual fields. It appears that whenever a value was assign to a field for one symbol, the value got populated to the same field across all symbols. Not sure what caused this behavior.
I wonder if there may be an updated version of this pair trading script that I can take a look. Thanks for your help.
Hey James,
I also had some issues with the scripts provided, but I managed to work through the bugs and have a working script. The PairEntry and PairExit scripts are below. To use them, I created third script called PairScript, which handles all the logic in terms of when to enter. When I have a pair, I just run this code:
For the example posted below, I hardcoded a pair into PairEntry so you can run it, and have the code commented that I use when running PairScript.
I found it much more useful to use the account object to track positions instead of self.variables
Hope this helps!
Hi David,
Thank you so much. You made my day.
James
David,
Thanks for the scripts you posted above! I cannot add the exit script as a dependency. Is there something special I have to do (like a path name or directory)? Alternatively, is there some way to combine your scripts into one?
My apologies for the naive questions. I'm new to CloudQuant and new to Python. Any help would be much appreciated! All the best.
Dave
Would anyone be willing to help me one on one? I am learning how to program. willing to pay if it makes sense