Hey guys,
Some could telling me why am I getting this error?
Runtime Error: AttributeError : 'IndicatorDataPoint' object has no attribute 'UpperBand'
at BolbandUpdated_30 in main.py:line 67
AttributeError : 'IndicatorDataPoint' object has no attribute 'UpperBand' (Open Stacktrace)
Thank you
I did not attached any backtest because it does not found backtest in this project but I'm sure I have back tested this algorithm
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
import decimal as dclass RollingWindowAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2018, 6, 1) #Set Start Date
self.SetEndDate(2019, 3, 22) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
self.SetTimeZone("Europe/Rome")
self.SetWarmUp(100)
self.symbols = ["EURUSD","EURAUD","GBPUSD","AUDUSD"]
self.forex = self.AddForex(self.symbols[0], Resolution.Minute, Market.FXCM)
consolidator_30 = QuoteBarConsolidator(30)
consolidator_30.DataConsolidated += self.On30Data
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_30)
self.window_30 = RollingWindow[QuoteBar](2) consolidator_60 = QuoteBarConsolidator(60)
consolidator_60.DataConsolidated += self.On60Data
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_60)
self.window_60 = RollingWindow[QuoteBar](2)
#consolidator_240 = QuoteBarConsolidator(240)
#self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_240)
#b240 = self.RegisterIndicator(self.symbols[0], self.Bolband, consolidator_240)
self.Bolband_30 = self.BB(self.symbols[0], 20, 2, MovingAverageType.Simple, Resolution.Minute)
self.Bolband_30.Updated += self.BolbandUpdated_30
self.RegisterIndicator(self.symbols[0], self.Bolband_30, consolidator_30)
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_30)
self.BolbandWindow_30 = RollingWindow[IndicatorDataPoint](3)
self.Bolband_60 = self.BB(self.symbols[0], 20, 2, MovingAverageType.Simple, Resolution.Minute)
self.Bolband_60.Updated += self.BolbandUpdated_60
self.RegisterIndicator(self.symbols[0], self.Bolband_60, consolidator_60)
self.SubscriptionManager.AddConsolidator(self.symbols[0], consolidator_60)
self.BolbandWindow_60 = RollingWindow[IndicatorDataPoint](3)
#self.AddEquity("SPY", Resolution.Minute)
#consolidator = TradeBarConsolidator(30)
#self._sma = SimpleMovingAverage(10)
#self.RegisterIndicator("SPY", self._sma, consolidator)
#self.SubscriptionManager.AddConsolidator("SPY", consolidator)
def BolbandUpdated_30(self, sender, updated):
self.BolbandWindow_30.Add(updated)
UpperBand_30 = self.BolbandWindow_30[0].UpperBand.Current.Value
LowerBand_30 = self.BolbandWindow_30[0].LowerBand.Current.Value
def BolbandUpdated_60(self, sender, updated):
self.BolbandWindow_60.Add(updated)
UpperBand_60 = self.BolbandWindow_60[0].UpperBand.Current.Value
LowerBand_60 = self.BolbandWindow_60[0].LowerBand.Current.Value
def On30Data(self, sender, bar):
self.window_30.Add(bar)
close_price_30 = self.window_30[0].Close
def On60Data(self, sender, updated):
self.window_60.Add(bar)
close_price_60 = self.window_60[0].Close
def OnData(self, data):
#fxOpen = data['EURUSD'].Open
#fxClose = data['EURUSD'].Close
#self.window.Add(data["EURUSD"])
if not (self.window_30.IsReady and self.window_60.IsReady):
return
#holdings = self.Portfolio["EURUSD"].Quantity
#previousPrice = self.window[1].Close
#stop_price_ = self.Securities['EURUSD'].Price * 0.50
#stop_price = self.Securities['EURUSD'].Price * 1.20
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket:
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log(str(orderEvent))
Halldor Andersen
Hi Patrik.
The rolling window does not have the attributes you are referencing to in your code.First, you need to create a QuoteBarConsolidator(30):
# In Inititalize consThirtyMin = QuoteBarConsolidator(30) consThirtyMin.DataConsolidated += self.OnDataConsolidated
Then, use SubscriptionManager.AddConsolidator to attach the consolidator to the security:
# In Inititalize self.SubscriptionManager.AddConsolidator(self.fxPair, consThirtyMin)
Also, construct the Bollinger Bands indicator and a rolling window:
# In Inititalize self.bb30 = BollingerBands("30minBB", 20,2,MovingAverageType.Simple) # Inititalize rolling window which stores to store upper bollinger band indicator values self.bbUpperRollingWindow = RollingWindow[float](3)
In the method OnDataConsolidated(), update the indicator and add the indicator value to the rolling window:
## In OnDataConsolidated # Update bb30 indicator self.bb30.Update(bar.EndTime, bar.Close) # Add to rolling window self.bbUpperRollingWindow.Add(self.bb30.Current.Value)
In OnData() you can implement your trading strategy using the values in the rolling window. I've attached a backtest where I demonstrate the above. Check out these documentation sections on Rolling Windows and Consolidating Data for further information.
Patrik Pavan
Thank you for the reply
It gives me another mistake again. The error is --> Runtime Error: AttributeError : 'CVTap' object has no attribute 'bbUpperrollingWindow' at OnData in main.py:line 68 AttributeError : 'CVTap' object has no attribute 'bbUpperrollingWindow' (Open Stacktrace) I simply added another bollinger band and a 60 min consolidator. I think the error is in the lines 63 and 64, can you have a look at my code?Furthermore I have another problem, when I try to attach a backtest, it tells me that there is no backtest done, but it is not true. from System import * from QuantConnect import * from QuantConnect.Data import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * from QuantConnect.Data.Consolidators import * from datetime import datetime import decimal as d import numpy as np class CVTap(QCAlgorithm): def Initialize(self): self.SetStartDate(2017,11,1) #Set Start Date self.SetEndDate(2017,11,14) #Set End Date self.SetCash(10000) #Set Strategy Cash self.fxPair = "EURUSD" # Add EURUSD self.AddForex(self.fxPair, Resolution.Minute) # Create consolidator for 30 minutes consThirtyMin = QuoteBarConsolidator(30) consThirtyMin.DataConsolidated += self.OnDataConsolidated cons60Min = QuoteBarConsolidator(60) cons60Min.DataConsolidated += self.OnDataConsolidated self.SubscriptionManager.AddConsolidator(self.fxPair, consThirtyMin) self.SubscriptionManager.AddConsolidator(self.fxPair, cons60Min) self.bb30 = BollingerBands("30minBB", 20,2,MovingAverageType.Simple) self.bb60 = BollingerBands("60minBB", 20,2,MovingAverageType.Simple) # Inititalize rolling window which stores to store upper bollinger band indicator values self.bbUpperRollingWindow = RollingWindow[float](3) self.bbUpperRollingWindow_60 = RollingWindow[float](3) def OnDataConsolidated(self, sender, bar): # Update bb30 indicator self.bb30.Update(bar.EndTime, bar.Close) self.bb60.Update(bar.EndTime, bar.Close) # Add to rolling window self.bbUpperRollingWindow.Add(self.bb30.Current.Value) self.bbUpperRollingWindow_60.Add(self.bb60.Current.Value) def OnData(self, data): # Return if rolling window is not ready if not self.bbUpperRollingWindow.IsReady and self.bbUpperRollingWindow_60: return self.Log("upper rolling window : " + str(self.bbUpperRollingWindow[0])) bb30cv = self.bbUpperrollingWindow[0] bb60cv = self.bbUpperRollingWindow_60[0] cv_30 = consThirtyMin[0].Current.Value cv_60 = cons60Min[0].Current.Value holdings = self.Portfolio[self.fxPair].Quantity stop_price_ = self.Securities[self.fxPair].Price * 0.97 stop_price = self.Securities[self.fxPair].Price * 1.05 if not self.Portfolio[self.fxPair].Invested: if cv_30 > self.bb30cv: return def OnOrderEvent(self, orderEvent): order = self.Transactions.GetOrderById(orderEvent.OrderId) if order.Status == OrderStatus.Filled: if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket: self.Transactions.CancelOpenOrders(order.Symbol) if order.Status == OrderStatus.Canceled: self.Log(str(orderEvent))Douglas Stridsberg
You're trying to access bbUpperrollingWindow when the name of the variable is self.bbUpperRollingWindow.
Link Liang
Hi Patrik,
Credit to @Douglas, in line “bb30cv = self.bbUpperrollingWindow[0]” you have a typo with lower case ‘r’ which caused AttributeError.
In addition, I see you are trying to access some local variable in another method. For example, you tried to access consThirtyMin in Ondata(), which is defined locally in Initialize(). Moreover, bb30cv is defined locally in Ondata() so you won’t need to add “self.” in line “if cv_30 > self.bb30cv:“.
Regarding the use of data consolidator, I suggest to use different data handler for each consolidator. Otherwise there could be some unexpected behavior. In the backtest I’ve made ThirtyMinHandler and SixtyMinHandler for each data consolidator. Since you use minute level resolution, Ondata() is triggered every minute, ThirtyMinHandler() is triggered every 30 min and SixtyMinHandler() is triggered every 60 min. You could do whatever you want in there.
Hope it helps!
Patrik Pavan
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!