Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Stop Order Issues

Hey Quantopianites!

I have been building this code for quite some time now and but for the life of me I cannot get a 10% stop loss working. I have it rebalancing quarterly so I couldn't use the StopOrder function since it will only look for stop losses quarterly. any hints or guidance would be great!

-Marco

3 responses

Maybe replace your stop loss function with something like this....

def do_daily(context, data):  
    # iterate through the currently held positions  
    for security, position in context.portfolio.positions.items():  
        stock_price = position.last_sale_price  
        stock_basis = position.cost_basis  
        stop_price = stock_basis - (stock_basis * 0.01)

        # Sell at market if the last price is <= the stop price  
        if stock_price <= stop_price:  
            order_target(security,0)  
            print "SOLD STOPLOSS"

Here's a backtest with that stop loss function

Thanks Dan this is exactly what I was looking for!