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.06k stars 990 forks source link

Backtest results plot break when using yfinance data #1064

Open marianolatorre opened 10 months ago

marianolatorre commented 10 months ago

Expected Behavior

Plot backtest results using data from yfinance

Actual Behavior

Getting error code

File ~/Documents/git/python.strategy-optimisations/.venv/lib/python3.9/site-packages/backtesting/_plotting.py:148, in _maybe_resample_data.<locals>._group_trades.<locals>.f(s, new_index, bars)
    145 if s.size:
    146     # Via int64 because on pandas recently broken datetime
    147     mean_time = int(bars.loc[s.index].view(int).mean())
--> 148     new_bar_idx = new_index.get_loc(mean_time, method='nearest')
    149     return new_bar_idx

TypeError: get_loc() got an unexpected keyword argument 'method'

Steps to Reproduce

Please see the colab link https://colab.research.google.com/drive/1R4JjhkdSi47QQI2gV7QUZXsYzY2-ihrr?usp=sharing

I'm learning to use the library and following the examples in the website which are based on data from GOOG. I wanted to swap the data to BTC from yfinance which works well when running the backtester but it breaks when plotting the chart.

Apart from the index named "Datetime" I can't see any other difference.

Screenshot 2023-09-06 at 19 08 34
oldriverno1 commented 9 months ago

same issue here, any update?

alexrmacleod commented 8 months ago

same issue here, any uppate?

0sahil commented 6 months ago

I believe when there is too much data on the graph to be shown, the data is resampled and then shown (which is the default behaviour), and then I got the same error. What worked for me was passing bt.plot(resample=False) while plotting the graph.