100% found this document useful (1 vote)
106 views

2018 WB 2869 - QuantConnect PairTradinginPython

This document introduces pairs trading using a Python algorithm on the QuantConnect platform. It discusses: 1) QuantConnect, which provides an algorithm development environment and backtesting tools. 2) The concept of pairs trading, which involves identifying two stocks with highly correlated prices, establishing a trading rule based on deviations from their typical price relationship, and taking offsetting positions to profit from reversion to the mean. 3) Implementing a pairs trading strategy in Python using the QuantConnect backtester to analyze trades, drawdown, profits and risk-adjusted returns based on historical data.

Uploaded by

Zen Trader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
106 views

2018 WB 2869 - QuantConnect PairTradinginPython

This document introduces pairs trading using a Python algorithm on the QuantConnect platform. It discusses: 1) QuantConnect, which provides an algorithm development environment and backtesting tools. 2) The concept of pairs trading, which involves identifying two stocks with highly correlated prices, establishing a trading rule based on deviations from their typical price relationship, and taking offsetting positions to profit from reversion to the mean. 3) Implementing a pairs trading strategy in Python using the QuantConnect backtester to analyze trades, drawdown, profits and risk-adjusted returns based on historical data.

Uploaded by

Zen Trader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Pairs Trading in Python

Introduction and application of classical pairs trading

Jared Broad
CEO and Founder

www.quantconnect.com
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


How do we do it?
FINANCIAL
DATA
EQUITIES
LEAN ALGO OPTIONS
TECHNOLOGY POWER
FUTURES
COMPUTING
FOREX
CRYPTO

We’ve built a web algorithm lab where thousands of

people test their ideas on financial data we provide; for free.

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


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.

February-2018 QuantConnect – Pairs Trading with Python Page 8


Example Price Divergence

Price Series of Stock A and B Stock A Stock B


14
12
Price(B) = 2 * Price(A) + 1
10
8
6
4
2
0
Jan-08 Feb-08 Mar-08 Apr-08 May-08 Jun-08 Jul-08 Aug-08

Price Divergence Stock A Stock B


16
14
12
10
8
6
4
2
0
Jan-08 Feb-08 Mar-08 Apr-08 May-08 Jun-08 Jul-08 Aug-08

February-2018 QuantConnect – Pairs Trading with Python Page 9


Basic Idea of Pairs Trading

When assets cross divergence threshold; trigger trade.

Sell CVX

Sell XOM

Buy XOM

Buy CVX

Sell CVX
Ratio Upper Threshold
Buy XOM
Standard Deviations

Sell XOM

Ratio Lower Threshold


Buy CVX

February-2018 QuantConnect – Pairs Trading with Python Page 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 Reversion

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

February-2018 QuantConnect – Pairs Trading with Python Page 10


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

❖ Create our Trading Signal.

❖ Run Backtest in QuantConnect.

Backtest

February-2018 QuantConnect – Pairs Trading with Python Page 14


Summary

Total Trades Drawdown Net Profit Sharpe Ratio


270 12.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 Thresholds

February-2018 QuantConnect – Pairs Trading with Python Page 15


www.quantconnect.com
Thank you.
Appendix
Cointegration and Stationary

Cointegration Stationary

• Cointegration is a statistical property of a time


series(like the stock price series). • The mean and the variance of the
• Cointegration specifies a co-movement relationship of series do not vary over time
price – the long term relationship.

{𝑿𝒕 } and {𝒀𝒕 } are cointegrated

• If two series {𝑋𝑡 } and {𝑌𝑡 } are not stationary


• But their linear combination 𝑌𝑡 = 𝛽𝑋𝑡 − 𝛼 is a stationary process

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy