I am a seasoned programmer and I understand the general concepts of asynchronous programming. I have two questions about how order status updating in QC work.
First question. I know that the handler OnOrderEvent will be called whenever any order status has been updated. The documentation stated that it can be called asynchronously. I think it means OnOrderEvent is run on another thread other than the one running the OnData handler. Is that correct?
Second question. Will any order status be updated asynchronously by other thread while the code in OnData is running? Is it possible that the situation shown in below code happen?
public override void OnData(Slice sl)
{
// Assume we have already placed a limit order on symbol,
// it is submitted but not filled by this point.
var isInvested1 = Securities[symbol].Invested; // isInvested1 = false
var openOrderCount1 = Transactions.GetOpenOrders(symbol).Count; // openOrderCount1 = 1
// The order has just been filled and the order status has been updated
// in another thread by this point.
var isInvested2 = Securities[symbol].Invested; // isInvested2 = true
var openOrderCount2 = Transactions.GetOpenOrders(symbol).Count; // openOrderCount2 = 0
}
Shile Wen
Hi Wilson,
OnOrderEvent is run on a separate thread if the Market Order is specified to be asynchronous. As for the second question, if the order is not a synchronized Market Order, then the situation you described is possible.
Best,
Shile Wen
Wilson Tang
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!