Hi working on longing the best and shorting worst from this list. I am having trouble getting the pipeline to print those 10 stocks (5 best and 5 worst)! I know my custom factor isnt attached but thought someone could help just based off this. Is there a way to put the top 5 into a variable and bottom 5 into a variable and be able to call them later in the code (like in rebalance so I can trade correctly)? Thanks
def make_pipeline(context):
pipe = Pipeline()
# make mask to have desired stocks we want
context.stock_universe = symbols('AAPL','GS','MS','C','JPM','AVGO','DE','HRS','UNH','DIS','AMZN','MA','FB','LLY','PG','CAT','AMGN','BLK','CVX','USB','TGT','UNP','T','SBUX','PM','PFE','PEP','HON','MSFT','MON','INTC','NFLX','TSLA','MYGN')
stock_universe = StaticAssets(context.stock_universe)
#call Last_Close_30 Class
projection_price = Last_Close_30(window_length = 30, mask = stock_universe)
highest_sr = projection_price.sortino.top(10)
lowest_sr = projection_price.sortino.bottom(10)
highest_diff_rank = projection_price.difference.rank(mask = highest_sr)
highest_diff = highest_diff_rank.top(5)
lowest_diff_rank = projection_price.difference.rank(mask = lowest_sr)
lowest_diff = lowest_diff_rank.bottom(5)
# Add the desired values to our pipe.
pipe.add(projection_price, 'projection')
Not getting the output I want its blank because no stock is in the top 5 and bottom 5
#pipe.set_screen(lowest_diff & highest_diff)
return pipe
def before_trading_start(context, data):
# Called every day before market open.
context.output = pipeline_output('my_pipeline')
"""
l_sortino_5_abs = []
s_sortino_5_abs = []
for stock in context.output.highest_diff:
for i in range(0,5):
l_sortino_pos = np.absolute(context.output['projection'][i][3])
l_sortino_5_abs.append(l_sortino_pos)
context.l_sortino_total_abs = sum(l_sortino_5_abs)
for stock in context.output.lowest_diff:
for i in range(0,5):
s_sortino_pos = np.absolute(context.output['projection'][i][3])
s_sortino_5_abs.append(s_sortino_pos)
context.s_sortino_total_abs = sum(s_sortino_5_abs)
"""
print context.output
def my_rebalance(context,data):
for stock in context.portfolio.positions:
if not get_open_orders(stock):
if stock not in context.output.index:
order_target_percent(stock,0)
for stock in context.output.highest_diff.index:
if not get_open_orders(stock):
order_target_percent(stock,(context.output['projection'][stock][3])/context.sortino_total_abs)
for stock in context.output.lowest_diff.index:
if not get_open_orders(stock):
order_target_percent(stock,(context.output['projection'][stock][3])/context.sortino_total_abs)