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

Error in .view() in _plotting._group_trades(column)

Open grayskripko opened this issue 4 years ago • 3 comments

Expected Behavior

When I pass too big dataframe and the system tries to resample it I have the error in the next screenshot

Actual Behavior

I think the screenshot should be more informative image

I tried .view(int) and .view('uint8') from the pandas documentation and got a little different error messages ...Length of values (16)... and ...Length of values(maybe 8, I forgot)...

Steps to Reproduce

I can't share data with you, sorry. In case you don't have enough information you can close the ticket.

  • Backtesting version: 0.3.3

grayskripko avatar Jan 20 '22 13:01 grayskripko

I've found the source of the problem

image

Backtest.plot(resample=True) works fine with my dataset when I reset my date-time index.

grayskripko avatar Jan 20 '22 13:01 grayskripko

Indeed, a small sample of the data with which to reproduce the bug sure would help.

kernc avatar Jan 20 '22 13:01 kernc

Also have this issue, in debugger the place with error was in call .view(int)
Managed to solve it locally, changed view(int) to view('int64') but then we need to changes nanoseconds to seconds, so result is divided by 1e9

my versions

  • Backtesting==0.3.3
  • numpy==1.22.2
  • pandas==1.4.0

In _plotting.py replaced function _group_trades

    def _group_trades(column):
        def _series_to_int(s):
            return s.view('int64').apply(lambda x: x / 1e9)

        def _array_to_int(a):
            return np.array([xi / 1e9 for xi in a.view('int64')])
            
        def f(s, new_index=pd.Index(_array_to_int(df.index)), bars=trades[column]):
            if s.size:
                # Via int64 because on pandas recently broken datetime
                mean_time = int(_series_to_int(bars.loc[s.index]).mean())
                # Because of FutureWarning: Passing method to Float64Index.get_loc is deprecated and will raise in a future version. Use index.get_indexer([item], method=...) instead.
                new_bar_idx = new_index.get_indexer([mean_time], method='nearest')[0]
                return new_bar_idx
        return f

funduck avatar Feb 22 '22 14:02 funduck