I find out that stop market order is not triggered even when the price is way below stop price in backtesting mode.
For example, I would like to buy 100 AAPL stocks and I used self.StopMarketOrder('AAPL', 1, 304.53). And I changed the order fill time as the trailing stop bootcamp taught us: stopMarketOrderFillTime = datetime.min. I also change the universe resolution to minute even though I don't know if it helps like this: self.UniverseSettings.Resolution = Resolution.Minute. I'm pretty sure the price was hit during the period of backtest. However, I still held the position even when the backtest ended. And I faced this problem many times.
I'd like to ask whether this kind of order only work in live mode. Or did I do it wrong?
The following is a simple example of what I did:
class Example(QCAlgorithm):
def Initialize(self):
self.UniverseSettings.Resolution = Resolution.Minute
stopMarketOrderFillTime = datetime.min
self.SetStartDate(2020, 5, 12) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
# self.AddEquity("SPY", Resolution.Minute)
self.AddEquity("SPY", Resolution.Minute)
self.AddEquity("AAPL", Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay("SPY"),self.TimeRules.AfterMarketOpen("SPY", 1), Action(self.Rebalance))
self.bought = False
def Rebalance(self):
if not self.bought:
self.StopMarketOrder("AAPL",100,304)
self.bought = True
Shile Wen
Hi Ken,
A Stop Market Order buys shares when the price is greater than or equal to the price specified, so in this case, if the Stop Market Order is at $304, any price >=$304, e.g. $311, will be executed. This is the reason the algorithm buys shares at a higher price. I believe the desired order is a Limit Order, which buys shares only when the price is less than or equal to the specified price. This way, at a price such as $300, the Limit Order will be executed. I’ve attached a backtest on how to use a Limit Order, and check out our documentation on types of Orders here.
On a side note, Action() is no longer needed.
Best,
Shile Wen
Louis Szeto
Hi Shile,
I believe that's not what Ken meant, because I had the same problem. It was supposed a Stop order was placed as drawdown cap from the StopMarketOrder. Yet, when there was outstanding quantity invested and the ticker price is lower than the stop price, the stop order is not executed in the backtest, making the drawdown of a single investment miserable.
Cheers
Louis
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Ken cheung
Thx Shile. But I don't think LimitOrder is what I want. I'd like to buy, in this case, 'AAPL' at any market price but, as Louis said, place a stop to cap my max drawdown. So one bad investment won't eat up all my efforts before. However, the stop order was not triggered even when the apple price was below 304.
Thx Louis for raising the same question with me.
Shile Wen
Hi Ken,
To buy at any price, we can use a MarketOrder, and to sell off our stock when it goes down to a certain price, we can use a Sell StopMarketOrder. Thus, we can combine these two orders to buy a stock then sell it off when the price is going down to limit our drawdown. I’ve attached a backtest with an example.
Best,
Shile Wen
Ken cheung
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!