"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
import quantopian.algorithm as algo
import quantopian.optimize as opt
from sklearn import cross_validation
from sklearn.linear_model import LinearRegression,Lasso
import statsmodels.api as sm
from quantopian.algorithm import order_optimal_portfolio
import numpy as np
import pandas as pd
import quantopian.optimize as opt
import scipy as sp
import talib
MAX_GROSS_EXPOSURE = 1.0
MAX_SHORT_POSITION_SIZE = 1.0 # 1.5%
MAX_LONG_POSITION_SIZE = 1.0 # 1.5%
# Scheduling Parameters
MINUTES_AFTER_OPEN_TO_TRADE = 25
BASE_UNIVERSE_RECALCULATE_FREQUENCY = 'close'
def update_parameters(context,data):
prices = data.history(context.future_list, 'price', context.window, '1d').dropna()
rets = np.log(prices).diff().fillna(0)
context.b = find_coint_coef(prices,context)
resid = rets.iloc[:,0]*context.b-rets.iloc[:,1]
resid = resid.dropna()
context.lb = int((np.log(2)/m_hurst(resid))*context.ml)
record(lookback=context.lb)
record(regrCoef = context.b)
def initialize(context):
"""
Called once at the start of the algorithm.
"""
# Rebalance every day, 1 hour after market open.
cl_lst = []
context.maxFuture = 20
set_benchmark(sid(28320))
cl_lst.append(continuous_future('CL', offset=0, roll='calendar', adjustment="mul"))
cl_lst.append(continuous_future('CL', offset=3, roll='calendar', adjustment="mul"))
context.window = 250
context.future_list = cl_lst
regr = LinearRegression()
context.regr = regr
context.b = 0.86
context.ml = 2
context.lb = 25
context.min_vol = 500
# Create our dynamic stock selector.
algo.schedule_function(
do_portfolio_construction,
date_rule=algo.date_rules.every_day(),
time_rule=algo.time_rules.market_open(minutes=MINUTES_AFTER_OPEN_TO_TRADE),
half_days=False
)
algo.schedule_function(
update_parameters,
date_rule=algo.date_rules.month_end(),
time_rule=algo.time_rules.market_open(minutes=MINUTES_AFTER_OPEN_TO_TRADE),
half_days=False
)
def prepair_alpha(context,data):
prices = data.history(context.future_list, 'open', context.lb+1, '1d').dropna()
vol = data.history(context.future_list,"volume",3,"1d").dropna()
avg_vol = vol.mean(axis=0)
min_vol = np.min(avg_vol)
record(minivolume = min_vol)
cutOff = min_vol > context.min_vol
rets = np.log(prices).diff().dropna()
resid = rets.iloc[:,0]*context.b - rets.iloc[:,1]
stdev = resid.std()
upper = resid.mean()-resid
zScore = upper/stdev
zScore = zScore[-1]*cutOff
assets = [data.current(x,"contract") for x in rets.columns]
output = pd.Series(np.array([zScore,-zScore]),index=assets)
return output
def find_coint_coef(pricing,context):
rets = np.log(pricing).diff().dropna()
xRet = rets.iloc[:,0]
yRet = rets.iloc[:,1]
x = xRet.reshape(len(rets),1)
y = yRet.reshape(len(rets),1)
context.regr.fit(x,y)
b = float(context.regr.coef_)
return b
def m_hurst(X):
X = np.array(X)
N = X.size
T = np.arange(1, N + 1)
Y = np.cumsum(X)
Ave_T = Y / T
S_T = np.zeros(N)
R_T = np.zeros(N)
np_std = np.std
np_ptp = np.ptp
for i in range(N):
S_T[i] = np_std(X[:i + 1])
X_T = Y - T * Ave_T[i]
R_T[i] = np_ptp(X_T[:i + 1])
R_S = R_T / S_T
R_S = (R_S)[1:]
n = (T)[1:]
A = np.column_stack((n, np.ones(n.size)))
[m, c] = np.linalg.lstsq(A, R_S)[0]
H = m
return H
def do_portfolio_construction(context, data):
context.alpha = prepair_alpha(context,data)
order_optimal_portfolio(
opt.TargetWeights(context.alpha),
constraints=[opt.MaxGrossExposure(1.0)])
record(pos1 = context.portfolio.positions[context.alpha.index[0]].amount)
record(pos2= context.portfolio.positions[context.alpha.index[1]].amount)
def handle_data(context,data):
"""
Called every minute.
"""
pass