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.53k stars 1.07k forks source link

ValueError: Invalid value for `superimpose`: Upsampling not supported. #233

Open sedna16 opened 3 years ago

sedna16 commented 3 years ago

This error start appearing a day ago when I was testing several strategies. I didnt changed anything in my codes before this happened.

The backtest doesnt continue and and the chart does not appear.

C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\backtesting\backtesting.py:1042: FutureWarning: Index.is_all_dates is deprecated, will be removed in a future version.  check index.inferred_type instead
  if (not data.index.is_all_dates and
C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\backtesting\backtesting.py:1057: FutureWarning: Index.__and__ operating as a set operation is deprecated, in the future this will be a logical operation matching Series.__and__.  Use index.intersection(other) instead
  if len(data.columns & {'Open', 'High', 'Low', 'Close', 'Volume'}) != 5:
ValueError: Invalid value for `superimpose`: Upsampling not supported.
sedna16 commented 3 years ago

Ok I think I found why this happened.

doenst work stockcode - NASDAQ:TSLA date format - strftime('%Y-%m-%d')

works here stockcode - NASDAQ:TSLA date format - strftime('%Y-%m-%d %H:%M:%S')

kernc commented 3 years ago

This is the relevant code part. https://github.com/kernc/backtesting.py/blob/1ee5670d03b26680d9add5c96a9f98e44fc722a2/backtesting/_plotting.py#L423-L445 I wonder what

import pandas as pd

df: pd.DataFrame  # your OHLC data

pd.DatetimeIndex(df.index).resolution  # ?

returns for your data?

kernc commented 3 years ago

@sedna16 Can you show a small, couple-lines example dataframe/index where this fails? I don't quite get this part:

doenst work stockcode - NASDAQ:TSLA date format - strftime('%Y-%m-%d')

sedna16 commented 3 years ago

ok, I'll try to recreate this.

sedna16 commented 3 years ago

@kernc - here's the whole print message

Backtest: Retreiving results with chart.
mySQL: rows from trading_data_api has been fetched.
                           open        high         low       close    volume
date
2017-01-30 14:30:00   50.506001   51.057999   49.419998   50.125999  19005500
2017-01-31 14:30:00   49.848000   51.178001   49.540001   50.386002  20580500
2017-02-01 14:30:00   50.610001   50.639999   49.810001   49.848000  19794000
2017-02-02 14:30:00   49.667999   50.484001   49.542000   50.310001  12499000
2017-02-03 14:30:00   50.382000   50.436001   49.936001   50.265999  10933500
...                         ...         ...         ...         ...       ...
2021-01-25 14:30:00  855.000000  900.400024  838.820007  880.799988  41173400
2021-01-26 14:30:00  891.380005  895.900024  871.599976  883.090027  23131600
2021-01-27 14:30:00  870.349976  891.500000  858.659973  864.159973  27334000
2021-01-28 14:30:00  820.000000  848.000000  801.000000  835.429993  26378000
2021-01-29 21:00:02  830.000305  842.409973  780.099976  793.530029  33999137

[1008 rows x 5 columns]
C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\backtesting\backtesting.py:1042: FutureWarning: Index.is_all_dates is deprecated, will be removed in a future version.  check index.inferred_type instead
  if (not data.index.is_all_dates and
C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\backtesting\backtesting.py:1057: FutureWarning: Index.__and__ operating as a set operation is deprecated, in the future this will be a logical operation matching Series.__and__.  Use index.intersection(other) instead
  if len(data.columns & {'Open', 'High', 'Low', 'Close', 'Volume'}) != 5:
Invalid value for `superimpose`: Upsampling not supported.
sedna16 commented 3 years ago

this is from yahoo, 7days 1min

kernc commented 3 years ago

Ok, the bug is due to weird timestamps:

...
2021-01-27 14:30:00
2021-01-28 14:30:00
2021-01-29 21:00:02  # <-- second change

for which pd.DatetimeIndex(...).resolution returns second, whereas the data period is computed as the median difference between successive values: https://github.com/kernc/backtesting.py/blob/1ee5670d03b26680d9add5c96a9f98e44fc722a2/backtesting/_util.py#L37-L40 which is more than T (one minute) that second resolution downsamples to.

sedna16 commented 3 years ago

this only happens on intra yahoo finance data. on finnhub, its ok

ruipinghe commented 3 years ago

I ran into this problem as well, should the timeseries be equal length? mine is like this: '2011-06-05 14:20:32', '2011-06-05 14:21:21', '2011-06-05 14:23:47', '2011-06-05 14:26:29', after I resample this issue solved, but I don't want it to be resampled, any else ways to solve this?

stephanebruckert commented 3 years ago

In my case ignoring the last row from the dataset fixed the issue:

2021-10-06 19:30:00+01:00 2021-10-06 19:45:00+01:00 2021-10-06 20:06:02+01:00

df = df.iloc[:-1]
rigidlab commented 2 years ago

I also use a dataframe returned from yfinance download. It looks like those weird timestamps have 0 volume, so I filter it out and rather than resample. Seems to work okay

df=df[df.Volume!=0]

AgarwalPragy commented 2 years ago

Getting the same issue.

It seems like this is because of the closing time not being at 00:00:00 AM.

The datetimes I have look like 2000-01-03 18:30:00, which is why pd.DatetimeIndex(df['datetime']).resolution returns minute, even though the orig_freq of data is 1 days.