kernc/backtesting.py

bt.plot(): TypeError: float() argument must be a string or a real number, not 'Timestamp'

Open

#906 opened on Feb 9, 2023

View on GitHub
 (5 comments) (0 reactions) (0 assignees)Python (4,387 stars) (863 forks)batch import
help wanted

Description

Expected Behavior

bt.plot()

plot

Actual Behavior

TypeError                                 Traceback (most recent call last)
Cell In[61], line 1
----> 1 bt.plot()

File ~/miniconda3/envs/tensorflow/lib/python3.10/site-packages/backtesting/backtesting.py:1592, in Backtest.plot(self, results, filename, plot_width, plot_equity, plot_return, plot_pl, plot_volume, plot_drawdown, smooth_equity, relative_equity, superimpose, resample, reverse_indicators, show_legend, open_browser)
   1589         raise RuntimeError('First issue `backtest.run()` to obtain results.')
   1590     results = self._results
-> 1592 return plot(
   1593     results=results,
   1594     df=self._data,
   1595     indicators=results._strategy._indicators,
   1596     filename=filename,
   1597     plot_width=plot_width,
   1598     plot_equity=plot_equity,
   1599     plot_return=plot_return,
   1600     plot_pl=plot_pl,
   1601     plot_volume=plot_volume,
   1602     plot_drawdown=plot_drawdown,
   1603     smooth_equity=smooth_equity,
   1604     relative_equity=relative_equity,
   1605     superimpose=superimpose,
   1606     resample=resample,
   1607     reverse_indicators=reverse_indicators,
   1608     show_legend=show_legend,
   1609     open_browser=open_browser)

File ~/miniconda3/envs/tensorflow/lib/python3.10/site-packages/backtesting/_plotting.py:613, in plot(results, df, indicators, filename, plot_width, plot_equity, plot_return, plot_pl, plot_volume, plot_drawdown, smooth_equity, relative_equity, superimpose, resample, reverse_indicators, show_legend, open_browser)
    611 ohlc_bars = _plot_ohlc()
    612 _plot_ohlc_trades()
--> 613 indicator_figs = _plot_indicators()
    614 if reverse_indicators:
    615     indicator_figs = indicator_figs[::-1]

File ~/miniconda3/envs/tensorflow/lib/python3.10/site-packages/backtesting/_plotting.py:573, in plot.<locals>._plot_indicators()
    568     r = fig.line(
    569         'index', source_name, source=source,
    570         legend_label=LegendStr(legend_label), line_color=color,
    571         line_width=1.3)
    572 # Add dashed centerline just because
--> 573 mean = float(pd.Series(arr).mean())
    574 if not np.isnan(mean) and (abs(mean) < .1 or
    575                            round(abs(mean), 1) == .5 or
    576                            round(abs(mean), -1) in (50, 100, 200)):
    577     fig.add_layout(Span(location=float(mean), dimension='width',
    578                         line_color='#666666', line_dash='dashed',
    579                         line_width=.5))

TypeError: float() argument must be a string or a real number, not 'Timestamp'

stats = bt.run() works fine.. not sure why I can't plot

  • Python: 3.10
  • Backtesting version: 0.3.3
  • bokeh.version: 2.4.3
  • OS: MacOS Ventura 13.1

Contributor guide