Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Issue with passing variables into sid from Pipeline

Hi :)

I'm wondering how I would go about ordering stocks from a list, but it seems like quantopian only accepts literals to sid() as arguments for security reasons :( In my buy_stuff method I'm running through a list of ten stocks and trying to set my portfolio as them evenly distributed. I get errors though when passing the loop variable to sid(). How else would I even be able to do this?

def initialize(context):  
    schedule_function(record_vars,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_close())  
    schedule_function(buy_stuff,  
                      date_rule=date_rules.month_start(),  
                      time_rule =time_rules.market_open())  
    schedule_function(sell_all,date_rule=date_rules.month_end(),  
                      time_rule=time_rules.market_close(minutes=5))  
    # Create our pipeline and attach it to our algorithm.  
    attach_pipeline(make_pipeline(), 'my_pipeline')  


def make_pipeline():

    curr_assets = Fundamentals.current_assets.latest  
    curr_liab = Fundamentals.current_liabilities.latest  
    debt = Fundamentals.current_debt.latest  
    num_shares =Fundamentals.available_for_sale_securities.latest  
    working_capital_value = (curr_assets/num_shares) - (curr_liab/num_shares)  
    #Returns filter greater than 0.1:  
    asset_cond = curr_assets >= 1.5 * curr_liab  
    debt_cond = debt <= 1.1 * curr_assets  
    high_dollar_volume = (dollar_volume > 500000)  
    is_tradeable = asset_cond & debt_cond

    #Here's our pipeline. Notice how I used the factors as columns and filters as constituents to the screen:  
    return Pipeline(  
        columns={  
            'working_capital_value':working_capital_value,  
            'current_assets': curr_assets,  
            'current_debt': debt,  
            'current_liabilities': curr_liab  
        },  
        screen = is_tradeable  
    )


def before_trading_start(context, data):  
    results = pipeline_output('my_pipeline')  
    context.my_securities = results.sort_values('working_capital_value', ascending=False).head(10)  
    context.true_secs  = context.my_securities.index  
    context.secs = [stock.sid for stock in context.true_secs]  

def sell_all(context,data):  
    for share in context.portfolio.positions:  
            order_target_percent(share, 0)


def buy_stuff(context,data):  
    for stock in context.secs:  
        #the issue is here :(  
        order_target_percent(symbol(sid),0.1)  
1 response

Using the index directly would be fairly typical & basic:

def buy_stuff(context,data):  
    for stock in context.my_securities.index:  
        order_target_percent(stock, 0.1)  

Meanwhile consider using this pipeline preview for visibility into what you're working with.