hi everyone,

I'm having a hard time to get the simple moving average of the volume for 15-min bars to match up to what's shown on other platform like TradingView. Any help is much appreciated!

 

from AlgorithmImports import *

class VolumeSMAAlgorithm(QCAlgorithm):
   def Initialize(self):
       self.SetStartDate(2022, 1, 1)   # Set Start Date
       self.SetEndDate(2022, 12, 31)   # Set End Date
       self.SetCash(100000)            # Set Strategy Cash
       self.symbol = self.AddEquity("SPY", Resolution.Minute).Symbol
       
       # Create a 15-minute consolidator
       self.consolidator = TradeBarConsolidator(timedelta(minutes=15))
       self.consolidator.DataConsolidated += self.OnDataConsolidated
       
       # Register the consolidator
       self.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)
       
       # Rolling window to store the last 100 15-minute bars
       self.history = RollingWindow[TradeBar](100) 
       
       # Simple Moving Average for volume with window of 20
       self.volumeSMA = SimpleMovingAverage(20)

   def OnDataConsolidated(self, sender, bar):
       # Add the consolidated bar to the rolling window
       self.history.Add(bar)
       
       # Check if the rolling window is ready
       if self.history.IsReady:
           # Calculate the total volume of the last 20 bars
           volumeSum = sum([tradeBar.Volume for tradeBar in self.history])
           self.volumeSMA.Update(bar.EndTime, volumeSum)
           
           # Log the current SMA value
           self.Log(f"Current Volume SMA: {self.volumeSMA.Current.Value}")