Ka Meng Ng
Ka Meng Ng

Reputation: 61

backtesting.py ploting function not working

I'm trying to learn backtesting.py, when I run the following sample code, it pops up these errors, anyone could help? I tried to uninstall the Bokeh package and reinstall an older version, but it doen't work.

BokehDeprecationWarning: Passing lists of formats for DatetimeTickFormatter scales was deprecated in Bokeh 3.0. Configure a single string format for each scale
C:\Users\paul_\AppData\Local\Programs\Python\Python310\lib\site-packages\bokeh\models\formatters.py:399: UserWarning: DatetimeFormatter scales now only accept a single format. Using the first prodvided: '%d %b'
  warnings.warn(f"DatetimeFormatter scales now only accept a single format. Using the first prodvided: {fmt[0]!r} ")
BokehDeprecationWarning: Passing lists of formats for DatetimeTickFormatter scales was deprecated in Bokeh 3.0. Configure a single string format for each scale
C:\Users\paul_\AppData\Local\Programs\Python\Python310\lib\site-packages\bokeh\models\formatters.py:399: UserWarning: DatetimeFormatter scales now only accept a single format. Using the first prodvided: '%m/%Y'
  warnings.warn(f"DatetimeFormatter scales now only accept a single format. Using the first prodvided: {fmt[0]!r} ")
GridPlot(id='p11925', ...)
import bokeh
import datetime
import pandas_ta as ta
import pandas as pd

from backtesting import Backtest
from backtesting import Strategy
from backtesting.lib import crossover
from backtesting.test import GOOG

class RsiOscillator(Strategy):

    upper_bound = 70
    lower_bound = 30
    rsi_window = 14

    # Do as much initial computation as possible
    def init(self):
        self.rsi = self.I(ta.rsi, pd.Series(self.data.Close), self.rsi_window)

    # Step through bars one by one
    # Note that multiple buys are a thing here
    def next(self):
        if crossover(self.rsi, self.upper_bound):
            self.position.close()
        elif crossover(self.lower_bound, self.rsi):
            self.buy()

bt = Backtest(GOOG, RsiOscillator, cash=10_000, commission=.002)
stats = bt.run()
bt.plot()

Upvotes: 6

Views: 10750

Answers (8)

Smc1962
Smc1962

Reputation: 1

Try this:

from backtesting import set_bokeh_output
set_bokeh_output(notebook=False)

It worked for me.

Upvotes: 0

György Baffia
György Baffia

Reputation: 1

I deleted all parameters of the function in the backtesting/_plotting.py file:

 formatter=DatetimeTickFormatter()

It's now using the default settings and it's working fine...

Upvotes: 0

PHANTOM-X
PHANTOM-X

Reputation: 586

I downgraded bokeh from 3.5.0 to 3.1.0 and worked

pip uninstall bokeh
pip install bokeh==3.1.0

my requirements.txt

pandas==2.2.2
plotly==5.23.0
numpy==2.0.1
Backtesting==0.3.3
bokeh==3.1.0
pip install -r requirements.txt

make sure you have the correct version, as you'll need to restart your Jupiter Notebook or reload the window Ctrl + Shift + P then type Reload Window

import bokeh
print(bokeh.__version__)

// outputs 3.1.0

Upvotes: 1

oats
oats

Reputation: 477

An issue was opened for this in the GitHub repo: https://github.com/kernc/backtesting.py/issues/803

A comment in the issue suggests to downgrade bokeh to 2.4.3:

python3 -m pip install bokeh==2.4.3

This worked for me.

NOTE:

Restart your Jupyter notebook after downgrading bokeh, In case you see it still doesn't work.

Upvotes: 16

user8512128
user8512128

Reputation: 1

I can produce plots after upgrading to 3.3.0 dev1, which solved my problem temporarily - though I still get the "BokehDeprecationWarning: Passing lists of formats for DatetimeTickFormatter scales was deprecated in Bokeh 3.0."

Upvotes: 0

Mike
Mike

Reputation: 1

Downgrading Bokeh didn't work for me. But, after importing backtesting in Jupyter, I needed to do: backtesting.set_bokeh_output(notebook=False)

The expected plot was then generated in a new interactive browser tab.

Upvotes: 0

I have update Python to version 3.11 & downgrade bokeh to 2.4.3

This worked for me.

Upvotes: 0

Stack
Stack

Reputation: 13

I had a similar issue, using Spyder IDE.

Found out I need to call the below for the plot to show for Spyder.

backtesting.set_bokeh_output(notebook=False)

Upvotes: 0

Related Questions