Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need help with stop loss

Hey everyone.
oh boy

Thanks,
-Jeff

2 responses

I personally don't have the patience to endure a minutely test on Quantopian. But you can run the following code on daily to see a trailing stop applied using a StopOrder argument on the order method.


def initialize(context):  
    context.UltraVIX = sid(41969)  
    context.StopPrice = None  
    set_commission(commission.PerTrade(cost=2))  
    set_slippage(slippage.FixedSlippage(spread=0))  
def handle_data(context, data):  
    if context.portfolio.positions[context.UltraVIX].amount == 0:  
        if data[context.UltraVIX].price < data[context.UltraVIX].open_price and \  
            data[context.UltraVIX].price < data[context.UltraVIX].mavg(60):  
            order_target_percent(context.UltraVIX, -.75)  
            log.info('SHORT a')  
        elif data[context.UltraVIX].price > data[context.UltraVIX].mavg(60) and \  
            data[context.UltraVIX].price < data[context.UltraVIX].mavg(5):  
            order_target_percent(context.UltraVIX, -1)  
            log.info('SHORT b')  
        else:  
            context.StopPrice = None  
    elif context.portfolio.positions[context.UltraVIX].amount < 0:  
        if get_open_orders(context.UltraVIX):  
            cancel_order(get_open_orders(context.UltraVIX)[0])  
        if (context.StopPrice is None):  
            context.StopPrice = data[context.UltraVIX].price * 1.2  
        else:  
            context.StopPrice = min(context.StopPrice, data[context.UltraVIX].price * 1.2)  
        order_target_percent(context.UltraVIX, 0, style=StopOrder(context.StopPrice))  
        log.info('STOP LOSS {0}'.format(context.StopPrice))  
    record(value=context.account.net_liquidation, unused=context.account.available_funds,  
           Price=data[context.UltraVIX].price, Stop=context.StopPrice)  

I guess maybe this is an example of true short-selling with cover.
Some logging added in case it might help.