I am trying to implement a Rolling Window within the SymbolData class for an ensemble of SMA's.
Essentially each security will have a RollingWindow dict to store the results of the various lookback windows - and it is using a Dict with a RollingWindow that is causing my troubles. The error I am receiving is…
During the algorithm initialization, the following exception has occurred: AttributeError : 'dict' object has no attribute 'Add'
at SmaUpdated
self.smaWindow.Add(updated)
File "main.py" in main.py:line 43
AttributeError : 'dict' object has no attribute 'Add'
And the class is below. Any help would be greatly appreciated!
class SymbolData:
def __init__(self, symbol, algorithm):
self.algorithm = algorithm
self.Symbol = symbol
self.sma = {}
self.smaWindow = {}
# Build an "Ensemble" of Moving averages
self.sma = algorithm.SMA(self.Symbol, 9*21, Resolution.Daily)
self.smaWindow = RollingWindow[IndicatorDataPoint](2)
self.sma.Updated += self.SmaUpdated
def SmaUpdated(self, sender, updated):
'''Event holder to update the fast SMA Rolling Window values'''
self.smaWindow.Add(updated)
Louis Szeto
Hi Mark
I don't quite understand the use of your dictionary, esp you immediately overwritten it
I think you've skipped some codes in the middle so it is impossible to reproduce your problem. But based on your error message and code, I assumed you're doing dict{'key1':rollingWindow1, ‘key2’:rollingWindow2, …}. Then you will have to use various updating method for each individual indicator and their rolling window. The update method should be correspond to the value of dict (rolling window item), not the whole dict itself.
Cheers
Louis
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.
Vladimir
Louis Szeto,
I backtested your latest version of the algorithm with only StartDate and EndDate changes.
Exposure starts at 0.5, as it should, and then increases to 2.01.
What could be the reason for this behavior and how to avoid it?
Vladimir
Louis Szeto,
I found a solution
for change in changes.RemovedSecurities:
if change.Symbol in self.SymbolData:
self.Liquidate()
del self.SymbolData[change.Symbol]
Louis Szeto
Hi Vladimir
Thanks, I forgotten to set exit criteria. You are correct
Cheers
Louis
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.
Mark Reeve
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!