I am going to create a new algorithm which combines Kalman Filters with pairs trading strategy together. Also, I extend my algorithm with the single pair into the one with multiple pairs. As for those pairs, I chose them through the same method as I explained in the previous pairs trading strategy article(link). The industries I included in my algorithm are: Consumer Electronics, Food Products, Biotechnology, and Oil, Gas & Consumable Fuels respectively. The target firms are: ‘GRPO’, ‘WWAV’, LOXO’, ‘EAC’(which is not qualified for cointegration test with any peers unfortunately). I will show part of the test here in order to clarify this adjustment.
The code I upload in the end was partially copied from the algorithm developed by David Edwards as part of Quantopian’s 2015 summer series. But I made some significant changes based upon the original one.
First, I change the whole Paris basket. I use the stocks listed above instead of the ones in the initial algorithm. I also change the specified time range of the schedule used to check how much time away from market open or close.Then, I adjust the frequency to check stock price into daily basis.
The original algorithm includes 6 pairs of stocks instead only 4 of mine. I change the weight formula accordingly. As for the ‘if statement’ part, I use +/- 1.75 as the criteria compared with Z-score of the spread in order to make the trading decision by replacing the former +/- 1.5. Because I believe parameters drift often causes trades to exited before the original spread has become profitable and the spread should be set larger in this case.
I also tried to change the ‘pnl’ which is the measurement for the profit and loss to check whether profits we make can actually cover the costs(cost of longing and commission) into a positive number(to make sure we will make profit larger than a specific number under these conditions). However, the distribution of returns becomes quite unstable and flat in the end. For the better simulation effect, I keep the pnl’s criteria back into 0(we will still make profit instead loss under this situation). But this strategy should not be tried repeatedly over backtesting stage since if we tend to change the backtesting algorithm in order to fit the out of sample data then we actually turn the out of sample data into in sample data and result in outfitting issue.
We can have a general feeling about the algorithm’s performance through the total returns graph. The returns are stable over time and well-performed in general(total returns > 10% most times and higher than benchmark’s total returns),
The overall backtesting period I use here is from 12–08–2014 to 12–08–2015. We can find part of the transaction and positions details as below.
As I mentioned before, a couple of indicators are very important for measuring the performance of the algorithm besides total returns. They are also my main focuses whenever I test the model. I will make more efforts on finding better algorithm to make sure those indicators are under controlled over time.
First, I will show the result of the volatility of this backtest. The ideal result of the ‘volatility’ should be low. We can see the volatilities are relatively low over this period even compared to the benchmark(S&P500).
In terms of Max Drawdown, they are relatively small compared to the backtesting results I posted before.That is indeed a progress I am making here. But only one sample cannot prove that Kalman Filtering pairs trading model is actually better than the others. Possibly, We can prove it through the tests I will post in the future.
Please feel free to comment on inefficiencies about anything I explained here as usual. I am always open to the mistakes and willing to learn from them as a beginner in this area.
Here is the code I used for backtesting of this case: