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.
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.
Hey Ming,
Lean does not have built-in OCO orders, instead we have to implement that feature ourselves. We can do this by using the OnOrderEvent method, which fires each time there is an order event. By keeping track of the order tickets for each order, we can cancel one order as the another fills.
For example, let's consider a situation where we enter a position with a stop loss and 2 take profit limit orders.
self.entryTicket = self.StopMarketOrder("AAPL", 100, self.stopBuyPrice)
self.stoplossTicket = self.StopMarketOrder("AAPL",-100,self.stopLossPrice)
# Enter limit order 1
self.profit1Ticket = self.LimitOrder("AAPL",-50,self.profitTarget1)
# Enter limit order 2
self.profit2Ticket = self.LimitOrder("AAPL",-50,self.profitTarget2)
If our stop loss fills, we need to cancel both of our take profits. If our first limit order fills, we need to update our stop loss to account for the change in our position. If both our limit order fills, we need to cancel our stop loss.
Check out the back test below as an example.
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.
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!