Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Iterating Through Portfolio to set Stop Loss Order(s)

While I am able to filter through and select the securities to buy, I am having an issue running an iterated stop loss. I am buying securities based on 'usual' price decreases, they buying up those shares as they being to return to a more 'normal' price. However, I want to sell those stocks after a 5% increase in the stocks price. I have written the 'morning_sales' function show below sell those securities after such conditions are met . Unfortunately, I get a syntax error after the stopOrder (in which the price is some percentage relative to the cost_basis per share), typically the error comes on the following line that defines a function to record variables. Any help here would be deeply appreciated. My code is as follows:

def morning_sales(context, data):

context.open_orders = get_open_orders()  

for sec in context.portfolio.positions.items():  
    if sec not in context.open_orders:  
        if data.can_trade(sec):  
            order_target_percent(sec, 0,  
            StopOrder(sec.cost_basis*1.05)

def Record_Variables(context, data):

record(Leverage = context.account.leverage)  
record(Value = context.portfolio.portfolio_value)  
record(Cash = context.portfolio.cash)