Hi all, we have a production trading system we're thinking about porting to Lean from another platform and I have a few questions about threading -- I hope this is the right place for it.
1. Does Lean serialize calls to my OnData() method? In other words, if my method ran forever would I get another call to OnData (at the next slice) on a different thread, or would I just get no further calls to OnData()?
2. Assuming the answer to #1 is yes, if I start background tasks from OnData() on a other threads, can those threads initiate orders independently without causing threading issues in Lean?
3. Similarly, are OnOrderEvent() serialized? Or, if many orders were outstanding, would I expect to get overlapping OnOrderEvent() calls on different threads?
4. Is there any scenario involving threads that I should be careful to avoid?
Thanks so much for all the work on what looks like a great system --
David
David Gilmour
To answer my own question, there is a good demo file in the Lean repo that at least addresses synchronization of order events. Take a look at OrderTicketDemoAlgorithm.cs.
Jared Broad
Sorry for the delay David; that is a good demo to look at. To answer your questions, to the best of my knowledge:
1. Does Lean serialize calls to my OnData() method? Yes
2. I start background tasks from OnData() on other threads, can those threads initiate orders independently without causing threading issues in Lean? Probably OK.
3. Similarly, are OnOrderEvent() serialized? Yes. You should get one at a time.
4. Is there any scenario involving threads that I should be careful to avoid? Avoid placing orders in the OnOrderEvent handler to avoid infinite loops of submitting and getting the order submitted event.
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.
David Gilmour
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!