Any Idea on how to apply this with results from pipeline? Cant figure out how to implement so far I have...
def before_trading_start(context, data):
context.data = pipeline_output('my_pipeline').dropna()
context.data_sort = context.data.sort(['eps_growth'], ascending=False).iloc[:200]
context.final_longs_sort = context.data_sort.sort(['dollar_volume'], ascending=False).iloc[:100]
context.long_final = context.final_longs_sort
context.security_list = context.long_final.index
context.hedge = symbol('TLT')
log.info(context.long_final)
def rebalance(context, data):
stock = context.security_list
bond = context.hedge
period = 70
target_lev = 1.00
pw = 0.5
price_hedge = data.history(context.hedge, 'price', period+1, '1d')[0:-1]
ret_sum1 = price_hedge.pct_change().sum()
prices = data.history(context.long_final.index, 'price', period+1, '1d')[0:-1]
ret_sum2 = prices.pct_change().sum()
r_diff = ret_sum2[stock] - ret_sum1[bond]
#record(r_diff = r_diff)
if r_diff>0.0:
adpt = (1+r_diff)**pw-1.0
elif r_diff<0.0:
adpt =-((1+abs(r_diff))**pw-1.0)
else:
adpt =0.0
wt_stock = target_lev*(0.50+adpt)
wt_bond = target_lev*(0.50-adpt)
order_target_percent(stock, wt_stock)
order_target_percent(bond, wt_bond)
record(wt_stock = wt_stock, wt_bond = wt_bond)
record(Leverage = context.account.leverage)