Hi guys,
I recently posted some thoughts here about a popular futures pair trading algorithm. I took a popular pairs trading algorithm from quantopian and backtested it with different parameters. I remarked that it was suspicious that the algorithm results depended so strongly on small changes in the algorithm parameters.
I found the reason for it. That algorithm has a bug on it. In the calculation of zscore,
zscore = (np.mean(spreads[-context.short_ma]) - np.mean(spreads)) / np.std(spreads, ddof=1)
it should be np.mean(spreads[-context.short_ma:])
. Instead of computing the zscore of short_ma return to long_ma returns, that algorithm is computing the zscore of a single day return, a day "short_ma days ago". Predicting anything with this is luck, that is why the algorithm is so sensitive to parameters. You can see the bug here and here.
The algorithm's behavior is a lot less attractive now because there aren't many entry points.
Note, there might also be a np.sqrt(short_ma)
missing, but that's less important as people are going to be playing around with the entry threshold anyway.