Back to Community
Trailing stop loss with multiple securities

I'm new to python so bear with me. Im attempting to combine a trailing stop loss with mulitpule securities. I put a rough example together with quantopians' fundamental data example and David Edward's trailing stop. The problem I believe is that context.stop_price just creates a stop price for the first security only and because the other stocks are lower than the context.stop_price they sell the next trade day. I would like some information on how to create a trailing stop loss for a variable amount of securities. Any ideas?

Clone Algorithm
71
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 55b46039b232c20c6dedcb50
There was a runtime error.
10 responses

Hi, You should get the trailstop code for a basket from this thread, scroll down to the latest iteration of the code
https://www.quantopian.com/posts/trailstop-algo-with-200dma-filter-simple-question-thanks

Hi, I may have phrased this question a little incorrectly. I am trying to set up individual trailing stop's for each of the stocks selected. For example, fundamental data would select 10 different stocks at the beginning of each month and eventuallly partition them off as each of their trailing stops is triggered. I believe that my algorithim only creates one trailing stop for one stock and applies it to all other stocks.

Your context.stop_price should be a dictionary indexed by Security objects representing stocks.

Initialize in line 23:

    context.stop_price={}  

Set or update in line 150:

               context.stop_price[stock] = max(context.stop_price[stock] if stock in context.stop_price else 0,  
                                               context.stop_pct * price)  

Use in line 163:

        if (data[stock].price < context.stop_price[stock]) and (current_position > 0):  

Delete in line 165:

            del context.stop_price[stock]  

@Darrell - That code (by David Edwards, posted on Aug. 28, 2014) also uses one scalar variable as a trailing stop for all securities.

Thanks! Runs well. Now just need to think of good place to reinvest cash from trail sells.

Clone Algorithm
71
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 55b5d163baaf4f0c71a6c100
There was a runtime error.

@André Hi, do you mean that One stop triggering inside the basket triggers All stops in the basket ? I dont see that in the log‘s
Thanks

@Darell What he means is your code does not work on multiple securities. You aren't trading multiple securities in your example so it is somewhat confusing that you would offer it as a solution for someone who specifically wants a trailing stop that will work on multiple securities. There is nothing in your code that directs it to trail a separate stop price for multiple securities. There is only one stop price, because the code is only setup to trade one security. If you are able to trade multiple securities with it, please show us. I think that would be the most useful code anyone has ever posted on here.

@James Thanks for pointing that out, i was suspecting that it was not allright. If the elders here could pitch in and show how a trail stop on a basket is done, that would be nice. Thanks

@Darrell, @James - Read Harrison's code of July 27, where he incorporated my suggestions from the day before. The code does work on multiple securities, selected each day before market open - see lines 124-125, 131 and 134; a list is available in context.stocks. Each stock has its own trailing stop price context.stop_price[stock]set or updated in line 150. When the most recent price data[stock].price is lower, the position in stock only, if long, is liquidated.

And here's a version that liquidates the entire basket if any security falls below its trailing stop price, if possible. Note that Quantopian does not allow us to sell positions which are no longer in the universe, or to sell them before updating the universe in before_trading_start.

In 2008, this algorithm updates the universe at the start of each month, buys, then liquidates later the same day or the next. Every month. The minute mode run does better because positions are liquidated sooner.

Clone Algorithm
21
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5648ac8bb7ab0810fe762f68
There was a runtime error.