How To Build Your Own Pairs Trading Algorithm Trading System
Quantopian MeetupGary Chan
Introduction
• Started programming at a young age• Played poker as main source of income for a few
years• Money is just a way to keep score• Became interested in stock market• Read ~70 textbooks, mostly corporate finance, CFA
curriculum• Currently running an algorithmic trading system
out of my apartment
Expectations
• I’m not here to divulge secrets• Teach a man to fish, not give him a fish• You will see how easy the pairs trading model is to
understand and build• Deployable by retail investors• Seeing is believing• You will not be able to build your own tomorrow…
but one day… if you want it badly enough• It’s a small world, so share ideas
Why you can beat the professionals
• Definition of professionals• Algorithmic does not mean high
frequency• HFT is not for retail investors• Strategy capacity• Find a niche
Disclaimers
• A lot of learning, before this came to fruition• Content here will not be complete due to time
constraints• Survivorship bias, Look ahead bias• Model risk, implementation risk, execution
risk, over parameterization• Random models can have profitable backtests• I’m not responsible if you lose money
Algorithmic Trading is Easy
• What does easy / hard mean?• Info, books, tools, cheaply or freely available• Yahoo, Google, Morningstar, Edgar, Quantopian• Visual Studio Express, MySQL, R• Many textbooks written on the subject• Low capital requirements (30K minimum) compared
to other ventures• Backtest before you use real money!!• Higher quality data will cost more money
My Current System
• Low frequency• Single computer, 3 years old• Runs on Wifi from my apartment• Started coding Oct 2012• Started forward testing play money in March 2013• Started forward testing real money June 2013• Current performance consistent with backtests• Just started using cloud computing
My Cloud
My Cloud Part 2
Backtesting• Profitable backtests does not mean good• You need a valid model• Live trading WILL underperform backtests
How A Good Backtest Looks• Consistent, high R Squared• Similar parameters have similar equity curves
Key Info From Backtests• Portfolio of 39 pairs• Average of 12 to 18 pairs with opened positions, $5400 max drawdown• $10k sized legs in backtests, $395 average profit per trade• 81.3% winning trades, 4540 trades, 26 day average holding period
Pairs Trading Books• Contains Code• Complete manual to putting together your own system
Types of Investment Strategies
• Mean Reversion– Buy low, sell high / Sell high, buy low– Fundamental Analysis– Statistical Arbitrage
• Momentum– Sell low, buy lower / Buy high, sell higher– News
• Technical analysis does not work
Necessary Math
• Basic statistics• Linear algebra• Standard deviations, regressions• Types of distributions (normal, logarithmic)• Random walks• Stationary (mean reverting) series• Cointegration
Mean Reversion Example #1
• Keep an opened mind• Everything can be measured in money• If you don’t believe me, put it on EBay• Fear can be measured in dollars• Unwarranted fear -> Sell volatility to profit
Mean Reversion Example #2
• Stocks move in random walks• Some stocks move together• Spread between the stocks are mean reverting• Buy low, sell high on the spread• Statistical arbitrage means you win most of the time,
not all the time• Buyouts and bankruptcies result in large losses• Diversification is a must• Learn some corporate finance / fundamentals
Case Study, GLD and IAU
• Two gold ETF’s• Pull end of day prices from Yahoo Finance into
csv files• Do a linear regression• Calculate the spread• Graph of spread is mean reverting• Find #Stdevs to enter / exit trade• Optimize parameters
Next Steps
• Try higher quality data • You could trade this by hand + excel, but
better to automate the process• Many systems are built in excel• Optimize the code• Backtest until you find a profitable strategy• Use a brokerage with an API, sample code• Forward test with play money
Next Steps Part 2
• Execution, order, position management• Test with small amounts of real money• Tweak your system• Ramp up trading size if still profitable• Exhaust universe for the strategy• Find new strategies• Never stop learning
The End
• Ernest P Chan - Quantitative Trading: How to Build Your Own Algorithmic Trading Business (Wiley Trading)
• Ganapathy Vidyamurthy - Pairs Trading: Quantitative Methods and Analysis (Wiley Finance)
• R - http://www.r-project.org/ (like Matlab)Yahoo, Google, Morningstar, Edgar, Quantopian
• Visual Studio Express, MySQL, R• [email protected]• https://twitter.com/RITrading - I tweet my trades here
Top Related