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?

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.

@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.

@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.

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian.

In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian.

In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.