Hey Harsh,
We can use a RollingWindow to store the 2 most recent bars. We can update our rolling window each time a new 30-minute bar is available. This is done easily by using the DataConsolidated event handler for our 30-minute consolidator.
# Define and Register Consolidator
thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=30))
self.SubscriptionManager.AddConsolidator(self.spy, thirtyMinuteConsolidator)
# Set DataConsolidated Event Handler, each time a new bar is available the OnThirtyMinuteBar method will be called
thirtyMinuteConsolidator.DataConsolidated += self.OnThirtyMinuteBar
# Define Rolling Window
self.barWindow = RollingWindow[TradeBar](2)
And in our event handler method, we can add our bar to our rolling window and also access the previous bar.
def OnThirtyMinuteBar(self, sender, bar):
self.barWindow.Add(bar)
if not self.barWindow.IsReady:
return
currentBar = self.barWindow[0]
previousBar = self.barWindow[1]
To plot a time series, we can use the self.Plot(chart_name, series_name, value) method.
self.Plot("My Custom Chart", "High", High)
self.Plot("My Custom Chart", "Low", Low)
Learn more about plotting in the documentation.