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/

    Different results in strategy when using multiple timeframes

    Indicators/Strategies/Analyzers
    2
    10
    180
    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.
    • F
      farzadex last edited by

      Hi,
      I have written a simple strategy and I have two timeframes, 1Min and a resampled 5Min. When I define two indicators based on the 5Min data, the result of the backtest differs from when I comment those lines.
      This is the strategy code:

      class Strategy(bt.Strategy):
          def __init__(self):
              self.dataclose = self.datas[0].close
              self.BB1small = bt.ind.BollingerBands(self.datas[0], period=16, devfactor=1.2)
              self.BB1large = bt.ind.BollingerBands(self.datas[0], period=89, devfactor=2.3)
              self.HMA1 = bt.ind.HullMovingAverage(self.datas[0], period=45)
      
              self.close1 = self.datas[0].close
      
              #************This is the part that creates the problem**************
              self.BB5small = bt.ind.BollingerBands(self.datas[1], period=16, devfactor=1.2)
              self.BB5large = bt.ind.BollingerBands(self.datas[1], period=89, devfactor=2.3)
              self.HMA5 = bt.ind.HullMovingAverage(self.datas[1], period=45)
              #********************************************************************
      
          def next(self):
              if self.close1[0] > self.BB1large.lines.top[0] and not self.position:
                  self.buy(size=.1)
              elif self.close1[0] < self.BB1large.lines.bot[0]:
                  self.close()
      
      
      D 1 Reply Last reply Reply Quote 0
      • D
        dasch @farzadex last edited by

        @farzadex its not quite clear what you mean with different results.

        you require more data if you resample from 1 min to 5 min. so the trading time range from your data will contain less trades. that does not mean that results are different but just the time range of the data you use will be shorter.

        F 1 Reply Last reply Reply Quote 0
        • F
          farzadex @dasch last edited by

          @dasch
          If I don't write this part:

                  self.BB5small = bt.ind.BollingerBands(self.datas[1], period=16, devfactor=1.2)
                  self.BB5large = bt.ind.BollingerBands(self.datas[1], period=89, devfactor=2.3)
                  self.HMA5 = bt.ind.HullMovingAverage(self.datas[1], period=45)
          

          the final portfolio value of backtest is 9976.
          but if I write it, however its data is not used in the next method, it affects the result and changes to 10042.

          D 2 Replies Last reply Reply Quote 0
          • D
            dasch @farzadex last edited by

            @farzadex yes, that is expected then, you don't take some trades, since there is no data available yet on the 5min indicators.

            if you disable these indicators, backtrader will not need to wait until these indicators are filled.

            if you enable them, you will get into nextlater. just log you data time to see the difference.

            1 Reply Last reply Reply Quote 1
            • D
              dasch @farzadex last edited by

              @farzadex

              for example:

              self.BB5large = bt.ind.BollingerBands(self.datas[1], period=89

              period=89 on 5min -> 89*5 = 445 -> data will need at least 445minutes 1min data to fill the BB5large.

              F 1 Reply Last reply Reply Quote 1
              • F
                farzadex @dasch last edited by

                @dasch Thanks. You're right. but how can I overcome this? because later I need to compare the values of indicators of various timeframes. what is the right way to do that?
                by the way, I used 1-min BTCUSD data for 1 month

                D 1 Reply Last reply Reply Quote 0
                • D
                  dasch @farzadex last edited by

                  @farzadex not really sure what you mean with overcoming this. The indicators need to wait until they have enough data, nothing to overcome here.

                  If you use one strategy with different timeframes, just add all datas, indicators you want to analyze and then log the values or compare them like you want.

                  F 1 Reply Last reply Reply Quote 0
                  • F
                    farzadex @dasch last edited by

                    @dasch
                    I'm new to backtrader and I still don't know why there is such difference between results. what you say is absolutely correct, but 445 minutes is less than a day and I don't think many trades could have happened in first 445 minutes.

                    D 1 Reply Last reply Reply Quote 0
                    • D
                      dasch @farzadex last edited by

                      why do you think, that the diff cannot come from the 445 minutes? its a diff of 66 dollars.

                      best way to find out is creating some logs, writing some analyzer, compare both results and look which trades were made.

                      with the limited amount of information provided this seems the best reason why you get different results.

                      F 1 Reply Last reply Reply Quote 1
                      • F
                        farzadex @dasch last edited by

                        @dasch
                        I logged the data and the first 3 trades don't happen when 5-min indicator is used and that's the difference between results.
                        Thanks 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