Hi everyone,
I know this might be a really obvious question but I really can't figure this out…
How do you properly manage a limit and stop market order at the same time? I've been having issues where they both fill or maybe its the limit order partially filling and then the stop market order filling.
Obviously when one Order gets filled the other gets cancelled but there still seems to be some problem in the code.
Should the stop market order be adjusted if the limit order partially fills? How does everyone handle this? It seems to me that this is a quite important piece of every algorithm yet I didn't find a lot of information about the topic.
I've provided an example that has this problem. Obviously I've placed the orders very close to the current price to illustrate but it also happens with larger ranges.
Thank you very much!
Fred Painchaud
Hello Simon,
It is indeed something that most algos should do. You are quite right.
Right now, a few things are missing in your code to handle the orders. I would invite you to read the following:
In a nutshell, you will need to keep your order tickets and then track those orders in OnOrderEvent. Since you trade only SPY here, it makes the situation more simple but you could look for the more general case.
Also I would like to get your attention to that line of code you have:
if order.Type == OrderType.Limit or order.Type == OrderType.Limit:
😊
One of the two must be StopMarket.
Fred
Vladimir
Simon,
Here is my attempt to recreate the algorithm you posted using LastTradeProfit and automatic position sizing (SetHoldings).
Simon
Thank you both very much! I didn‘t even see that typo maybe that will fix it already!
Is still have some questions:
Thanks again for your fast answers
Fred Painchaud
Hi Simon,
Fred
Vladimir
Here is another version of SL and TP using UnrealizedProfit.
Simon
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!