Pairs Trading In Python - Interactive Brokers LLC

Transcription

Pairs Trading in PythonIntroduction and application of classical pairs tradingJared BroadCEO and Founderwww.quantconnect.com

Outline Introduction to QuantConnect General Idea of Pairs Trading Implementing the Model Testing and Researching Weaknesses of Pairs Trading SummaryFebruary-2018QuantConnect – Pairs Trading with PythonPage 2

What is QuantConnect?We empower investors with powerfulinvestment tools and connect the brightestminds from around the world with capital they need.February-2018QuantConnect – Pairs Trading with PythonPage 3

What is QuantConnect?QuantConnect is a community of 50,000 Engineers, Data Scientists, ProgrammersFrom 6,100 Cities and 173 CountriesFebruary-2018QuantConnect – Pairs Trading with PythonPage 4

Building Thousands of Algorithms Every DayFebruary-2018QuantConnect – Pairs Trading with PythonPage 5

How do we do it?FINANCIALDATALEAN ERCOMPUTINGWe’ve built a web algorithm lab where thousands ofpeople test their ideas on financial data we provide; for free.February-2018QuantConnect – Pairs Trading with PythonPage 6

Pairs Trading – Market Neutral Trading StrategyPairs trading is a type of statistical arbitrageBasic Idea:February-20181)Select two stocks which move similarly.2)Find where the price diverges.3)Sell the high priced stock and buy the low priced stock.QuantConnect – Pairs Trading with PythonPage 7

The Price RatioTo standardize the prices – we make aPrice Ratio. This allows us to compareStock A and Stock B over time.Price Ratio Stock A / Stock BIf the Ratio changes significantly,it’s a signal to trade.We can measure this change withstandard deviation.February-2018QuantConnect – Pairs Trading with PythonPage 8

Example Price Divergence1412Price Series of Stock A and BStock AStock BPrice(B) 2 * Price(A) 11086420Jan-08Feb-08Mar-08Apr-08May-08Jun-08Price DivergenceJul-08Aug-08Stock AStock 8Mar-08Apr-08May-08QuantConnect – Pairs Trading with PythonJun-08Page 9

Basic Idea of Pairs TradingWhen assets cross divergence threshold; trigger trade.Sell CVXSell XOMBuy XOMBuy CVXSell CVXRatio Upper ThresholdStandard DeviationsBuy XOMSell XOMRatio Lower ThresholdBuy CVXFebruary-2018QuantConnect – Pairs Trading with PythonPage 10

Can we apply this idea to trading strategy?To apply this concept; We model price with the log of stock price. This follows Brownian Motion N(0, 𝑡). This way the difference of the asset prices are cointegrated.i.e. log(pricex) – log(pricey) is “One-Order Cointegrated”.Stationary & Mean ReversionStep 1: Generate the spread of two log price series𝑆𝑝𝑟𝑒𝑎𝑑𝑡 log(𝑌𝑡 ) (𝛼 𝛽log(𝑋𝑡 ))Step 2: Set the range of spread series [lower, upper]February-2018If 𝑆𝑝𝑟𝑒𝑎𝑑𝑡 𝑢𝑝𝑝𝑒𝑟 𝑡ℎ𝑟𝑒𝑠ℎ𝑜𝑙𝑑Buy 𝑋𝑡 , Sell 𝑌𝑡If 𝑆𝑝𝑟𝑒𝑎𝑑𝑡 𝑙𝑜𝑤𝑒𝑟 𝑡ℎ𝑟𝑒𝑠ℎ𝑜𝑙𝑑Buy 𝑌𝑡 , Sell 𝑋𝑡QuantConnect – Pairs Trading with PythonPage 10

Exploratory Research Find two likely cointegrated stocks: e.g. XOM, CVX Estimate spreads between each stock. Check for stationarity.QuantBook ResearchFebruary-2018QuantConnect – Pairs Trading with PythonPage 12

Pairs Trading Weaknesses Double the fees. Capitalize on small price movements. Risk correlation will break down (e.g. CEOchange, new technology) Execution risk (slippage).February-2018QuantConnect – Pairs Trading with PythonPage 13

LEAN Implementation Create our Trading Signal. Run Backtest in QuantConnect.BacktestFebruary-2018QuantConnect – Pairs Trading with PythonPage 14

SummaryTotal TradesDrawdownNet ProfitSharpe Ratio27012.9%34%0.555 We use cointegration to detect a long term relationship of two stocks. Changes in that relationship might signal a chance to profit by pairs trading.Next Steps – Defining Trading Rules, Setting ThresholdsFebruary-2018QuantConnect – Pairs Trading with PythonPage 15

www.quantconnect.comThank you.

Appendix

Cointegration and StationaryStationaryCointegration Cointegration is a statistical property of a timeseries(like the stock price series). Cointegration specifies a co-movement relationship ofThe mean and the variance of theseries do not vary over timeprice – the long term relationship.{𝑿𝒕 } and {𝒀𝒕 } are cointegrated If two series {𝑋𝑡 } and {𝑌𝑡 } are not stationary But their linear combination 𝑌𝑡 𝛽𝑋𝑡 𝛼 is a stationary processHow to test if two series are cointegrated?Augmented Dickey-Fuller testFebruary-2018QuantConnect – Pairs Trading with PythonPage 18

Our Research EnvironmentFebruary-2018QuantConnect – Pairs Trading With PythonPage 19

Coding the Idea, The Algorithm LabFebruary-2018QuantConnect – Pairs Trading With PythonPage 20

Going Live, Deploying to Live TradingFebruary-2018QuantConnect – Pairs Trading with PythonPage 21

Introduction to QuantConnect General Idea of Pairs Trading Implementing the Model Testing and Researching Weaknesses of Pairs Trading Summary February-2018 QuantConnect -Pairs Trading with Python Page 2. What is QuantConnect? We empower investors with powerful