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}")
Mia Alissi
Hi Ron,
Your code seems to be calculating the Simple Moving Average (SMA) of the total volume of the last 20 bars, not the SMA of the volume of each bar.
To calculate the SMA of the volume for each 15-minute bar, you should update the SMA with the volume of each bar, not the total volume of the last 20 bars. Here's how you can do it:
This way, the SMA will be calculated using the volume of each 15-minute bar, which should match what's shown on other platforms like TradingView.
Please note that the data you get from QuantConnect might be slightly different from what you see on other platforms due to differences in data sources and processing. If you notice any significant discrepancies, you can report them at https://www.quantconnect.com/data/issues/create.
I hope this helps! Let me know if you have any other questions.
Sources:
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.
Ron Le
Thanks Mia, but still wrong, the SMA volume with your code is about 10x more than what's shown on Tradingview
Mia Alissi
Hi Ron,
I see, it seems like there might be a discrepancy between the data you're getting from QuantConnect and the data on TradingView. This could be due to a number of reasons:
Data Source Differences: Different platforms may use different data sources, which can lead to discrepancies in the data. QuantConnect uses its own data, which may be different from the data used by TradingView.
Data Processing Differences: Different platforms may process and adjust the data differently. For example, some platforms adjust the data for splits and dividends, while others do not. This can also lead to discrepancies in the data.
Indicator Calculation Differences: Different platforms may calculate indicators differently. For example, some platforms may use a different type of moving average in their calculations.
If you're confident that the settings and data are identical on both platforms, then it might be worth checking the following:
Indicator Warm-up: In your code, you're setting a warm-up period of 100 bars. This means that the first 100 bars of data are used to warm up the indicator, and the indicator values might not be accurate during this period. If you're comparing the SMA values during the warm-up period, this could explain the discrepancy.
Data Consolidation: You're using a 15-minute consolidator in your code. Make sure that you're comparing the SMA values with the 15-minute data on TradingView.
If you've checked all these factors and the discrepancy still exists, it might be worth reaching out to the QuantConnect community or support for further assistance.
Sources:
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.
Ron Le
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!