We have an algorithm trading stocks live on Interactive Brokers.
When there is an open position, it creates stop and limit orders for stop loss and take profit.
If stop order receives PartialFill event, limit order is reduced appropriately, on Fill event, it is closed; and vise-versa, of course.
However, I have faced this strange behavior that sometimes order is not being canceled, when it should, here is what I can see in logs (order #2 is stop, order #3 is limit, some unneccessary lines removed):
# added stop and limit orders
2022-06-30 13:37:31 New Order Event: Time: 06/30/2022 13:37:31 OrderID: 2 EventID: 1 Symbol: ASPN Status: Submitted Quantity: -1264 StopPrice: 10.25 Message: Interactive Brokers Order Event
2022-06-30 13:37:31 New Order Event: Time: 06/30/2022 13:37:31 OrderID: 3 EventID: 1 Symbol: ASPN Status: Submitted Quantity: -1264 LimitPrice: 10.62 Message: Interactive Brokers Order Event
# partial and eventually complete fill of stop order
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 4 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -100 FillPrice: 10.2738 USD StopPrice: 10.27 OrderFee: 0.406784 USD Message: Interactive Brokers Order Fill Event - 1164 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 5 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -100 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 0.406776 USD Message: Interactive Brokers Order Fill Event - 1064 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 6 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -700 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 4.947429 USD Message: Interactive Brokers Order Fill Event - 364 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 7 Symbol: ASPN Status: PartiallyFilled Quantity: -1264 FillQuantity: -100 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 0.706776 USD Message: Interactive Brokers Order Fill Event - 264 remaining
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 2 EventID: 8 Symbol: ASPN Status: Filled Quantity: -1264 FillQuantity: -264 FillPrice: 10.27 USD StopPrice: 10.27 OrderFee: 1.865887 USD Message: Interactive Brokers Order Fill Event
# cancelling limit order
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 2 Symbol: ASPN Status: CancelPending Quantity: -1164 LimitPrice: 10.62
# trying to update limit order on Partial fills
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 3 Symbol: ASPN Status: UpdateSubmitted Quantity: -564 LimitPrice: 10.62 Message: Interactive Brokers Order Event
# this is my code logging partial fills
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 100.0
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 100.0
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 700.0
2022-06-30 13:38:32 Partial fill for stop loss: reducing qty by 100.0
# Status=New??? Why would would it create a new order now?
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 4 Symbol: ASPN Status: New Quantity: -564 LimitPrice: 10.62 Message: Interactive Brokers Order Event
2022-06-30 13:38:32 New Order Event: Time: 06/30/2022 13:38:32 OrderID: 3 EventID: 5 Symbol: ASPN Status: UpdateSubmitted Quantity: -564 LimitPrice: 10.62 Message: Interactive Brokers Order Event
So in my understanding, when PartialFill event comes later that Filled event (which is understandable in async environment), my code first cancels order and then tries to update its quantity. I would expect in this situation for order to be left canceled, but instead it has recreated a new order. It doesn't seem right to me.
Sorry, I cannot share the full code, but it is pretty straightforward and was working as described above for quite some time without issues.
Louis Szeto
Hi Yuri
We recommend the implementation of Placing Active Orders of One Cancels the Others Orders. You may want to add additional logic on PartiallyFilled with Update the size of other orders.
Best
Louis
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.
Yuri Lopukhov
Hi, Louis Szeto this is basically how I implemented it, how does it solve the problem I am having?
Louis Szeto
Hi Yuri
I’m not sure about why your algorithm would return a quantity of 564 when there should have no state of remaining 564 shares following the flow of your logs. Have you try:
? That way you would have use the singular portfolio state rather than outdated data.
Best
Louis
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.
Yuri Lopukhov
Hi, Louis, the problem I am trying to solve is in the subject, it is when Lean creates a new order, when I am simply trying to update an order, which is an unexpected behavior in my opinion. I do not expect that when I am trying to update an order, that it would create a new one.
Yuri Lopukhov
Perhaps it will be more clear if you focus on these lines in log:
as you can see here, at the same time first I try to cancel order, then to update its quantity. This is because PartialFill event was sent in my handler AFTER the Fill event. That is why my code first cancels order (on Fill event), then tries to update it (on PartialFill event)
and then you can see this:
Here order status was reset to New. Instead of staying canceled, it was changed back to New.
So now that I think about it, I guess it is properly to say that the order was uncancelled, i.e. its status was reset from Cancelled to New? Still not sure if this is expected behavior. In real trading I expect cancelled order to remain cancelled.
Also, I was not able to re-create this behavior in backtesting. Perhaps this only happens when order has status CancelPending, or there is some other difference between backtesting and live.
.ekz.
Any additional thoughts on this Louis Szeto?
Alexandre Catarino
Hi Yuri Lopukhov ,
Please send an email to support@quantconnect.com.
We can try to fetch the syslog of that live deployment to understand what happened.
We have had reports about orders with OrderStatus.New that the Engineering Team has addressed. Orders were submitted to IB but never got a response, so the order status didn't change from OrderStatus.New to OrderStatus.Submitted. It looks like your case is different, but we cannot be sure without reading the syslogs. Regardless, the fixes were implemented after you created this thread.
Best regards,
Alex
Yuri Lopukhov
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!