kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.05k stars 989 forks source link

Can't run it over 10001 index from csv , then I got the error f"Length of passed values is {len(data)}, " ValueError: Length of passed values is 2, index implies 1. #792

Open KnudY9 opened 1 year ago

KnudY9 commented 1 year ago

Expected Behavior

I wanna use it over 10000 index with this command(index_col='time', parse_dates=True) without the error ,when I import the csv.

Of cource I should type the code like follow. df = pd.read_csv("df.csv")

But the date didn't recognize ,when I don't use index_col='time', parse_dates=True. And the follow error occurred ,when I import the csvfile over 10000 index.There is reproducibility.

Actual Behavior

I read the csv by follow code and csv

code filename = "raw.csv" df = pd.read_csv(filename,index_col='time', parse_dates=True)

csvdata example actual data have 10002 index. time Open High Low Close Volume
2020/1/1 0:01 7187.67 7188.06 7182.2 7184.03 7.248148
2020/1/1 0:02 7184.41 7184.71 7180.26 7182.43 11.68168
2020/1/1 0:03 7183.83 7188.94 7182.49 7185.94 10.02539

Line 10001 does not give an error. But an error occurs when the csv reaches 10002 rows. This error occurred when reading CSV with the following command.

target code index_col='time', parse_dates=True

Steps to Reproduce

1. 2. 3.


C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting\_plotting.py:122: UserWarning: Data contains too many candlesticks to plot; downsampling to '1T'. See `Backtest.plot(resample=...)`
  warnings.warn(f"Data contains too many candlesticks to plot; downsampling to {freq!r}. "
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\groupby\generic.py", line 261, in aggregate
    func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\groupby\groupby.py", line 1085, in _python_agg_general
    result, counts = self.grouper.agg_series(obj, f)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\groupby\ops.py", line 904, in agg_series
    return grouper.get_result()
  File "pandas\_libs\reduction.pyx", line 164, in pandas._libs.reduction.SeriesBinGrouper.get_result
  File "pandas\_libs\reduction.pyx", line 76, in pandas._libs.reduction._BaseGrouper._apply_to_group
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\groupby\groupby.py", line 1062, in <lambda>
    f = lambda x: func(x, *args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting\_plotting.py", line 147, in f
    mean_time = int(bars.loc[s.index].view(int).mean())
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\series.py", line 668, in view
    self._values.view(dtype), index=self.index
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\series.py", line 314, in __init__
    f"Length of passed values is {len(data)}, "
ValueError: Length of passed values is 2, index implies 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "1.py", line 183, in <module>
    bt.plot() 
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting\backtesting.py", line 1609, in plot
    open_browser=open_browser)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting\_plotting.py", line 204, in plot
    resample, df, indicators, equity_data, trades)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting\_plotting.py", line 158, in _maybe_resample_data
    ExitBar=_group_trades('ExitTime'),
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\resample.py", line 288, in aggregate
    result, how = self._aggregate(func, *args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\base.py", line 416, in _aggregate
    result = _agg(arg, _agg_1dim)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\base.py", line 383, in _agg
    result[fname] = func(fname, agg_how)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\base.py", line 367, in _agg_1dim
    return colg.aggregate(how)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\groupby\generic.py", line 267, in aggregate
    result = self._aggregate_named(func, *args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\groupby\generic.py", line 480, in _aggregate_named
    output = func(group, *args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\backtesting\_plotting.py", line 147, in f
    mean_time = int(bars.loc[s.index].view(int).mean())
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\series.py", line 668, in view
    self._values.view(dtype), index=self.index
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\series.py", line 314, in __init__
    f"Length of passed values is {len(data)}, "
ValueError: Length of passed values is 2, index implies 1.

Additional info

KnudY9 commented 1 year ago

anybody here_?

kernc commented 1 year ago

Can you share some minimal example code to reproduce the issue?

muratokutucu commented 10 months ago

Hello, I have the same issue. Any solution please?