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/

    bug with resampling

    General Code/Help
    3
    6
    1557
    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.
    • R
      rsbowman last edited by

      Hi,

      I have some 5m data I'm trying to resample to hourly using cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes) but I get the following error on running cerebro.run()

      Traceback (most recent call last):
        File "test.py", line 237, in <module>
          result = cerebro.run()
        File "/home/who/dev/py/trading/lib/python3.5/site-packages/backtrader/cerebro.py", line 1142, in run
          runstrat = self.runstrategies(iterstrat)
        File "/home/who/dev/py/trading/lib/python3.5/site-packages/backtrader/cerebro.py", line 1310, in runstrategies
          self._runnext(runstrats)
        File "/home/who/dev/py/trading/lib/python3.5/site-packages/backtrader/cerebro.py", line 1627, in _runnext
          self._check_timers(runstrats, dt0, cheat=True)
      UnboundLocalError: local variable 'dt0' referenced before assignment
      

      Indeed you can see at https://github.com/mementum/backtrader/blob/master/backtrader/cerebro.py#L1627 that the variable dt0 is not in scope, and again a few lines later. However, I'm not smart enough to know what the fix is -- when I changed dt0 to data0 I get a different error later....

      Thanks so much for this great software!

      B 1 Reply Last reply Reply Quote 0
      • T
        ThatBlokeDave last edited by

        Hey @rsbowman

        Maybe you can also post your code? The cause might be something else other than resampling.

        Also are you up to date?

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators @rsbowman last edited by backtrader

          @rsbowman said in bug with resampling:

          Indeed you can see at https://github.com/mementum/backtrader/blob/master/backtrader/cerebro.py#L1627 that the variable dt0 is not in scope

          The error message only shows that you have triggered an error condition in which dt0 won't be defined. See below for a 1st good reason for it.

          I have some 5m data I'm trying to resample to hourly

          cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes)
          

          That isolated (gives no real information about what's being done) already shows that:

          • 5m data is being resampled to 1-minute data, which in itself is impossible.

            In any case this diagnostic assumes that data has the right charateristics, which it may not due to the lack of context.

          As pointed out by ThatBlokeDave, some code with the complete picture could really help.

          1 Reply Last reply Reply Quote 0
          • R
            rsbowman last edited by rsbowman

            Thank you both, and thank you for your patience. I think I have misunderstood the Cerebro.resampledata method. My example above was incorrect, here's the code I tried:

            import datetime
            # my backtrader __version__ = '1.9.58.122'
            import backtrader as bt
            
            if __name__ == '__main__':
                cerebro = bt.Cerebro()
                data = bt.feeds.GenericCSVData(
                    dataname="some-5m-data.csv",
                    fromdate=datetime.datetime(2015, 1, 1),
                    todate=datetime.datetime(2015, 12, 31),
                    openinterest=-1,
                    timeframe=bt.TimeFrame.Ticks)
            
                cerebro.resampledata(d, timeframe=60*bt.TimeFrame.Minutes)
                #cerebro.resampledata(data, 
                #    timeframe=bt.TimeFrame.Ticks,
                #    compression=12)
                cerebro.run()
            

            I'd like to turn my 5m data into hourly data. Now I think that multiplying 60 times TimeFrame.Minutes is not meaningful and instead I should do the thing that's commented out above... Is this correct? Compress 12 5m bars into a single 60m bar?

            Thanks again!

            B 1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators @rsbowman last edited by backtrader

              @rsbowman said in bug with resampling:

              data = bt.feeds.GenericCSVData(
                  dataname="some-5m-data.csv",
                  fromdate=datetime.datetime(2015, 1, 1),
                  todate=datetime.datetime(2015, 12, 31),
                  openinterest=-1,
                  timeframe=bt.TimeFrame.Ticks)
              

              That is not being tagged as 5-minutes data. It is being tagged as Ticks

              See here Docs - Data Resampling

              The parameters timeframe and compression are explained for resampledata. They also apply to the data feed for the initial tagging.

              See also the [See: Community - FAQ] for timeframe and compression

              In any case:

              @rsbowman said in bug with resampling:

              Is this correct? Compress 12 5m bars into a single 60m bar?

              You don't specify a transform, you specify your target

              1 Reply Last reply Reply Quote 0
              • R
                rsbowman last edited by

                Ok, so in the case above the correct thing to do would be

                # ...
                data = bt.feeds.GenericCSVData(
                    dataname="some-5m-data.csv",
                    fromdate=datetime.datetime(2015, 1, 1),
                    todate=datetime.datetime(2015, 12, 31),
                    timeframe=bt.TimeFrame.Minutes,
                    compression=5)
                cerebro.resampledata(data, 
                    timeframe=bt.TimeFrame.Minutes,
                    compression=60)
                

                This looks correct when I run it, 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