Bollinger band top/bot .shift ability
-
Re: BBand Strategy - code runs forever with no output
I am trying to create a signal/trigger for my buy and sell when self.dataclose < self.bband.lines.bot.shift() ------ because I want to trade when the close price is above/below the bollinger top/bot at T-1.
I tried it but I am getting this error below as per screenshot. please advise if there is a way to deal with that. thank you!
Have also showed part of the code showing how I added .shift to my bollinger band strategy. Thanks.
(trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return if self.getposition().size == 0: if self.datahigh > self.bband.top.shift(1): self.sell() elif self.datalow < self.bband.bot.shift(1): self.buy() if (self.getposition().size > 0 and self.dataclose > self.sma) or ( self.getposition().size < 0 and self.dataclose < self.sma): self.close()
AttributeError Traceback (most recent call last) <ipython-input-47-9a474057fbd2> in <module> 123 cerebro.adddata(data) 124 print('run begin') --> 125 optimized_runs = cerebro.run(maxcpus=1) 126 127 print('runs completed: ' + str(len(optimized_runs))) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs) 1125 # let's skip process "spawning" 1126 for iterstrat in iterstrats: -> 1127 runstrat = self.runstrategies(iterstrat) 1128 self.runstrats.append(runstrat) 1129 if self._dooptimize: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata) 1291 self._runonce_old(runstrats) 1292 else: -> 1293 self._runonce(runstrats) 1294 else: 1295 if self.p.oldsync: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in _runonce(self, runstrats) 1693 1694 for strat in runstrats: -> 1695 strat._oncepost(dt0) 1696 if self._event_stop: # stop if requested 1697 return ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\strategy.py in _oncepost(self, dt) 309 self.next() 310 elif minperstatus == 0: --> 311 self.nextstart() # only called for the 1st value 312 else: 313 self.prenext() ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\lineiterator.py in nextstart(self) 345 346 # Called once for 1st full calculation - defaults to regular next --> 347 self.next() 348 349 def next(self): <ipython-input-47-9a474057fbd2> in next(self) 86 87 if self.getposition().size == 0: ---> 88 if self.datahigh > self.bband.top.shift(1): 89 self.sell() 90 elif self.datalow < self.bband.bot.shift(1): AttributeError: 'LineBuffer' object has no attribute 'shift'
-
@curious_one said in Bollinger band top/bot .shift ability:
if self.datahigh > self.bband.top.shift(1):
Almost there. I'm assuming
self.datahigh
is self.datas[0].high? (include more code next time :)) If so, then try:if self.datahigh[0] > self.bband.top[-1]:
This will yield true if the current bar high is greater then the previous bar bollinger top.
Have a look here for a further explanation of index slicing in backtrader.