Menu

Pair trading strategy r

2 Comments

pair trading strategy r

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 trading. 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 strategy algorithmic trading and currently serves as head of the Quantitative Trading Desk and Vice-president of Argentina Valores S. Marco is also Co-Founder and CEO of Quanticko Trading SA, a firm devoted to the development of high frequency trading strategies and trading software. One of my favorite classes during EPAT was the one on statistical arbitrageso 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 pair from its mean, the higher the probability of a reversal, which makes the trade more attractive. I chose the following pair of stocks:. Pair idea is the following: If we find two stocks that are correlated they correspond to the same sectorand the pair ratio diverges trading a certain threshold, we short the stock that is expensive and buy the one that is cheap. Strategy 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 trading 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 trading, 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. Pair exit rules apply at a certain Z-score threshold. For the optimization of the strategy the variables that I used were the following:. Pair in-sample period for backtesting was till The Z-score was calculated using the following parameters:. I used quantstrat library [1] for backtesting the strategy. Let us dive into the code:. As strategy earlier, I will use quantsrat library for the optimization of trading 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 strategy 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 strategy see from our summary there are 2 trading, 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 pair 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 strategy 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 trading case 4. But if we take into account the number of transactions, the profit to max drawdown, pair end trading, 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 pair 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 strategy 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 strategy 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 Strategy 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 pair 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. Yemen Zambia Zimbabwe ProspectID Name This field is for validation purposes and should strategy left unchanged. This iframe contains the logic required to handle AJAX powered Gravity Forms. Pair Trading Strategy and Backtesting using Quantstrat. Pair Trading Strategy and Backtesting using Quantstrat On July 27, By admin In Project Work EPATR ProgrammingTrading Strategies 1 Comment. Author Marco trading spent his career as a trader and portfolio manager, with a particular focus in equity and derivatives markets. Introduction One of my favorite classes during EPAT was the one on statistical arbitrageso the pair trading strategy seemed a nice idea for me. I chose the following pair of stocks: Bank of America BAC Citigroup C The idea is the following: Trading Strategy Logic The logic is simple. The Z-score is the number of standard deviations that the pair ratio has diverged from its mean: For the optimization of the strategy the variables that I used were the following: Z-Score entry thresholds Z-Score exit thresholds Second condition cointegration True or False Code details and In-Sample Strategy The Z-score was calculated using the following parameters: Moving average of pair price ratio: Let us dive into the code: Load libraries library quantstrat library tseries library IKTrading library PerformanceAnalytics. In order to use quantstrat we first have to define and initialize instruments, strategy, portfolio, account and orders: Inititalize strategy, portfolio, account and orders qs. ADF, ft2 colnames Augmented. Ratio 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. If we want to set the ADF test second condition off, we just change it to "1", in that case the p-value will always be lower than the significance level and the and the strategy will not require the pair to be cointegrated. Z-Score entry and exit thresholds: From the in-sample backtest we got the following results: Out of Sample Backtest: 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: Next Steps For understanding the statistics behind Pair Trading, Correlation and Cointegration, have a look at our post here. Development of Cloud-Based Automated Trading System with… Algorithmic Trading Strategies, Paradigms and Modelling… EPAT Final Project by Jacques Joubert — Statistical… Essential Books on Algorithmic Trading. Leave a Reply Cancel reply Your email address will not be trading. Categories Career Advice pair Downloadables 15 Getting Started 74 News 44 Events 28 Press Releases 3 Programming and Trading Tools 73 Other Languages 10 Python 24 R Programming 35 Trading Platforms 5 Project Work EPAT 10 Trading Trading 55 Webinars 26 Previous Webinars 25 Upcoming Webinars 1. Helpful Sources Quantocracy Quantsportal Quantpedia KDnuggets R-bloggers The Financial Hacker Wall Street Oasis Robot Wealth Turing Finance. India QuantInsti Quantitative Learning Pvt Ltd A, Boomerang, Chandivali Farm Road, Powai, Mumbai — Toll Free: Connect with us… Show us some pair on Quantocracy. Click here to register. pair trading strategy r

2 thoughts on “Pair trading strategy r”

  1. Ńĺäîé says:

    Because speed is prone to blunder and inaccuracies,the more speed one adopt,the more is it likely that he would commit mistakes and, thus would compromise with the final outcome.Decisions taken and situation responded to,in hastiness lead to inaccuracy.So, one must be circumspect about never to take decision,make judgement and assessment out of unneeded briskness.

  2. amnber72 says:

    You will be able to share your knowledge and advice with a large and vibrant community looking to lead a healthier lifestyle while earning a reliable paycheck.

Leave a Reply

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

inserted by FC2 system