Generate code in 3 minutes
Enter your idea and AI generates runnable code.
Based on the QuantConnect LEAN documentation, the discrepancy between backtesting and live trading regarding missing data is caused by how the engine handles processing latency relative to the data resolution.
In backtesting, the LEAN engine controls the flow of time. It operates synchronously.
OnData method takes a long time to process a specific data slice, the engine simply "pauses" the simulation clock.In live trading, the engine cannot pause the market. Time moves forward regardless of your algorithm's state.
OnData method finally finishes execution, the engine delivers the most recent slice available at that exact moment, skipping the intermediate slices that occurred while the algorithm was busy.The documentation provides a specific example regarding high-frequency data:
OnData logic takes 3.5 seconds to compute.To resolve this in live trading, you must ensure your OnData execution time is significantly lower than your data resolution.
OnData.Train methods or Scheduled Events that run less frequently.Resolution.Second to Resolution.Minute.Q: Does LEAN queue data in live trading if my algorithm is slow?
A: No. To prevent trading on stale data, LEAN skips intermediate slices and delivers only the most recent data once the algorithm becomes available again.
Q: How can I perform heavy computations without missing market data?
A: You should perform intensive computations (like model training) in the Train method or a separate Scheduled Event. These run in a separate thread (in live mode) or separate logic flow, preventing the blocking of the main OnData loop.
Q: Will OnData fire if there is no new data?
A: Yes, if "fill-forward" is enabled (which is the default behavior). The engine will generate data points based on the last known price to ensure the algorithm continues to tick, though this does not solve the issue of missing real data due to latency.