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/

    How to use 5minutes BOLL and 10 minute BOLL together?

    Indicators/Strategies/Analyzers
    2
    3
    101
    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.
    • G
      goldcar last edited by

      I have 1 minute CSV data file already. Now, I know I should use resampling function to make it working. however it failed because I lack of knowledge of Python. hope can get some help here.

      here is the datas feed.

        datapath = '../datas/ag2206.csv'
          data = btfeeds.GenericCSVData(dataname=datapath,
                                        fromdate=datetime(2022, 1, 20),
                                        todate=datetime(2022, 1, 29),
                                        nullvalue=0.0,
                                        dtformat=('%Y-%m-%d %H:%M:%S'),
                                        # timeframe=bt.TimeFrame.Minutes,
                                        datetime=2,
                                        open=3,
                                        high=4,
                                        low=5,
                                        close=6,
                                        volume=7,
                                        openinterest=9
                                        )
          data5m = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)
          cerebro.adddata(data5m)
          data30m = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
          cerebro.adddata(data30m)
          # Run over everything
          cerebro.run()
      

      I am not sure above is correct.

      then in strategy part,

      class Boll_strategy(bt.Strategy):
          def __init__(self):
              # I think 5minute boll should be in datas[0]?
              self.lines.top = bt.indicators.BollingerBands(self.datas[0], period=26).top
              self.lines.top = bt.indicators.BollingerBands(self.datas[1], period=26).bot
          def next(self):
              if self.datas[1].close:
                  print('-----------30top=%s' % (self.lines.top30[0]))
              if self.datas[0].close:
                 print('-----------5m top=%s' % (self.lines.top[0]))
      

      but nothing shows up. what did I miss? I am sure there are enough 1 minutes datas in file. there are 6 million 1 minute datas in file.

      Appreciated for any help.

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

        A few things to help you on your way.

        You don't need to add data with resample. Resample will add the date automatically.

        Also, you use the variable self.lines.top twice. Finally, I would avoid using the .lines. Not sure if there's an impact here but lines are an important word inside backtrader.

        I've rewritten your code a bit to help you out.

        import backtrader as bt
        
        class Strategy(bt.Strategy):
        
            params = (("bb_period", 22),)
        
            def __init__(self):
                self.bb = bt.indicators.BollingerBands(self.datas[0], period=self.p.bb_period)
                self.bb5 = bt.indicators.BollingerBands(self.datas[1], period=self.p.bb_period)
                self.bb30 = bt.indicators.BollingerBands(self.datas[2], period=self.p.bb_period)
        
            def next(self):
                print(
                    "OHLCV",
                    bt.num2date(self.data.datetime[0]),
                    self.datas[0].close[0],
                    self.datas[1].close[0],
                    self.datas[2].close[0],
                )
                print(
                    "Bollinger Bands\n",
                    "minute", self.bb.top[0], self.bb[0], self.bb.bot[0], '\n',
                    "5 min", self.bb5.top[0], self.bb5[0], self.bb5.bot[0], '\n',
                    "30 min", self.bb30.top[0], self.bb30[0], self.bb30.bot[0], '\n',
                )
        
        if __name__ == "__main__":
        
            cerebro = bt.Cerebro()
        
            data = bt.feeds.GenericCSVData(
                dataname="data/dev.csv",
                dtformat=("%Y-%m-%d %H:%M:%S"),
                timeframe=bt.TimeFrame.Minutes,
                compression=1,
                date=0,
                high=1,
                low=2,
                open=3,
                close=4,
                volume=6,
            )
        
            cerebro.adddata(data)
            cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)
            cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
        
            cerebro.addstrategy(Strategy)
        
            # Execute
            cerebro.run()
        
        

        RunBacktest.com

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

          @run-out said in How to use 5minutes BOLL and 10 minute BOLL together?:

          A few things to help you on your way.

          You don't need to add data with resample. Resample will add the date automatically.

          Also, you use the variable self.lines.top twice. Finally, I would avoid using the .lines. Not sure if there's an impact here but lines are an important word inside backtrader.

          I've rewritten your code a bit to help you out.

          import backtrader as bt
          
          class Strategy(bt.Strategy):
          
              params = (("bb_period", 22),)
          
              def __init__(self):
                  self.bb = bt.indicators.BollingerBands(self.datas[0], period=self.p.bb_period)
                  self.bb5 = bt.indicators.BollingerBands(self.datas[1], period=self.p.bb_period)
                  self.bb30 = bt.indicators.BollingerBands(self.datas[2], period=self.p.bb_period)
          
              def next(self):
                  print(
                      "OHLCV",
                      bt.num2date(self.data.datetime[0]),
                      self.datas[0].close[0],
                      self.datas[1].close[0],
                      self.datas[2].close[0],
                  )
                  print(
                      "Bollinger Bands\n",
                      "minute", self.bb.top[0], self.bb[0], self.bb.bot[0], '\n',
                      "5 min", self.bb5.top[0], self.bb5[0], self.bb5.bot[0], '\n',
                      "30 min", self.bb30.top[0], self.bb30[0], self.bb30.bot[0], '\n',
                  )
          
          if __name__ == "__main__":
          
              cerebro = bt.Cerebro()
          
              data = bt.feeds.GenericCSVData(
                  dataname="data/dev.csv",
                  dtformat=("%Y-%m-%d %H:%M:%S"),
                  timeframe=bt.TimeFrame.Minutes,
                  compression=1,
                  date=0,
                  high=1,
                  low=2,
                  open=3,
                  close=4,
                  volume=6,
              )
          
              cerebro.adddata(data)
              cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)
              cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
          
              cerebro.addstrategy(Strategy)
          
              # Execute
              cerebro.run()
          
          

          cool, works charm, thanks a lot

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