By Chris Fenaroli, Delaney Mackenzie, and Maxwell Margenot
https://www.quantopian.com/lectures/introduction-to-pairs-trading
https://www.quantopian.com/lectures/mean-reversion-on-futures
This lecture corresponds to the Mean Reversion on Futures lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.
Part of the Quantopian Lecture Series:
In [ ]:
# Useful Functions
def find_cointegrated_pairs(data):
n = data.shape[1]
score_matrix = np.zeros((n, n))
pvalue_matrix = np.ones((n, n))
keys = data.keys()
pairs = []
for i in range(n):
for j in range(i+1, n):
S1 = data[keys[i]]
S2 = data[keys[j]]
result = coint(S1, S2)
score = result[0]
pvalue = result[1]
score_matrix[i, j] = score
pvalue_matrix[i, j] = pvalue
if pvalue < 0.05:
pairs.append((keys[i], keys[j]))
return score_matrix, pvalue_matrix, pairs
In [ ]:
# Useful Libraries
import numpy as np
import pandas as pd
import statsmodels
import statsmodels.api as sm
from statsmodels.tsa.stattools import coint, adfuller
from quantopian.research.experimental import history, continuous_future
# just set the seed for the random number generator
np.random.seed(107)
import matplotlib.pyplot as plt
We'll use some artificially generated series first as they are much cleaner and easier to work with. In general when learning or developing a new technique, use simulated data to provide a clean environment. Simulated data also allows you to control the level of noise and difficulty level for your model.
Determine whether the following two artificial series $A$ and $B$ are cointegrated using the coint()
function and a reasonable confidence level.
In [ ]:
A_returns = np.random.normal(0, 1, 100)
A = pd.Series(np.cumsum(A_returns), name='X') + 50
some_noise = np.random.exponential(1, 100)
B = A - 7 + some_noise
#Your code goes here
In [ ]:
C_returns = np.random.normal(1, 1, 100)
C = pd.Series(np.cumsum(C_returns), name='X') + 100
D_returns = np.random.normal(2, 1, 100)
D = pd.Series(np.cumsum(D_returns), name='X') + 100
#Your code goes here
In [ ]:
cn = continuous_future('CN', offset = 0, roll = 'calendar', adjustment = 'mul')
sb = continuous_future('SB', offset = 0, roll = 'calendar', adjustment = 'mul')
cn_price = history(cn, 'price', '2015-01-01', '2016-01-01', 'daily')
sb_price = history(sb, 'price', '2015-01-01', '2016-01-01', 'daily')
#Your code goes here
In [ ]:
cl = continuous_future('CL', offset = 0, roll = 'calendar', adjustment = 'mul')
ho = continuous_future('HO', offset = 0, roll = 'calendar', adjustment = 'mul')
cl_price = history(cl, 'price', '2015-01-01', '2016-01-01', 'daily')
ho_price = history(ho, 'price', '2015-01-01', '2016-01-01', 'daily')
#Your code goes here
In [ ]:
#Your code goes here
In [ ]:
cl_out = get_pricing(cl, fields='price',
start_date='2016-01-01', end_date='2016-07-01')
ho_out = get_pricing(ho, fields='price',
start_date='2016-01-01', end_date='2016-07-01')
#Your code goes here
This exercise is more difficult and we will not provide initial structure.
The Hurst exponent is a statistic between 0 and 1 that provides information about how much a time series is trending or mean reverting. We want our spread time series to be mean reverting, so we can use the Hurst exponent to monitor whether our pair is going out of cointegration. Effectively as a means of process control to know when our pair is no longer good to trade.
Please find either an existing Python library that computes, or compute yourself, the Hurst exponent. Then plot it over time for the spread on the above pair of stocks.
These links may be helpful:
In [ ]:
# Your code goes here
Congratulations on completing the Mean Reversion on Futures exercises!
As you learn more about writing trading models and the Quantopian platform, enter the daily Quantopian Contest. Your strategy will be evaluated for a cash prize every day.
Start by going through the Writing a Contest Algorithm tutorial.
This presentation is for informational purposes only and does not constitute an offer to sell, a solic itation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. ("Quantopian"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.