Upload
others
View
11
Download
2
Embed Size (px)
Citation preview
Pairs Trading in PythonIntroduction and application of classical pairs trading
www.quantconnect.com
Jared Broad
CEO and Founder
Outline
▪ 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
investment tools and connect the brightest
minds from around the world with capital they need.
February-2018 QuantConnect – Pairs Trading with Python Page 3
What is QuantConnect?
QuantConnect is a community of 50,000 Engineers, Data Scientists, Programmers
From 6,100 Cities and 173 Countries
February-2018 QuantConnect – Pairs Trading with Python Page 4
Building Thousands of Algorithms Every Day
February-2018 QuantConnect – Pairs Trading with Python Page 5
We’ve built a web algorithm lab where thousands of
people test their ideas on financial data we provide; for free.
LEAN ALGO
TECHNOLOGY
FINANCIAL
DATA
POWER
COMPUTING
How do we do it?
EQUITIES
OPTIONS
FUTURES
FOREX
CRYPTO
February-2018 QuantConnect – Pairs Trading with Python Page 6
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.
February-2018 QuantConnect – Pairs Trading with Python Page 7
February-2018 QuantConnect – Pairs Trading with Python Page 8
The Price Ratio
To standardize the prices – we make a
Price Ratio. This allows us to compare
Stock A and Stock B over time.
Price Ratio = Stock A / Stock B
If the Ratio changes significantly,
it’s a signal to trade.
We can measure this change with
standard deviation.
0
2
4
6
8
10
12
14
Jan-08 Feb-08 Mar-08 Apr-08 May-08 Jun-08 Jul-08 Aug-08
Price Series of Stock A and B Stock A Stock B
Price(B) = 2 * Price(A) + 1
0
2
4
6
8
10
12
14
16
Jan-08 Feb-08 Mar-08 Apr-08 May-08 Jun-08 Jul-08 Aug-08
Price Divergence Stock A Stock B
Example Price Divergence
February-2018 QuantConnect – Pairs Trading with Python Page 9
Basic Idea of Pairs Trading
Buy CVX
Sell XOM
Buy XOM
Sell CVX
Buy CVX
Sell XOM
Buy XOM
Sell CVXRatio Upper Threshold
Ratio Lower Threshold
When assets cross divergence threshold; trigger trade.
February-2018 QuantConnect – Pairs Trading with Python Page 10
Sta
nd
ard
Devia
tio
ns
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 Reversion
February-2018 QuantConnect – Pairs Trading with Python Page 10
Step 1: Generate the spread of two log price series
𝑆𝑝𝑟𝑒𝑎𝑑𝑡 = log(𝑌𝑡) − (𝛼 + 𝛽log(𝑋𝑡))
Step 2: Set the range of spread series [lower, upper]
If 𝑆𝑝𝑟𝑒𝑎𝑑𝑡 > 𝑢𝑝𝑝𝑒𝑟 𝑡ℎ𝑟𝑒𝑠ℎ𝑜𝑙𝑑 Buy 𝑋𝑡, Sell 𝑌𝑡
If 𝑆𝑝𝑟𝑒𝑎𝑑𝑡 < 𝑙𝑜𝑤𝑒𝑟 𝑡ℎ𝑟𝑒𝑠ℎ𝑜𝑙𝑑 Buy 𝑌𝑡, Sell 𝑋𝑡
Exploratory Research
❖ Find two likely cointegrated stocks: e.g. XOM, CVX
❖ Estimate spreads between each stock.
❖ Check for stationarity.
QuantBook Research
February-2018 QuantConnect – Pairs Trading with Python Page 12
Pairs Trading Weaknesses
❖ Double the fees.
❖ Capitalize on small price movements.
❖ Risk correlation will break down (e.g. CEO
change, new technology)
❖ Execution risk (slippage).
February-2018 QuantConnect – Pairs Trading with Python Page 13
LEAN Implementation
Backtest
❖ Create our Trading Signal.
❖ Run Backtest in QuantConnect.
February-2018 QuantConnect – Pairs Trading with Python Page 14
Summary
❖ 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 Thresholds
Total Trades Drawdown Net Profit Sharpe Ratio
270 12.9% 34% 0.555
February-2018 QuantConnect – Pairs Trading with Python Page 15
www.quantconnect.com Thank you.
Appendix
Cointegration and Stationary
• Cointegration is a statistical property of a time
series(like the stock price series).
• Cointegration specifies a co-movement relationship of
price – the long term relationship.
• The mean and the variance of the
series do not vary over time
Cointegration Stationary
• If two series {𝑋𝑡} and {𝑌𝑡} are not stationary
• But their linear combination 𝑌𝑡 = 𝛽𝑋𝑡 − 𝛼 is a stationary process
{𝑿𝒕} and {𝒀𝒕} are cointegrated
How to test if two series are cointegrated?
Augmented Dickey-Fuller test
February-2018 QuantConnect – Pairs Trading with Python Page 18
Our Research Environment
February-2018 QuantConnect – Pairs Trading With Python Page 19
Coding the Idea, The Algorithm Lab
February-2018 QuantConnect – Pairs Trading With Python Page 20
Going Live, Deploying to Live Trading
February-2018 QuantConnect – Pairs Trading with Python Page 21