Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
TrailStop Algo with 200Dma Filter, Simple Question - Thanks

Hi, having issues with integrating a N Day Sma Filter to a Long/Short TrailStop strategy. I have the trails calculated somewhat (via min/max) problem is i want to only take Long trail signals if price > 200dma and only short trail signals when < 200dma. Also the price cross below or above N day sma should override any trail stop thats active at that moment. I think i have the if and else conditions wrongly structured, could someone please look it over.
Thanks for your time

8 responses

Sorry, wrong backtest - here is the new one with code commented properly
Thanks

I think you are on the right track, I changed a couple small things. It checks for the sell conditions first and updates the stop price no matter what since you use the min/max trailing stop. I made all the elif statements if statements because they shouldn't interfere with each other. It doesn't process the rest of the bar if it closes its position, it will wait until the next bar for a buy/sell signal. I also have it initializing the stop price with None.

Hi David and thanks for the mods, its almost quite what i had in mind, need one more alteration, please look at the graph, hope its clear.
I think the code needs one more set of trade conditions (reverse conditions?) that would define beahaviour in respect to the trail stop (setting and obeying the BuyStop/SellStop depending on on what side of maverage the price is)
https://www.dropbox.com/s/u7mcz6ugkk3niqo/Screen%20Shot%202014-08-18%20at%2023.23.52.png

I took a closer look and I think I see your problem, correct me if I'm wrong. When your trailing stop is triggered, it does try to go flat, but because it is still above its moving average, it immediately re-enters the position on the next bar. It's is probably symmetric on the short side too.

If that's the problem, you'll have to come up with some logic to handle when your algo enters that no-mans land after a trailing stop is triggered, but before the price has crossed to the other side of the moving average. You'll need some type of re-entry rule before it can trade on the same side again. Hopefully it's an easy fix, but I think that's what is causing the problem

David, you are correct. I got it so far that it behaves as it should when in long mode (price > maverage) and it even trails the stop down when in short mode (price

Damit, it cropped all the text i had, here it is again

Its almost there, in long mode it behaves correct, here are the remaining issues
1. It should go long or short at the price crossing the maverage, no matter what stop is saying at that moment
2. Position sizes dont make sense, it only trades 1-2 shares max at this version
3. Stop behaviour when price below maverage is not correct, that is it trails down well but not when stopped out of short, you can see it on the graph when disabling Position plot

Additional stuff:
- Tried to add your datetime check for eod trading, but it did not proceed at all, i added this code below handle data

    time = str(get_datetime().astimezone(timezone("US/Eastern")))  
    if time != '16:00:00': # only trading at this time once a day  
        return  
  • Would it be better off using talib.atr ? (less code, as dont have to min/max the stops manually)

Thanks

Darell,
You could certainly use something like atr for your stops, theres no correct answer to that one really. This might be useful, but its pretty math heavy, Optimal Trading Stops and Algorithmic Trading. I'll take a closer look later.

It looks like you were using order_target when you meant to use order_target_percent, I changed that and now it doesn't just get the 1-2 shares.