I am converting my backtesting algorithm to Live. I know there are two main areas I have to address:
- Collecting history to build up my indicators prior to processing live candles. This seems pretty straightforward and I can test it.
- Dealing with broker disconnects. Worst case is, a Stop or Limit order gets filled during the disconnect, and I have to reconcile the status of open orders between my code and reality. Which raises two questions:
- After a disconnect/reconnect will QuantConnect be out of sync with the broker? For instance, if I query an order status, will QC show me incorrect information? Or will it query the broker?
- Realistically I don't see how I will be able to test this code - reconciling order status after a broker disconnect (or a program restart). I would really love to see some working code that handles this, if anyone can provide that.
Thank you.
Mia Alissi
QuantConnect provides event handlers to deal with broker disconnects and reconnects. When the brokerage connection restores after it disconnects, your algorithm is notified through the
OnBrokerageReconnect
event handler. Similarly, if the brokerage connection breaks, your algorithm is notified through theOnBrokerageDisconnect
event handler. Here are the Python code examples for these handlers:(Source: QuantConnect Documentation)
In live trading, orders fill asynchronously. QuantConnect sends your order to the API of your brokerage and waits for their response to update the state of your algorithm and portfolio. Therefore, after a disconnect/reconnect, QuantConnect should not be out of sync with the broker. When your order fills, the fill price and fee is set by your brokerage. You can add event handlers to your algorithm to monitor the brokerage connection and brokerage messages.
(Source: QuantConnect Documentation)
As for testing the code for reconciling order status after a broker disconnect, it might be challenging to simulate this scenario. However, you can write your code in a way that it checks the order status before performing any operation on it. For instance, you can avoid the
OrderResponseErrorCode.InvalidNewOrderStatus
(-32) error in live trading by checking theStatus
property of the order ticket or order event before you update or cancel an order. Here is a Python code example:(Source: QuantConnect Documentation)
I hope this helps! Let me know if you have any other questions.
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.
Lee Allen
Thank you Mia! There are two things I would like to clarify:
QuantConnect sends your order to the API of your brokerage and waits for their response to update the state of your algorithm and portfolio. Therefore, after a disconnect/reconnect, QuantConnect should not be out of sync with the broker.Â
Suppose I submit a StopMarketOrder which may fill in minutes or hours (or never). And then it fills during a broker disconnect. After reconnecting to the broker I assume I will not receive an OrderEvent ‘Fill’ event. If I read the OrderTicket.Status will it show Filled? In other words, does that query the broker to get the status, or does it rely on some tracking within LEAN that may be out of sync?
For instance, you can avoid the OrderResponseErrorCode.InvalidNewOrderStatus (-32) error in live trading by checking the Status property of the order ticket or order event before you update or cancel an order. Here is a Python code example:
I really like this, it will make my code more robust.Â
Thank you
Mia Alissi
QuantConnect's LEAN engine is designed to handle broker disconnects and maintain the state of your algorithm and portfolio. When a disconnect occurs, LEAN will attempt to reconnect and synchronize with the brokerage. This includes updating the status of any open orders.
If a StopMarketOrder fills during a broker disconnect, after reconnecting, LEAN should update the order status to 'Filled'. The
OrderTicket.Status
does not directly query the broker each time it is accessed, but it should reflect the most recent information received from the broker.However, it's important to note that the exact behavior can depend on the specific brokerage and how they handle disconnects. Some brokerages might not provide complete information immediately after a reconnect, which could potentially lead to temporary discrepancies.
Here's a Python code snippet showing how you might check an order's status:
I'm glad you found the information about handling
OrderResponseErrorCode.InvalidNewOrderStatus
useful! It's always a good practice to check the status of an order before attempting to modify it.Remember, the key to handling broker disconnects is to write robust code that can handle unexpected situations. Always check the status of orders and other important variables, and include error handling where appropriate.
Let me know if you have any other questions!
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.
Lee Allen
Fantastic, thank you!
Lee Allen
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!