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/

    Cerebo.plot() Error in plotting resampled data

    General Code/Help
    2
    6
    167
    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.
    • D
      dusk last edited by

      I would love to backtest my strategy at a 4-hour timeframe. Therefore, I tried resampling it from minute to 4hour with bt.TimeFrame.Minutes, compression = 240. However, when I run cerebro.plot with this, it showed typeerror (which never showed up when I simply adddata(data)

      #CODE
      cerebro = bt.Cerebro()
      data = pd.read_csv("some.csv",
                         index_col="datetime",
                         parse_dates=True)
      data = bt.feeds.PandasData(dataname=data,
                                 fromdate=datetime(2020,10,1),
                                 todate=datetime(2020,12,31))
      cerebro.resampledata(data, bt.TimeFrame.Minutes, compression=240)
      cerebro.broker.setcash(100000)
      cerebro.broker.setcommission(0.001)
      cerebro.run()
      cerebro.plot()
      
      #ERROR
      Traceback (most recent call last):
        File "/Users/steven/Documents/Projects/Quantitative Trading/Backtesting.py", line 21, in <module>
          cerebro.plot()
        File "/Users/steven/Documents/Projects/Quantitative Trading/venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 991, in plot
          start=start, end=end, use=use)
        File "/x/venv/lib/python3.7/site-packages/backtrader/plot/plot.py", line 220, in plot
          self.plotdata(data, self.dplotsover[data])
        File "/x/venv/lib/python3.7/site-packages/backtrader/plot/plot.py", line 661, in plotdata
          datalabel += data._name
      TypeError: can only concatenate str (not "int") to str
      

      I have tried to find where does the int come from but failed. Would love some input for this, thanks!

      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @dusk last edited by

        @dusk you may try to supply the name argument to the resampledata method call:

        cerebro.resampledata(data, bt.TimeFrame.Minutes, compression=240, name='kuku')
        

        will it work?

        D 1 Reply Last reply Reply Quote 1
        • D
          dusk @vladisld last edited by

          @vladisld seems not as well
          it showed multiple value passed for the argument "name", which i guess it is from dataname = data from bt.feeds.PandasData(), and name="kuku".

          Traceback (most recent call last):
            File "x/Backtesting.py", line 28, in <module>
              name="kuku")
          TypeError: resampledata() got multiple values for argument 'name'
          
          vladisld 1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld @dusk last edited by

            @dusk seems strange. Could you please show your code?

            D 1 Reply Last reply Reply Quote 0
            • D
              dusk @vladisld last edited by

              @vladisld Sure! The error occurs regardless of my strategy. Didn't add any strategy below.

              import backtrader as bt
              import pandas as pd
              from datetime import datetime
              
              cerebro = bt.Cerebro()
              data = pd.read_csv("Cryptofeed/BTCUSDT_d.csv",
                                 index_col="datetime",
                                 parse_dates=True)
              data = bt.feeds.PandasData(dataname=data,
                                         fromdate=datetime(2020,10,1),
                                         todate=datetime(2020,12,31))
              cerebro.resampledata(data,bt.TimeFrame.Minutes,compression=240,name="kuku")
              cerebro.broker.setcash(100000)
              cerebro.broker.setcommission(0.001)
              print(f"Starting Portfolio Value: {cerebro.broker.getvalue()}")
              cerebro.run()
              print(f"Ending Portfolio Value: {cerebro.broker.getvalue()}")
              cerebro.plot()
              

              Error Message:

              Traceback (most recent call last):
                File "/x/Backtesting.py", line 25, in <module>
                  cerebro.resampledata(data,bt.TimeFrame.Minutes,compression=240,name="kuku")
              TypeError: resampledata() got multiple values for argument 'name'
              

              Thanks a lot!

              P.S If i delete the name="kuku" part, error message will become

              Traceback (most recent call last):
                File "/x/Backtesting.py", line 33, in <module>
                  cerebro.plot()
                File "/x/venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 991, in plot
                  start=start, end=end, use=use)
                File "/x/venv/lib/python3.7/site-packages/backtrader/plot/plot.py", line 220, in plot
                  self.plotdata(data, self.dplotsover[data])
                File "/x/venv/lib/python3.7/site-packages/backtrader/plot/plot.py", line 661, in plotdata
                  datalabel += data._name
              TypeError: can only concatenate str (not "int") to str
              
              vladisld 1 Reply Last reply Reply Quote 0
              • vladisld
                vladisld @dusk last edited by

                @dusk the second parameter to the resampledata should be 'name' and not 'timeframe'. The 'timeframe' should be provided as a named argument only.

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