Pairs Trading In Python - Interactive Brokers

2y ago
127 Views
8 Downloads
2.29 MB
21 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Jacoby Zeller
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

Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select two stocks which move similarly. 2) Find where the price diverges. 3) Sell the high priced stock and buy the low priced stock. Feb

Related Documents:

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

In practice, pairs trading contains three main steps5: Pairs selection: identify stock pairs that could potentially be cointegrated. Cointegration test: test whether the identified stock pairs are indeed cointegrated or not. Trading strategy design: study the spread dynamics and design proper trading rules. 5G. Vidyamurthy, Pairs Trading .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

A Python Book A Python Book: Beginning Python, Advanced Python, and Python Exercises Author: Dave Kuhlman Contact: dkuhlman@davekuhlman.org

wesley long hospital . womack army medical center . 3 overview of goals the student should spend the majority of their time in the inpatient pharmacy learning the roles and responsibilities of every staff member. this is a medication use process and systems based experience, not a clinical experience. students should be encouraged to work with pharmacy technicians, including those .