I recently had issues in Live trading a project, that worked perfectly in backtesting.
Lets assume you schedule trading 5 minutes after market opening on 1-minute resolution. In theory it could be the case that within 5 minutes, no bar arrives or that bar 1,2,3,4 has data but bar 5 has not.
In addition I learned from the QC support that in high volatility phase, time slices might arrive milliseconds later, so the scheduler does not catch it.
While in backtest, it always works, in LiveMode orders were skipped.
The support send me a fix which i am sharing here:
def Initialize(self):
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 5),self.trade)
self.weight_by_sec = {"SPY":0.5,"TLT":0.5}
#----------works in backtest, does not work in LiveMode---------
def trade(self):
for sec, weight in self.weight_by_sec.items():
# Check that we have data in the algorithm to process a trade
if not self.CurrentSlice.ContainsKey(sec) or self.CurrentSlice[sec] is None:
self.Error(f"[trade],{sec},missing_data")
continue
qty=self.CalculateOrderQuantity(sec,weight)
entry_order = self.MarketOrder(sec, qty, tag=str(sec),asynchronous=False)
#----------works in backtest and LiveMode---------
def trade(self):
weight_by_sec = {"SPY":0.5,"TLT":0.5}
for sec, weight in self.weight_by_sec.items():
# Check that we have data in the algorithm to process a trade
bar = self.Securities[sec].GetLastData()
if bar.EndTime.date() < self.Time.date():
self.Error(f"[trade],{sec},data_not_arrived_yet")
continue
qty=self.CalculateOrderQuantity(sec,weight)
entry_order = self.MarketOrder(sec, qty, tag=str(sec),asynchronous=False)
Alexandre Catarino
Thank you for the post, Santa24 .
Hi Community,
Please refer to Writing Algorithm > Scheduled Events > Live Trading Considerations for a detailed explanation of this difference.
Yuri Lopukhov
Quote: In backtesting, Scheduled Events are part of the main algorithm manager loop, so they may not execute exactly when you set them. For example, if your algorithm subscribes to minute resolution US Equity data with regular trading hours and you set a Scheduled Event to occur at 2:00 AM, your Scheduled Event will execute at 9:31 AM when the next bar is fed into your algorithm.
This doesn't seem right to me, it may be quite surprising and affect results significantly. Are you considering changing this to closer match execution in live mode?
Yuri Lopukhov
Santa24 while the fixed code will not produce errors, it will also not trade if the bar was not yet received.
Like if the bar lags 1 second, it will log error and won't trade today at all. I don't think that's what you want, at least results will be very far from backtesting.
I would use OnData or consolidator handler instead and check if current time is more than 9:35. Of course, make sure it will only work once a day (save current date and compare), like this:
Santa24
Yuri Lopukhov that is a possibility. However, i assumed my code would trade at 9.35 if there was at least one bar between 09:30 and 09:35. If the bar came at 09:33, it should work. However my code would skip it only if the first minute bar came later than 09:35:001 - in this case there might be liquidity issues
Rafael Trevisan
Hi Santa24, I had a very similar issue and in the end I was very frustrated when I realized the results in backtest wouldn't be even close when live trading.
In my case, when using the GetLastData method in backtest I was getting filled with stale prices, which obviously produced a significantly different result.
In short, make sure you check you don't have any stale price warning in your backtests.
Fred Painchaud
Alexandre Catarino
Just read the details in the docs. Is the clock used by the algo in live mode in-sync with the market clock? It's certainly also a consideration I believe that might be added. A scheduled event at 10:00:00.00000000 AM in the algo is not necessarily 10:00:00.00000000 AM in the markets' infra.
@All
In most backtest simulations, time like humans have defined does not exist. Time is timestamped and it flows at the pace of the data flow. If you can process 1000 1s data points per second for one asset, say, then 1s wall clock is 1000s backtest time. But then, in live trading, processing 1000 1s data points per second for one asset is not possible, or else, you are basically predicting the next 1000 1s data points for that asset on that market. You're gonna get rich, exponentially fast.
Fred
Santa24
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!