Hi I encountered a live trading error but could not find the reason why..
I am having a scheduled code which runs every 2 minutes. Occasionally there will be a dictionary changed size runtime error. I checked inside the piece of code and is very sure there isn't such a case.
2021-07-15 14:52:02 LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: Every 2 min. The error was RuntimeError : dictionary changed size during iteration
2021-07-15 14:52:03 Runtime Error: LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: Every 2 min. The error was RuntimeError : dictionary changed size during iteration
2021-07-15 14:52:03 Runtime Error: LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: Every 2 min. The error was RuntimeError : dictionary changed size during iteration: StackTrace: LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: Every 2 min. The error was RuntimeError : dictionary changed size during iteration
I am suspecting that there are other scheduled codes which might have been changing the dictionary size while the piece of scheduled code is running. (I have a few scheduled tasks running at different time)
Is there anyway to get the line number or print out the dictionary that was affected? Or has anyone met into similar issues before. It happen intermittently and not during backtest. so likely some events triggered it.
Varad Kabade
Hi Pranava,
Would you please attach the code snippet where there is the above error for further assistance?
Best,
Varad Kabade
Pranava
Hi Varad,
this is the code snippet from the function which is scheduled to run every 2min
not sure if setting MarketOrder to be asynchronous helps. now is not async.
OnOrderEvent does this
handleSymbolAfterSellingPosition does this:
Pranava
it might not be caused by 2 scheduled events running at the same time cos I did some debugging and when the time the error occured, a position was being closed, and theres no other scheduled event running at that time.
this is the log trace
Varad Kabade
Hi Pranava,
When running an algorithm live, multiple orders are filled simultaneously instead of queueing the OnOrder event; they are run parallelly. Therefore in the above snippet, the following dictionary is accessed simultaneously.
in handleSymbolAfterSellingPosition
Inside the
To resolve this, we recommend creating a deep copy of the dictionary using "deepcopy" method from the copy library. Refer to the following code snippet:
Best,
Varad kabade
Pranava
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!