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 Strategy does not work properly

    Indicators/Strategies/Analyzers
    2
    3
    256
    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.
    • H
      HexaBraum last edited by

      Even though the buy and sell signals get triggered it seems like the orders get executed delayed or whatever. Here is my code for init and next:

          def __init__(self):
      
              # self.dataclose = self.datas[0].close
      
              self.bb_inds = dict()
              for d in self.datas:
                  bb = bt.ind.BollingerBands(d, period=21, devfactor=2.0,
                                             movav=bt.ind.MovAv.Simple)
                  self.bb_inds[d] = dict()
                  self.bb_inds[d]["bb_top"] = bb.top
                  self.bb_inds[d]["bb_bot"] = bb.bot
      
      
          def notify_order(self, order):
      
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enough cash
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
      
                  self.bar_executed = len(self)
      
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
              # Write down: no pending order
      
              #self.order = None
      
          def next(self):
              for d in self.datas:
                  dt, dn = self.datetime.date(), d._name
                  pos = self.getposition(d).size
                  # Simply log the closing price of the series from the reference
                 # self.log('Close, %.2f' % d.close[0])
      
              # Check if we are in the market
              #if not self.position:
                  if d.close[0] < self.bb_inds[d]["bb_bot"][0]:
      
                      # BUY, BUY, BUY!!! (with default parameters)
                      self.log('BUY CREATE, %.2f' % d.close[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      #self.order = self.buy()
                      self.buy(data=d, size=1, price=d.close[0])
      
                  # Already in the market ... we might sell
                  if d.close[0] > self.bb_inds[d]["bb_top"][0]:
                      # SELL, SELL, SELL!!! (with all possible default parameters)
                      self.log('SELL CREATE, %.2f' % d.close[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      #self.order = self.sell()
                      self.sell(data=d, size=1, price=d.close[0])
      

      The plot:
      Plot.PNG

      1 Reply Last reply Reply Quote 0
      • H
        HexaBraum last edited by

        Fixed it.

        H 1 Reply Last reply Reply Quote 0
        • H
          hoflz5 @HexaBraum last edited by

          @hexabraum what was the issue?

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