Hello,
I am trying to work on seeing if the RSI of a stock has been above 66 within the last 5 periods. If it hasn't then I want to add it to my universe selection. Attached is the python code I am using, but I keep getting an error.
Runtime Error: TypeError : Cannot get managed object
at CoarseSelectionFilter
if self.averages[symbol].rsi_max < 66:
===
at Python.Runtime.Dispatcher.TrueDispatch (System.Object[] args) [0x00098] at :0
at Python.Runtime.Dispatcher.Dispatch (System.Object[] args) [0x00008] at :0
at __System_Func`2\[\[System_Collections_Generic_IEnumerable`1\[\[QuantConnect_Data_UniverseSelection_CoarseFundamental\ in main.py:line 71
TypeError : Cannot get managed object
What am I doing wrong here?
Kevin Dwyer
class CrawlingRedDog(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 10, 8) self.SetEndDate(2020, 12, 31) self.SetCash(100000) # Set Strategy Cash self.AddUniverse(self.CoarseSelectionFilter) self.UniverseSettings.Resolution = Resolution.Daily self.averages = {} self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage) def CoarseSelectionFilter(self, coarse): selected = [] coarse = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True) coarse = [c for c in coarse if c.Price > 10][:100] for coar in coarse: symbol = coar.Symbol if symbol not in self.averages: history = self.History(symbol, 200, Resolution.Daily) self.averages[symbol] = SelectionData(history) self.averages[symbol].update(self.Time, coar.AdjustedPrice) if self.averages[symbol].is_ready(): if self.averages[symbol].rsi_max < 66: selected.append(symbol) return selected def OnSecuritiesChanged(self, changes): self.changes = changes for security in self.changes.RemovedSecurities: if security.Invested: self.Liquidate(security.Symbol) for security in self.changes.AddedSecurities: self.SetHoldings(security.Symbol, .10) class SelectionData(): def __init__(self, history): self.rsi = RelativeStrengthIndex(20, Resolution.Daily) self.rsi_max = IndicatorExtensions.MAX(self.rsi, 5) for data in history.itertuples(): time = data.Index[1] close = data.close self.rsi.Update(time, close) def is_ready(self): return self.rsi.IsReady def update (self, time, price): self.rsi.Update(time, price)
I can't attach my project code so I am inserting it here
Derek Melchin
Hi Kevin,
To resolve the issue, we need to replace
if self.averages[symbol].rsi_max < 66
with
if self.averages[symbol].rsi_max.Current.Value < 66
See the attached backtest for reference.
Best,
Derek Melchin
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.
Kevin Dwyer
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!