backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

Backtest results plot break when using yfinance data

Open marianolatorre opened this issue 2 years ago • 3 comments

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

marianolatorre avatar Sep 06 '23 18:09 marianolatorre

same issue here, any update?

oldriverno1 avatar Sep 18 '23 06:09 oldriverno1

same issue here, any uppate?

alexrmacleod avatar Oct 14 '23 12:10 alexrmacleod

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.

0sahil avatar Jan 04 '24 19:01 0sahil