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/

    Attempting to create Bollinger Band indicator from scratch, am I missing studies? (SMA, StdDev)

    General Code/Help
    2
    4
    135
    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.
    • earlcharles1
      earlcharles1 last edited by

      from future import (absolute_import, division, print_function,
      unicode_literals)

      import datetime # For datetime objects
      import os.path # To manage paths
      import sys # To find out the script name (in argv[0])

      Import the backtrader platform

      import backtrader as bt
      class BollingerBand(bt.Strategy):

      params = (('period', 5),
          ('devfactor', 3.0),)
      
      
      def __init__(self):
          self.period = self.params.period
          self.devfactor = self.params.devfactor
          self.close = self.datas[0].close
          self.midband = bt.indicators.SimpleMovingAverage(self.close,self.period)
          self.topband = self.midband + self.devfactor*StandardDeviation(data,period)
          self.botband = self.midband - self.devfactor*StandardDeviation(data,period)
      
      def next(self):
          self.log('Close, %.2f' % self.close[0])
          if self.order:
              return
          if not self.position:
              if self.close[0] > self.topband:
                  self.log('Short/Sale, %.2f' % self.close)
                  self.order = self.sell()
          else:
              if self.close[0] < self.botband:
                  self.log('Buy, %.2f' % self.close)
                  self.order = self.buy()
      

      if name == 'main':
      cerebro = bt.Cerebro()
      cerebro.addindicator(BollingerBand)
      modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
      datapath = os.path.join(modpath, 'RICK.csv')
      data = bt.feeds.YahooFinanceCSVData(
      dataname=datapath,
      # Do not pass values before this date
      fromdate=datetime.datetime(2019, 11, 13),
      # Do not pass values before this date
      todate=datetime.datetime(2020, 11, 13),
      # Do not pass values after this date
      reverse=False)
      cerebro.adddata(data)
      cerebro.broker.setcash(5000.0)
      cerebro.addsizer(bt.sizers.FixedSize, stake =10)
      cerebro.broker.setcommission(commission=0.0)
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        There is alread a built in Bollinger Bands. If you feel the need to recreate it, you should extend the class bt.Indicators not bt.Strategy. Then instantiate the indicator in your strategy.

        If developing bespoke you might consider looking at the Bollinger Band code (https://github.com/backtrader2/backtrader/blob/master/backtrader/indicators/bollinger.py) in the indicators class in backtrader to see the original code.

        RunBacktest.com

        earlcharles1 1 Reply Last reply Reply Quote 2
        • earlcharles1
          earlcharles1 @run-out last edited by

          @run-out Thank you, I tried to delete this post but couldn't find the option.

          run-out 1 Reply Last reply Reply Quote 1
          • run-out
            run-out @earlcharles1 last edited by

            @earlcharles1 Yes all of our thoughts are recorded forever here. :)

            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