Cerebo.plot() Error in plotting resampled data
-
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!
-
@dusk you may try to supply the
name
argument to theresampledata
method call:cerebro.resampledata(data, bt.TimeFrame.Minutes, compression=240, name='kuku')
will it work?
-
@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'
-
@dusk seems strange. Could you please show your code?
-
@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
-
@dusk the second parameter to the resampledata should be 'name' and not 'timeframe'. The 'timeframe' should be provided as a named argument only.