Hi community,
I am new here, so sorry for the dumb question. I am trying to wrap me head around the different type of orders and how to manage them.
I am backtesting on a daily frequency, so on day X-1 I find a good setup and I want to trade on day X, then I submit a StopMarketOrder within the on_data block on day X-1, so that as soon as the price hit the mark on day X, it trigger the MarketOrder that gets filled ASAP. This works fine as expected, but the problem is about TP (take profit) and - mainly - about SL (stop loss), which is my biggest concern.
If I submit the SL StopMarketOrder at the same time on Day X-1, sometimes it got hit before the other order takes place, so I enter in a short positon that I have no intention to have.
I tried to submit it based on the even on order, so when my actual order get filled, but it get delayed and actioned the day X+1, which is too late and I took the fall already.
So, is it there a way to submit both, but only have it if the first one is actually filled (or partially filled)?
Thanks for your advices in advance. Michele
Mia Alissi
Here's a corrected version of the algorithm to manage Stop Loss (SL) and Take Profit (TP) orders effectively:
This code sets up a basic framework for managing SL and TP orders after an initial StopMarketOrder is filled. Adjust the logic as needed to fit your specific trading strategy and conditions.
Sources: Stop Market Orders - QuantConnect.com, Other Order Types - QuantConnect.com, Trading and Orders - QuantConnect.com, Trailing Stop Orders - QuantConnect.com, Stop Limit Order Regression Algorithm
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.
Michele Facco
Thank you bot Mia, but this doesn't seem to be able to solve the issue. Also, what about the two on_data statements?! 😅
Louis Szeto
Hi Michele
I believe you mean market-on-open order. You can set up your take profit and stop loss order after it gets filled using the on_order_event handler.
You can find more details here.
Best
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.
Michele Facco
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!