This insightful webinar on pairs trading and sourcing data covers the basics of pair trading strategy followed by two examples. In the first example, Marco covers the pairs trading strategy for different stocks traded on the same exchange, and in the second example, Marco has illustrated the pairs strategy for different commodity futures traded on different exchanges.
Marco also details the different data sources including Quandl which can be used for creating trading strategies. Marco has spent his career as a trader and portfolio manager, with a particular focus in equity and derivatives markets.
He specializes in quantitative finance and algorithmic trading and currently serves as head of the Quantitative Trading Desk and Vice-president of Argentina Valores S.
One of my favorite classes during EPAT was the one on statistical arbitrage , so the pair trading strategy seemed a nice idea for me. My strategy triggers new orders when the pair ratio of the prices of the stocks diverge from the mean. But in order to work, we first have to test for the pair to be cointegrated. If the pair ratio is cointegrated, the ratio is mean-reverting and the greater the dispersion from its mean, the higher the probability of a reversal, which makes the trade more attractive.
I chose the following pair of stocks:. The idea is the following: If we find two stocks that are correlated they correspond to the same sector , and the pair ratio diverges from a certain threshold, we short the stock that is expensive and buy the one that is cheap. Once they converge to the mean, we close the positions and profit from the reversal. The logic is simple. The algorithm calculates the daily Z-score for every pair of stocks.
The Z-score is the number of standard deviations that the pair ratio has diverged from its mean:. Once the Z-score is outside of a certain threshold, we fulfill the first condition required for sending an order. But the algorithm must also meet a second condition: It calculates the rolling Augmented Dickey Fuller test for the pair of stocks.
More specifically, it gets the p-value from the test. Then it compares it with a defined significance level alpha and if the p-value is less than the alpha, it means that the price ratio series are stationary and the second condition is met. If both conditions are met, then the algorithm buys the loser and sells the winner. The exit rules apply at a certain Z-score threshold.
For the optimization of the strategy the variables that I used were the following:. The in-sample period for backtesting was till The Z-score was calculated using the following parameters:.
I used quantstrat library  for backtesting the strategy. Let us dive into the code:. As mentioned earlier, I will use quantsrat library for the optimization of my strategy. In order to use quantstrat we first have to define and initialize instruments, strategy, portfolio, account and orders:.
In the following chart we can see the evolution of the Z-score during the period and the possible values for the threshold where the ratio reverts to the mean and the extreme values.
As we can see from our summary there are 2 indicators, 7 signals and 3 rules defined in our strategy. Now we can run the backtest, check the transactions and the performance of our strategy.
From this table we can get the values for the variables that optimize the strategy. At first sight it seems that there are 3 candidates case 4, case 6 and case 8. If we compare between cases 6 and 8 we arrive to the conclusion that case 8 is the best one as it has a greater annualized Sharpe ratio and profit to max drawdown, a higher percentage of positive trades, a greater end equity and with the same number of trades. So now we are left with only 2 candidates: If we would only be checking for the one with the greatest annualized Sharpe ratio, we would prefer case 4.
But if we take into account the number of transactions, the profit to max drawdown, the end equity, the percentage of positive trades and the fact that the difference in the Sharpe ratio is not a big difference we would definitely select case 8 as our best candidate.
Now that we have optimized the strategy and obtained the optimal values for the parameters, we can run an out of sample blacktest and see how the strategy performs. The out of sample period for the back test goes from the to the and the optimized values for the thresholds and rules were the following:.
The following chart show us the different transactions, the end equity and the drawdown results for our strategy:. From the table below we can see that the results from the out of sample backtest are not as good as the ones we got from the in sample backtest.
The annualized Sharpe ratio is still positive but smaller than the 3. The profit to max drawdown is quite worse than the 4. Our strategy delivers a cumulative return of The idea when I started the Executive Program in Algorithmic trading was to learn how to model a quantitative trading strategy, backtest it and then optimize it.
Thanks to my professors and QuantInsti staff I feel that the objective was accomplished. Everything in the course was excellent and would recommend it to everyone interested in learning algorithmic trading. For understanding the statistics behind Pair Trading, Correlation and Cointegration, have a look at our post here.
Learn the application of mean reversion and optimising trading parameters using this Excel Downloadable model. If you are a coder or a tech professional looking to start your own automated trading desk. Learn automated trading from live Interactive lectures by daily-practitioners. Your email address will not be published. This iframe contains the logic required to handle Ajax powered Gravity Forms.
Pair Trading Strategy and Backtesting using Quantstrat. Barter System To Algo Trading. Leave a Reply Cancel reply Your email address will not be published. Want to Learn Algo Trading? Click here to register.More...