Analysis, code, data, python

Extremely Basic Pair Trading Backtest in Python

A pair trade is just like the name implies, trading pairs of stocks. You do it when you notice pairs of stocks that seem related.

Coke and Pepsi is a classic pair trade example because they both had market tailwinds but their stocks didn’t always follow the same short term pattern. But longer term had agreement. Enter a pair trade!

In this tutorial I’ll reproduce the results from Github – LongOnly – PairsTrading and apply them to a few other pairs I’m interested in. (UPWK, FIVRR) and (AMD, INTL), and (AMD, NVDA)

(skip to Jupyter Notebook)

Reproduction of Results: (KO and PEP)

In the LongOnly tutorial, predictably he only goes long. It’s a very basic strategy that looks at daily winners/losers and invests in the loser if he has money from his last position. In practice you would vary the window to look back beyond just daily. It’s my hunch a 2-4 hour mark would be good with Upwork.

Much wow, was actually super straightforward. I used Python 3.9 with latest pandas, matplotlib, and requests installed. Worked like a charm and I’ve never seen code that used cookies from the requests session before. Nice!

There’s a few tweaks in my pairs_only notebook but nothing major.

Other Pairs

Well that’s no good! We would have dodged the May 2021 dip but at quite the cost.
Even worse!
Oh dear

What Gives?

Friends Joey GIF - Friends Joey Shrug GIFs

Rather than just not publish this since the results are so crappy I’m experimenting with sharing my failures. Too often in life we see the best of things, we don’t see all the failed coding channels, the cruddy initial research, and blunders.

Always seeing the cream of the crop makes goals unrealistic. Coding isn’t this magical thing and there are plenty of false starts or projects that need to be scrapped. Granted maybe I’m being lazy here by postingand I do certainly need to make some good tutorials too!

I don’t believe this trading approach is flawed per se, pairs trading/mean reversion is a great idea but the algorithm I selected was far too basic. And as implemented offered little opportunity to tweak parameters. You should really use some stats too. The Johansen test can handle more than 2 securities in a pair and the Augmented-Dickey-Fuller ADF test would be good to evaluate reversion windows. Also, position sizing is huge, i.e. how much to buy. In the implementation we used we bet the bank each time. That has too much sequence of returns risk. Often times you’ll bet a fraction of some ultimate total strategy commitment size. With constant re-evaluation.

You can also use the Kelly criterion if you have high volume trading or the modified Kelly criterion is good too.

If you’re interested in pairs trading more I would strongly recommend you read this tutorial as a good next tutorial. The way the author talks about interpreting results and biases is spot-on too. I’d be cautious about the FX bootcamp though, that can be a slimy industry.

Further Resources

Read my IB_Insync tutorial if you have an algorithm you actually want to use.

If you want me to make a video for this let me know by leaving me a comment below or reach out on Twitter. I can walk through how I got new tickers and more about the process if that’s interesting. Or do a better job documenting install steps with Python

Algorithmic Trading by – E.P. Chan is pretty decent and goes over more strategies. I would also recommend just googling things, Twiecki and Quantopian are good too.See tutorial 3 in that link I would have used the zipline library but the creators, Quantopian, are no more and I didn’t see any compelling forks.

As an aside, historical trade monopolies and what foreigners thought of exotic spices is interesting

Leave a Reply

Your email address will not be published. Required fields are marked *