Hi, I am trying to buy stock using a market order, and then have a stop-loss using a stop market order if the price goes at or below a certain price. As I understand from this post:
https://www.quantconnect.com/forum/discussion/8514/stop-market-order-not-triggered-in-backtest/p1a MarketOrder() using a positive number of shares followed by a StopMarketOrder() with a negative number of shares and a stop price below the current price, should be used. However, when I implement this in my code it seems to purchase the stock and then immediately sell the stock, despite the current price being above the stop price. Would someone know what the issue is? Thanks.
Dahui
I think I was misreading the Orders tab results actually, it may be working as intended. Couple other bugs but seem unrelated. Not sure how to delete Discussion posts, sorry.
Derek Melchin
Hi Dahui,
The orders are working as intended. However, it appears to be exiting so quickly sometimes because the algorithm can place trades shortly after the market open. At that point in time, the LOD is very close to the entry price.
The algorithm could be improved by removed AddEquity from the CoarseSelectionFunction. The algorithm automatically subscribes to the symbols returned from this method, so AddEquity is not necessary.
In addition, when creating indicators for securities in a dynamic universe, we should use the full class name (VolumeWeightedAveragePriceIndicator, ExponentialMovingAverage) instead of the helper methods. See the EmaCrossUniverseSelectionAlgorithm for an example.
Lastly, the `isMarketHours` method can be replaced with our IsMarketOpen method, which accounts for shortened trading days.
Best,
Derek Melchin
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.
Dahui
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!