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/

    mixing timeframe indicators

    General Code/Help
    1
    1
    21
    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 use two resample datas for 3 timeframe Boll indicators. before coding ,I do read the help page:
      https://www.backtrader.com/docu/mixing-timeframes/indicators-mixing-timeframes/
      Forgive me that I dont really understand its thoery. Here is my code:

      class AG2022(bt.Strategy):
        boll_mid1m = []
        boll_mid5m = []
        boll_mid30m = []
        bollrace = []
        
       def __init__(self):
              self.buy_order = None
              self.live_data = False
              self.dataclose = self.data0.close
              self.order = None
              self.boll_mid1m = btind.BollingerBands(self.data0, period=26).mid
              self.boll_mid5m = bt.indicators.BollingerBands(self.data1, period=26).mid()
              self.boll_mid30m = bt.indicators.BollingerBands(self.data2, period=26).mid()
              self.bollrace = self.boll_mid5m[0] / self.boll_mid30m[-2]
      
      def next(self):
           print(self.boll_mid1m[0])       
           print(self.bollrace[0])
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          store = CTPStore(ctp_setting)
          init_backfill = 1000 if is_trading_period() else 0
          stockkwargs = dict(
              # latethrough=False,
              timeframe=bt.TimeFrame.Ticks,
              num_init_backfill=1000,
              # init_backfill=0
          )
          _TICKER = "ag2206.SHFE"
          data = store.getdata(dataname=_TICKER, **stockkwargs)
          cerebro.adddata(data)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=30)
          cerebro.addstrategy(AG2022, store=store)
          cerebro.run()
      

      the print(self.boll_mid1m[0]) is ok.
      the print(self.bollrace[0]), sometimes generates error
      return self.array[self.idx + ago]
      IndexError: array index out of range

      or just nothing at all, not going into "next" section.
      I guess it could be backfill not enough problem , so I increase to 1000maximum from provider.
      but the value is always "0"

      anything wrong in my code? appreciated for your help.

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