21
How To Build Your Own Pairs Trading Algorithm Trading System Quantopian Meetup Gary Chan

Pairs Trading from NYC Algorithmic Trading Meetup November '13

Embed Size (px)

DESCRIPTION

Gary Chan presented at the NYC Algorithmic Trading Meetup. More on the presentation, including a sample Excel file, on our blog http://blog.quantopian.com/gary-chan-on-pairs-trading-presentation-from-nyc-algorithmic-trading-meetup/ You can sign up for future meetups here: www.meetup.com/NYC-Algorithmic-Trading/

Citation preview

Page 1: Pairs Trading from NYC Algorithmic Trading Meetup November '13

How To Build Your Own Pairs Trading Algorithm Trading System

Quantopian MeetupGary Chan

Page 2: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 3: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 4: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 5: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 6: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 7: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 8: Pairs Trading from NYC Algorithmic Trading Meetup November '13

My Cloud

Page 9: Pairs Trading from NYC Algorithmic Trading Meetup November '13

My Cloud Part 2

Page 10: Pairs Trading from NYC Algorithmic Trading Meetup November '13

Backtesting• Profitable backtests does not mean good• You need a valid model• Live trading WILL underperform backtests

Page 11: Pairs Trading from NYC Algorithmic Trading Meetup November '13

How A Good Backtest Looks• Consistent, high R Squared• Similar parameters have similar equity curves

Page 12: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 13: Pairs Trading from NYC Algorithmic Trading Meetup November '13

Pairs Trading Books• Contains Code• Complete manual to putting together your own system

Page 14: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 15: Pairs Trading from NYC Algorithmic Trading Meetup November '13

Necessary Math

• Basic statistics• Linear algebra• Standard deviations, regressions• Types of distributions (normal, logarithmic)• Random walks• Stationary (mean reverting) series• Cointegration

Page 16: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 17: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 18: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 19: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 20: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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

Page 21: Pairs Trading from NYC Algorithmic Trading Meetup November '13

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