Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Stop Loss doesn't seem to work

Hello,

The stop loss when using StopOrder doesn't seem to work. I am placing my stop 2% below the previous days high or 2% above the previous days low (if going short). My code is basically detecting local minimums and exiting the trade with the opposite aroon crosses over. The problem is sometimes when the algorithm makes a bad call the stoploss is not working and it gets stuck in a bad trade for too long. If you look at the March 31st trade its clearly not using the stop loss. I have a draw down of over 6% in that one trade alone which should be impossible if the stop was working.

Thanks,
Michael

2 responses

Hey Michael,
I would have to dig through the transactions to know for sure, but my guess is that the issue is with old stop orders being triggered. If you get a buy signal and the price goes up, that might trigger one of the previous stops from a short sale to trigger, and it will buy again. Also, I'm not sure that you can be sure that 2% below the previous days high is less than the current price for your stop, if you place a stop price above the current price, the order won't fill.

I would try canceling old stop orders completely once you place a new 'target order' because you don't want the old stops to trigger at some later date. Below is an example of one way to track orders and cancel old ones. Let me know is this wasn't the problem.

David

def initialize(context):  
    context.orders = {}

def handle_data(context, data):  
    now = get_datetime()  
    # place a new order  
    context.orders[now] = order(some stock, 20, style=StopOrder(some stop price))  
    # cancel all the old ones  
    for dt in context.orders.keys():  
        if dt < now:  
            cancel_order(context.orders[dt])  
            # Delete it from the orders dictionary  
            del context.orders[dt]  

Hi David,

I was able to fix it using the context.stop_loss which was much better and allowes me to use trailing stop losses.