OCO are convenient for bracket orders and exclusive entry for double setups. OCA is similar and quite useful for entering in different securities. Order attachment is useful for placing stop loss and take profit as soon as the entry order is filled (and those SL/TP orders need to be tied by OCO). I know IB supports this natively, but this could be made transparent to algorithms by having brokerage classes do this client side if server functionality is not available.
This is not a huge priority to me, since I can always create my own order manager, but I wonder if there are plans to support this.
Keep up the awesome work!
Jared Broad
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.
Rahul Chowdhury
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.
Jacob Barbee
Rahul Chowdhury, does this allow for multiple positions to be open at a time?
Â
Jacob Barbee
I did a 1 Bracket OCO Order, but how would I do this if I want there to be up to 5 open positions at one time? Would this allow that to happen or can there only be one order open at a time. I thought it could be possible if each order has a specific Order ID Number.
Varad Kabade
Hi Jacob Barbee,
We can extend the above implementation for multiple open orders by using a dictionary to store the order tickets by the order ID. Note that Lean does not allow multiple open positions. If you need further assistance regarding the same, please open a new thread.
Best,
Varad Kabade
Dmitri Pavlenkov
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!