Back to Community
Auto Adjusting Stop Loss

The goal behind this algorithm was to invest in a broad range of stocks, eliminate the ones that aren't doing well, and "double down" on the ones that are. I used 60k as the initial capital because I wanted to try and create a realistic strategy. I also ran this algorithm with a higher starting capital of $1M and buying shares in blocks of 100, which also showed high returns, peaking at 16x, and ending at 10x.

The stop loss price is set as a function of the rate of return, and trails the current price so as to lock in a profit in the event that the price of a particular security starts dropping.

There is a lot that could be done to further improve this algorithm, some of my initial thoughts:
- Refine initial investment criteria to pick fewer losers.
- Set a initial timeframe during which a stock must go up a certain % otherwise it is sold as a "loser"
- Optimize the setting of the stop loss price
- Testing on a wider range of stocks and time frames

Additionally, this algorithm works best (perhaps only?) during bull markets since it only goes long (hence the start date of 2010). You could probably modify to go short as well, particularly if it started to sense the portfolio as whole a starting to see negative returns.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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: 516c379e02cdb00684aa0e00
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
5 responses

Another thing this algo would benefit from is more strict cash management/order limits. I cloned it and ran a copy, and it leveraged hundreds of thousands of dollars on the $60k initial investment. Check out the sample algorithm, and you'll see it includes some language about calculating the notional value of the portfolio, and orders can only be placed if the notional is within limits. That would make your algo closer to tradeable.


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.

I should have started with: Nice algo! I liked reading it, it has some neat ideas.

HI Dan and Brad
Great community and work
Dan is there a link for the sample algo that you created?
Thank You

On the front page, under the Test heading, there is an option to clone a sample algorithm. This includes the context.min_notional and context.max_notional that Dan is referring to.

Dan, thanks for the feedback. Limits were definitely something I overlooked.

Here is the sample algo. And here are more samples, on several of the API features, in the help doc.