Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Bollinger band top/bot .shift ability

    Indicators/Strategies/Analyzers
    2
    2
    222
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      curious_one last edited by

      Re: BBand Strategy - code runs forever with no output

      @run-out

      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'
      
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @curious_one last edited by

        @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.

        RunBacktest.com

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors