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:

  1. def Initialize(self):
  2. self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 5),self.trade)
  3. self.weight_by_sec = {"SPY":0.5,"TLT":0.5}
  4. #----------works in backtest, does not work in LiveMode---------
  5. def trade(self):
  6. for sec, weight in self.weight_by_sec.items():
  7. # Check that we have data in the algorithm to process a trade
  8. if not self.CurrentSlice.ContainsKey(sec) or self.CurrentSlice[sec] is None:
  9. self.Error(f"[trade],{sec},missing_data")
  10. continue
  11. qty=self.CalculateOrderQuantity(sec,weight)
  12. entry_order = self.MarketOrder(sec, qty, tag=str(sec),asynchronous=False)
  13. #----------works in backtest and LiveMode---------
  14. def trade(self):
  15. weight_by_sec = {"SPY":0.5,"TLT":0.5}
  16. for sec, weight in self.weight_by_sec.items():
  17. # Check that we have data in the algorithm to process a trade
  18. bar = self.Securities[sec].GetLastData()
  19. if bar.EndTime.date() < self.Time.date():
  20. self.Error(f"[trade],{sec},data_not_arrived_yet")
  21. continue
  22. qty=self.CalculateOrderQuantity(sec,weight)
  23. entry_order = self.MarketOrder(sec, qty, tag=str(sec),asynchronous=False)
+ Expand

Author

Santa24

July 2022