21
Pairs Trading in Python Introduction and application of classical pairs trading www.quantconnect.com Jared Broad CEO and Founder

Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

  • Upload
    others

  • View
    11

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

Pairs Trading in PythonIntroduction and application of classical pairs trading

www.quantconnect.com

Jared Broad

CEO and Founder

Page 2: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 3: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 4: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 5: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

Building Thousands of Algorithms Every Day

February-2018 QuantConnect – Pairs Trading with Python Page 5

Page 6: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 7: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 8: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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.

Page 9: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 10: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 11: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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 𝑋𝑡

Page 12: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 13: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 14: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

LEAN Implementation

Backtest

❖ Create our Trading Signal.

❖ Run Backtest in QuantConnect.

February-2018 QuantConnect – Pairs Trading with Python Page 14

Page 15: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 16: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

www.quantconnect.com Thank you.

Page 17: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

Appendix

Page 18: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

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

Page 19: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

Our Research Environment

February-2018 QuantConnect – Pairs Trading With Python Page 19

Page 20: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

Coding the Idea, The Algorithm Lab

February-2018 QuantConnect – Pairs Trading With Python Page 20

Page 21: Pairs Trading in Python - Interactive Brokers · 2018-02-16 · Pairs Trading –Market Neutral Trading Strategy Pairs trading is a type of statistical arbitrage Basic Idea: 1) Select

Going Live, Deploying to Live Trading

February-2018 QuantConnect – Pairs Trading with Python Page 21