Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Set screen and trading question

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)  
5 responses

To get a list of securities passing some pipeline filter all one needs to do is put the filter into the pipeline definition. I'm assuming that the 5 best and 5 worst stocks are 'lowest_diff' and 'highest_diff'. So, since you already have the filters created, simply add them to the pipe...

pipe.add(lowest_diff, 'lowest_diff')  
pipe.add(highest_diff, 'highest_diff')  

This adds two boolean columns to the pipeline output that can then be filtered on.

context.output = pipeline_output('my_pipeline')  
context.best_5 = context.output.query('highest_diff == True).index.tolist()  
context.worst_5 = context.output.query('lowest_diff == True).index.tolist()

We now have two lists of securities ('context.best_5 ' and 'context.worst_5 ') which can be used to trade on.

for stock in context.best_5:  
    if not get_open_orders(stock):  
        order_target_percent(stock,(context.output['projection'][stock][3])/context.sortino_total_abs)  

for stock in context.worst_5:  
    if not get_open_orders(stock):  
        order_target_percent(stock,(context.output['projection'][stock][3])/context.sortino_total_abs)  

I tried that but i keep getting this error for the part where I make the context.best_5 and context.worst_5
ValueError: expr must be a string to be evaluated, given
??

Thanks

given **

boolean given

Quantopian offers a great debugging tool for the IDE https://www.quantopian.com/help#debugger. Try setting breakpoints and checking for variable values to trace the problem. You can actually look at the value for an EXPRESSION in the debugger. I often check the type of a variable by entering 'type(my_variable)'. Remember variable types are not static in Python and can change. This often causes problems.

Anyway, attached is the basics of what you are trying to do and it seems to work without an error. You didn't attach a backtest (which is always helpful) so I had to make some adjustments to your code.