Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Multiple Pairs Trading Algo

Hey,

I am looking for some feedback on the pairs trading code I wrote. It is based off some previous work done by others on here. Depending on the pairs chosen, the code will spit out some funny results, which I believe to be due to how beta and the spread is calculated.

Would greatly appreciate any feedback on how this could be improved and made more robust.

Cheers,

Nick

4 responses

This looks pretty good. All I did here was move context.algos into a pandas series, that way you can use .apply to speed up the algo some. I'm not positive on how much that actually speeds up the algo, but supposedly it should be faster.

Hello David,

Thank you for helping me with the code I had with spread ratio pair trading! Nicolas and I are actually working together to learn and try out different strategies on Quantopian. I combined what we had with the panda series you included, and here is one of the back-tests. I ran several back-tests and I noticed a few problems:

  1. The SPY return is not the same for two different back-tests for the same time period.
  2. In previous back-test, the program would trade on excessive leverage...such as buying 900k, selling 900k for an initial capital of 10k. I tried to fix the program, it doesn't trade that away anymore, but is there a way to be sure that this will not happen again, like limiting the leverage?
  3. I ran several back-tests at the same time, and got an additional tab "ERROR DETAILS" and the program did not run until the end of the time period.

I would really appreciate if you can give me some feedback on our progress!

Thank you in advance!

Terence

Just a quick update, I am still having issues managing excessive leverage. I ran the attached backtest with $5000 starting capital. Within the first few weeks of the test, the algo goes short 50k worth of one of the tickers. I can not seem to figure out what is causing this issue, look forward to hearing from others who may have encountered this issue.

Thanks!

Nick

def handle_data(self, context, data):  
        #cash = context.notional*self.pct  
        cash  = context.portfolio.cash  # this might help to avoid borrowing  
        record(cash=cash)