Back

Adding an Indicator Registered for Consolidated QuoteBar to RollingWindow

I have a strategy which trades on the Minute resolution, with two consolidators - 15m and 4Hr. I used a number of indicators, one of which is the MACD, which is instantiated twice - once each for 15m and 4hr.

To obtain the previous MACD value for the 4Hr timeframe, I use a RollingWindow. However, when I tried to access a specific window data, I encounter a runtime error - 

 

Runtime Error: TypeError : Cannot get managed object
at OnData in main.py:line 105
TypeError : Cannot get managed object (Open Stacktrace)

Have I missed anything ? Here is my source code:

def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

self.symbols = ["EURUSD","EURAUD","GBPUSD","AUDUSD"]
self.SetStartDate(2016, 1, 1) #Set Start Date
self.SetEndDate(2018, 6, 1) #Set End Date
self.SetCash(25000) #Set Strategy Cash

self.forex = self.AddForex(self.symbols[0], Resolution.Minute, Market.FXCM)
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)

self.SetWarmup(100)

# Trade 15m and 240m timeframes
consolidator_15 = QuoteBarConsolidator(15)
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_15)
consolidator_240 = QuoteBarConsolidator(240)
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_240)

# Initiatialize indicators


self._macd = MovingAverageConvergenceDivergence(self.symbols[0], 12, 26, 9, MovingAverageType.Exponential)
self._macdTrend = MovingAverageConvergenceDivergence(self.symbols[0], 12, 26, 9, MovingAverageType.Exponential)


self.RegisterIndicator(self.symbols[0], self._macd, consolidator_15)
self.RegisterIndicator(self.symbols[0], self._macdTrend, consolidator_240)

self._macdTrend.Updated += self.MacdUpdated
self._macdWindow = RollingWindow[IndicatorDataPoint](3)

self.__previous = datetime.min


def OnData(self, data):

if self.IsWarmingUp: return



# only once per day
if self.__previous.date() == self.Time.date(): return

# wait for our macd to fully initialize
if not ( self._macd.IsReady and self._macdTrend.IsReady) : return


self._macdTrend.Previous = self._macdWindow[1]

# Problem code here on self._macdTrend.Previous

trendMACDUp = self._macdTrend > 0 and (self._macd.Current.Value > self._macdTrend.Previous)


self.__previous = self.Time

def MacdUpdated(self, sender, updated):
self._macdWindow.Add(updated)

Any help is very much appreciated. Thanks.

Update Backtest







Thanks for the source code it made helping much easier. In the code above there was a type error since self._macd.Current.Value and self._macdTrend.Previous are two separate types. When trying to determine if self._macd.Current.Value > self._macdTrend.Previous we needed to make sure that they are the same type (Decimal.decimal in this case). 

When dealing with type errors like this it is recommended to use the self.Debug() call to confirm data types. self.Debug() prints out information into the web console while the algorith, is running. In the backtest below there are self.Debug() statements to prove the following conditional statements will work. 

https://www.quantconnect.com/docs/algorithm-reference/logging-and-debug#Logging-and-Debug-Debug-Messages
0


Many thanks for your help, Guru. I finally managed to get out of this "forest". But I have another question:

What I intend to do is to compare the previous bar with the current bar, assuming both bars are already _closed_. So if I use the code as above, am I doing it right, or should I be comparing _macdWindow[1] and _macdWindow[2] ? What I don't want to do is to compare a value that is still moving within the 4 hour period...

0

Yes, the values in the indicator windows are added after a period is closed. Comparing _macdWindow[0] and _macdWindow[1] will suffice for what you are trying to do.

0

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