Back

How to access previous SMA data in the MultipleSymbolConsolidationAlgorithm.py algorithm.

I am trying to access the previous period SMA indicator values. As an example, I want to get the value of the SMA(10 period) 5 bars in the past. What is the correct syntax for accessing the previous SMA indicator values?

I can access the bar data with 'self.Data[symbol].Bars[0].Low', where the '0' of 'Bars[0]' is the most recent and '1' is the previous bar data etc.
Low is the low of the bar.  I can also access the Time, High, Open and Close for each bar.
    
I have not been able to determine how to access the previous data for the SMA indicator. I can access the current value with 'self.Data[symbol].SMA.Current.Value' but I am not sure of how to access the previous data.  I have tried 'self.Data[symbol].SMA[1]' and I get 'unindexable object' error.
    
My question is what do I need to do to access the previous value of the SMA indicator?

I have attached the backtest of the algorithm.
You can see the self.Debug() statement at the end of the OnData(self,data) section.

I appreciate the help.

Update Backtest








0

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.


All data in quantconnect are for one single time, so to resolve your issue, you can either create an empty list and update it with your data or use RollingWindow.

0

Hi Hal,

As pointed out by Milad above, we can utilize a RollingWindow to track historical indicator values. In this example, we just need to adjust the SymbolData class to include a RollingWindow

def __init__(self, symbol, barPeriod, windowSize):
...
sma_lookback = 5
self.HistoricalSMA = RollingWindow[float](sma_lookback)

Then, when a new consolidated bar is passed to the algorithm, we add the latest SMA value onto the RollingWindow.

def OnDataConsolidated(self, sender, bar):

self.Data[bar.Symbol.Value].SMA.Update(bar.Time, bar.Close)
if self.Data[bar.Symbol.Value].SMA.IsReady:
sma = self.Data[bar.Symbol.Value].SMA.Current.Value
self.Data[bar.Symbol.Value].HistoricalSMA.Add(sma)

Accessing historical values in the RollingWindow is then as easy as
latestSMA = self.Data[bar.Symbol.Value].HistoricalSMA[0]
previousSMA = self.Data[bar.Symbol.Value].HistoricalSMA[1]

See the attached backtest for the full solution. Also, consider reviewing our documentation on RollingWindows.

Best,
Derek Melchin

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed