Suppose we have a ScheduledEvent that occurs hourly, and minute-level data feed with consolidation into hourly bars.
The default behavior for something like the following would use stale prices from the previous hour:
class MyAlgo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1, 5)
self.SetEndDate(2015, 1, 6)
self.SetCash(1)
# Subscribe to minute data
self.symbols = ['EURUSD', 'GBPUSD', 'USDJPY']
for symbol in self.symbols: self.AddForex(symbol, Resolution.Minute, Market.Oanda)
# Rolling Window of 4 Hourly bars for each symbol
self.symbolConsolidatedData = { symbol : RollingWindow[QuoteBar](4) for symbol in self.symbols }
for symbol in self.symbols:
consolidator = QuoteBarConsolidator(timedelta(hours=1))
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator(self.Symbol(symbol), consolidator)
# Hourly Scheduled Event
self.Schedule.On(self.DateRules.EveryDay('EURUSD'),
self.TimeRules.Every(timedelta(hours=1)),
self.CheckDataWindows)
def OnData(self, data):
pass
def OnDataConsolidated(self, sender, bar):
self.symbolConsolidatedData[bar.Symbol.Value].Add(bar)
def CheckDataWindows(self):
# Number of bars in each window
count = { symbol : window.Count for symbol, window in self.symbolConsolidatedData.items() }
self.Log(count)
# Most recent time
if all( [c >= 1 for c in count.values()] ):
time = { symbol: window[0].EndTime.isoformat() for symbol, window in self.symbolConsolidatedData.items() }
self.Log(f'Most Recent Data: {time}')
self.Log(f'Algorithm Time: {self.Time}')
return
Logs:
2015-01-05 01:00:00 : {'EURUSD': 1, 'GBPUSD': 1, 'USDJPY': 1}
2015-01-05 01:00:00 : Most Recent Data: {'EURUSD': '2015-01-05T00:00:00', 'GBPUSD': '2015-01-05T00:00:00', 'USDJPY': '2015-01-05T00:00:00'}
2015-01-05 01:00:00 : Algorithm Time: 2015-01-05 01:00:00
2015-01-05 02:00:00 : {'EURUSD': 2, 'GBPUSD': 2, 'USDJPY': 2}
2015-01-05 02:00:00 : Most Recent Data: {'EURUSD': '2015-01-05T01:00:00', 'GBPUSD': '2015-01-05T01:00:00', 'USDJPY': '2015-01-05T01:00:00'}
2015-01-05 02:00:00 : Algorithm Time: 2015-01-05 02:00:00
2015-01-05 03:00:00 : {'EURUSD': 3, 'GBPUSD': 3, 'USDJPY': 3}
2015-01-05 03:00:00 : Most Recent Data: {'EURUSD': '2015-01-05T02:00:00', 'GBPUSD': '2015-01-05T02:00:00', 'USDJPY': '2015-01-05T02:00:00'}
2015-01-05 03:00:00 : Algorithm Time: 2015-01-05 03:00:00
For instance, at time `2015-01-05 01:00:00`, the goal is to use a ScheduledEvent to run some analysis on the hourly bar that closes at time `2015-01-05 01:00:00`, and generate predictions for `2015-01-05 02:00:00`. However because the ScheduledEvent occurs at the same time the hourly bar consolidates, it is using the (stale) previous hourly bar closing at time `2015-01-05 00:00:00` instead.
Any suggestions on “delaying” the ScheduledEvent until the hourly bar is ready? E.g. Hourly Bar Consolidates at time `2015-01-05 01:00:00`, Scheduled Event is called at time `2015-01-05 01:00:01`.
Adam W
Just saw there's an open issue here:
Adam W
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!