Back to Community
How to add a stop loss if it continues to loss in 4 days

As the title, how to add a stop loss order if it continues to loss in 4 days?
Could you provide an example? Thanks.

4 responses

create a scheduled function that will check the profit/loss of each security you hold at the first minute of every day, if the gain is negative then add +1 to a counter, if the counter is = 4 then sell. If the gain is positive then reset the counter to 0.

How do I control the draw down?

This is a sample algorithm which does a 4 day stop loss.

Each day, it retrieves the price history for your stock(s). It checks if that price history is consecutively descending, if so, it closes the position.

Hope this helps!

Clone Algorithm
14
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: 5807b4f392d7bf13f831f40f
There was a runtime error.
Disclaimer

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.

Hi Matthew, this looks like a brilliant code, thanks for sharing it. Please, I'm quite new to Quantopian and I hope you could apologize my simple question.
I see you're running the function "stop_loss" as a scheduled daily function.
When the code executes:
price_data = data.history(context.stock, "price", 4, "1d")
say today is Oct 7th, 2016,
what's the most recent value given by that instruction? is it the open price as of 9.30am of Oct, the 7th?
Also, are you sure it's:
return all(earlier >= later for earlier, later in zip(array, array[1:]))

I get increasing values rather than decreasing, so I'm a little confused.
Many thanks!
Angelo