I'm looking at the codebase and not really seeing an elegant way of submitting order properties on a single order. I'm looking to submit a StopMarketOrder, however also want to add order properties (IOrderProperties) that will tell the BacktestingBrokerage to CloseOnTrigger, thereby simulating Bitmex's Close on Trigger feature. This essentially tells the exchange to only reduce your position.
I've only come across DefaultOrderProperties but looks like the intention is for all orders to execute with that, and is not included in the order requests (e.g. StopMarketOrder) but only used further down the stack.
Curious if others have wanted to do the same and how they went about it. Don't really like the idea of setting the Brokerage's DefaultOrderProperties before the execution, and then revert it back afterwards. Feels like you're likely to run into some race/multithreaded anomoly. OrderProperties does execute a memberwise clone so it should work in theory.
Rahul Chowdhury
Hey Henmo,
It is possible to implement close on trigger yourself with the OnOrderEvent method. This method executes every time an order event takes place, such as an order being submitted, cancelled, filled, etc.
For example, let's say you placed a market order with a stop loss order and a take profit limit order.
self.marketTicket = self.MarketOrder("SPY", quantity) self.limitTicket = self.LimitOrder("SPY", -quantity, takeProfit) self.stopTicket = self.StopMarketOrder("SPY", -quantity, stopLoss)
With the order tickets, you can update the limitOrder or the stopMarketOrder if either is filled.
OnOrderEvent(self, orderevent): if orderevent.Status == OrderStatus.Filled: orderId = orderevent.OrderId # If limit order has been filled, we cancel our stop loss if self.limitTicket is not None and orderId == self.limitTicket.OrderId: self.stopTicket.Cancel() # If stop order has been filled, we cancel our limit order elif self.stopTicket is not None and orderId == self.stopTicket.OrderId: self.limitTicket.Cancel()
Henno van Rensburg
Perhaps I wasnt clear in my description, but the order needs to be submitted to the exchange with this particular flag set. Let's say spot are at 50 and you are long 1000 units, and have a stop order of -2000 with the Close on Trigger set (reduce only), if the market moves to 40, you'll end up with no position (0 holdings) instead of being short 1000.
No OnOrderEvent can help in this regard, unless you execute a reverse order to get back to 0. Not practical given fees and drift.
Jared Broad
Hi Henno;
Unfortunately not yet. We run a balance of making the API simple enough for everyone and flexible enough to handle all the things people request. Currently given the infinite "special cases" N-brokerages and M-order types we've focused on the common subset of orders which are in all brokerages and their behaviors for LEAN.
It might be nice to create a generic submit order API method that can take an "IOrder" which can define a special way to behave and his own properties. If you're interested in developing a pull-request to LEAN we can collaborate with you to get it merged and then it'll be available in live-trading here.
Best
JB
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.
Henno van Rensburg
Thanks for the reply Jared. Seems like adding an IOrderProperties argument (optional/overload) for the various order submission methods on \Algorithm\QCAlgorithm.Trading.cs will be a generic enough implementation. The current implementation allows for custom IOrderProperties to be set on the Algo but it's assumed that will be used for all orders, whereas the requirement I have is to pass along different IOrderProperties for each order.
Alternatively, order submission can be executed in a way that mutates the DefaultOrderProperties and revert thereafter, since a memberwise clone is taken it should be safe. Although this does feel slightly more like a hack tbh.
Before creating a merge request would be good to get your thoughts on this. Not really a fan of loading the codebase with infinite overloads... it's a slippery slope ;-)
Jared Broad
> Not really a fan of loading the codebase with infinite overloads... it's a slippery slope ;-)
Me either! That's why I was suggesting we made an order method that takes a generic order. i.e.
MarketOrder() <-- internally creates an order, add it to Transactions.AddOrder
Order( new MyCustomOcoOrder() ) <--- creation of specific order type up to the caller; and then add it to the Transactions.AddOrder method.
You absolutely could make what you mentioned above (cloning the IOrderProperties) if you wanted; even as a simple helper method in your algorithm. If wanting to build it for your algorithm that would be the fastest way.
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.
Henno van Rensburg
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!