-
It's a momentum strategy. You take the mom=ma_long/ma_short. If mom less then 1, it gives a short signal. He only wants to rank stocks to short if the momentum is less than 1. Similarly he only wants to long if mom greater then 1. He then ranks given these conditions. He calls all of the conditions in initialize.
def initialize(context):
pipe = Pipeline()
pipe = attach_pipeline(pipe, name='factors')
He attaches all four to the pipeline and calls them factors.
He uses them when he puts them into results. And then ranks each of the factors by taking the axis=1 mean which is horizontal. Axis=0 is vertical.
Compute final rank and assign long and short baskets.
def before_trading_start(context, data):
results = pipeline_output('factors').dropna()
ranks = results.rank().mean(axis=1).order()
-
This line of code puts each of the stocks in a dictionary file.
context.security_list = context.longs.index.union(context.shorts.index).tolist()
Then he can say
for security in context.shorts.index:
if get_open_orders(security):
continue
if data.can_trade(security):
order_target_percent(security, -context.shorts[security])
for security in context.longs.index:
if get_open_orders(security):
continue
if data.can_trade(security):
order_target_percent(security, context.longs[security])
Something that confused me, is the before_trading_start function does not have to be before the initialize. I would probably switch it the other way. So he creates the pipeline in initialize and the weights in before_trading_start.
This is how he gets the weights for how many longs or shorts.I am not sure what /= means?
context.shorts /= context.shorts.sum()
context.longs /= context.longs.sum()