Hi all,
As part of a test strategy, I am trying to use to On Balance Volume to confirm a breakout using a rolling window to check if the previous days OBV is either greater or lower than the current OBV. This results in:
Runtime Error: TypeError : Cannot get managed object at Update sd.obvWindow[1] < sd.obv.Current.Value: === at Python.Runtime.PyObject.Invoke(PyTuple args in Alpha6.py:line 31 TypeError : Cannot get managed objec
Am I using this indicator correctly? I don't understand why it cannot retrieve the value when I've implemented it in the same way as my other indicators. Code it below
Thanks!
class Donchian6(AlphaModel):
def __init__(self, lowerBand = 55, upperBand = 55, emaPeriod = 100, resolution = Resolution.Daily):
self.lowerBand = lowerBand
self.upperBand = upperBand
self.resolution = resolution
self.emaPeriod = emaPeriod
self.insightPeriod = Time.Multiply(Extensions.ToTimeSpan(resolution), upperBand)
self.symbolData = {}
resolutionString = Extensions.GetEnumString(resolution, Resolution)
self.Name = '{}({},{},{})'.format(self.__class__.__name__, lowerBand, upperBand, resolutionString)
def Update(self, algorithm, data):
insights = []
for key, sd in self.symbolData.items():
if sd.donchian.IsReady and \
sd.donchianWindow.IsReady and \
sd._donchian["UpperBand"].IsReady and \
sd._donchian["LowerBand"].IsReady and \
sd.obv.IsReady and\
sd.obvWindow.IsReady:
if sd._donchian["UpperBand"][1] < sd.Security.Close and \
sd.Security.Close > sd.ema.Current.Value and \
sd.obvWindow[1] < sd.obv.Current.Value:
insights.append(Insight.Price(sd.Security.Symbol, self.insightPeriod, InsightDirection.Up))
if sd._donchian["LowerBand"][1] > sd.Security.Close and \
sd.Security.Close < sd.ema.Current.Value and \
sd.obvWindow[1] > sd.obv.Current.Value:
insights.append(Insight.Price(sd.Security.Symbol, self.insightPeriod, InsightDirection.Down))
return insights
def OnSecuritiesChanged(self, algorithm, changes):
for added in changes.AddedSecurities:
self.symbolData[added.Symbol] = SymbolData(algorithm, added, self.upperBand, self.lowerBand, self.emaPeriod, self.resolution)
for removed in changes.RemovedSecurities:
data = self.symbolData.pop(removed.Symbol, None)
if data is not None:
algorithm.SubscriptionManager.RemoveConsolidator(removed.Symbol, data.Consolidator)
class SymbolData:
def __init__(self, algorithm, security, lowerBand, upperBand, emaPeriod, resolution):
self.Security = security
self.donchian = DonchianChannel(upperBand, lowerBand)
self._donchian = {}
self.Consolidator = algorithm.ResolveConsolidator(security.Symbol, resolution)
algorithm.RegisterIndicator(security.Symbol, self.donchian, self.Consolidator)
self.donchian.Updated += self.DonchianUpdated
self.donchianWindow = RollingWindow[IndicatorDataPoint](2)
self._donchian["UpperBand"] = RollingWindow[float](2)
self._donchian["LowerBand"] = RollingWindow[float](2)
self.ema = ExponentialMovingAverage(emaPeriod)
self.ConsolidatorEMA = algorithm.ResolveConsolidator(security.Symbol, resolution)
algorithm.RegisterIndicator(security.Symbol, self.ema, self.ConsolidatorEMA)
self.obv = OnBalanceVolume()
self.ConsolidatorOBV = algorithm.ResolveConsolidator(security.Symbol, resolution)
algorithm.RegisterIndicator(security.Symbol, self.obv, self.ConsolidatorOBV)
self.obv.Updated += self.OBVUpdated
self.obvWindow = RollingWindow[IndicatorDataPoint](2)
def DonchianUpdated(self, sender, updated):
self.donchianWindow.Add(updated)
self._donchian["UpperBand"].Add(self.donchian.UpperBand.Current.Value)
self._donchian["LowerBand"].Add(self.donchian.LowerBand.Current.Value)
def OBVUpdated(self, sender, updated):
self.obvWindow.Add(updated)
Alexandre Catarino
Hi Nicholas Fitzgerald ,
Could you please apply the following changes?
The TypeError occurs because the algorithm is comparing an IndicatorDataPoint object (sd.obvWindow[1]) with a floating-point number (sd.obv.Current.Value)
Best regards,
Alex
P.S.: Please share the full backtest instead of code snippets. It helps the Community to help you. If the algorithm doesn't create a backtest because of the error, you can comment out the code that generates the bug and tell the Community the changes that need to be made to reproduce the bug.
Nicholas Fitzgerald
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!