import numpy as np
def initialize(context):
schedule_function(check_pairs,date_rules.every_day(),time_rules.market_close(minutes=60))
context.AA = sid(45971)
context.UAL = sid(28051)
context.long_on_spread = False
context.shorting_spread = False
def check_pairs(context,data):
AA = context.AA
UAL = context.UAL
prices = data.history([AA,UAL],'price',30,'1d')
short_prices = prices.iloc[-1:]
mavg_30 = np.mean(prices[AA]-price[UAL])
std_30 = np.std(prices[AA]-price[UAL])
mavg_1 = np.mean(short_prices[AA] - short_prices[UAL]
***if std_30 > 0:***
zscore = (mavg_1 - mavg_30)/std_30
if zscore > 1.0 and not context.shorting_spread:
order_target_percent(AA,-0.5)
order_target_percent(UAL,0.5)
context.shorting_spread = True
context.long_on_spread = False
elif zscore < 1.0 and not context.long_on_spread:
order_target_percent(AA,0.5)
order_target_percent(UAL,-0.5)
context.shorting_spread = False
context.long_on_spraed = True
elif abs(zscore) < 0.1:
order_target_percent(AA,0)
order_target_percent(UAL,0)
context.shorting_spread = False
context.long_on_spread = True
record (zscore = zscore )