Hi, so I have multiple trades open at the same time and I have this code:
#for long
order = self.MarketOrder(self.spy.Symbol, self.mysize * self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price)
stop_loss_order = self.StopMarketOrder(self.spy.Symbol, -self.mysize * self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price, sl1)
take_profit_order = self.LimitOrder(self.spy.Symbol, -self.mysize * self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price, tp1)
#for short
order =self.MarketOrder(self.spy.Symbol, -self.mysize * self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price)
stop_loss_order = self.StopMarketOrder(self.spy.Symbol, self.mysize * self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price, sl1)
take_profit_order = self.LimitOrder(self.spy.Symbol, self.mysize * self.Portfolio.Cash / self.Securities[self.spy.Symbol].Price, tp1)
But if the stop loss is filled then is the take profit automatically cancelled? If not, which I think also that it does not cancel automatically, then how to cancel the take profit if the corresponding order's stop loss is filled and vice versa for long and short orders?
Please give some code for reference or is there a better way like:
self.buy(sl=sl1, tp=tp1, size=self.mysize)
where I can place a order and its stop loss and take profit at the same time in 1 order?
Mia Alissi
To manage the cancellation of take profit orders when a stop loss is filled (and vice versa), you can manually implement this logic in QuantConnect. Here's a conceptual approach:
Place Bracket Orders: When you place a market order, simultaneously place a stop loss and a take profit order.
Track Orders: Maintain a dictionary to track the relationship between the stop loss and take profit orders.
Order Event Handling: Use the
on_order_event
method to listen for order events. When either the stop loss or take profit order is filled, cancel the other order.Here's a code example to illustrate this approach:
This code snippet creates a bracket order by placing a market order along with a stop loss and a take profit order. It manually manages the cancellation of the opposite order when one of them is filled.
Sources: Forum Discussion: Take Profit and Stop Loss on Stop Orders, Forum Discussion on Support for One-Cancels-The-Other Order
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.
Shiven Garg
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!