Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bt.plot not working #1150

Unanswered
JJTittles asked this question in Q&A
Jun 18, 2024 · 4 comments · 4 replies
Discussion options

Hi, I have recently downloaded backtesting.py and was using the sample sma crossover strategy provided. I am using the spyder GUI. The code ran and stats generated but the bt.plot() generate a number of errors and did not generate a chart. Several sources have pointed to compatibility issues with the Bokeh graphics library. I have uninstalled and reinstalled both Bokeh 3.2.1 and 3.1.1 versions and neither seems to work.
While the stats are nice if I cant get the graphing to work it is not much use to me. and I will need to try another backtesting library.

You must be logged in to vote

Replies: 4 comments 4 replies

Comment options

@JJTittles; I can assure you that the plotting function works just fine. Here is a chart that I just ran.
Screenshot 2024年06月22日 110916

I'm running the backtesting.py library from pycharm without any problems, as well as a range of many other bokeh plots. If you'd like help getting it working, you're going to need to do better than "your thing doesn't work, so I'm throwing my toys out of the cot". If you can describe what you've tried, what you're seeing and what errors are appearing, it might be possible for others to offer their free time to assist you.

You must be logged in to vote
0 replies
Comment options

Thanks Paul for replying, unfortunately some of my frustrations came through in my e-mail. Below is the code I was running along with output stats and warning/error message. It runs ok until it tries to plot the results in graph form. I am running Bokeh (3.1.1) and python (3.11). I have tried is other a versions of Bokeh (3.4.1,/3,2.1). with no success.. Which one should should I be running with the library? Thanks Patrick --------------------------------Program-------------------------------------------------------------------- from backtesting import Backtest, Strategy from backtesting.lib import crossover from backtesting.test import SMA, GOOG class SmaCross(Strategy): n1 = 10 n2 = 20 def init(self): close = self.data.Close self.sma1 = self.I(SMA, close, self.n1) self.sma2 = self.I(SMA, close, self.n2) def next(self): if crossover(self.sma1, self.sma2): self.buy() elif crossover(self.sma2, self.sma1): self.sell() bt = Backtest(GOOG, SmaCross,cash=10000, commission=.002, exclusive_orders=True) stats = bt.run() print(stats) bt.plot()
...
-------------------------------------------Stats output------------------------------------------------------------------------ runfile('E:/2GB HD Backup/User Files/Python Program files (.py)/Test_backtester_code.py', wdir='E:/2GB HD Backup/User Files/Python Program files (.py)') Start 2004年08月19日 00:00:00 End 2013年03月01日 00:00:00 Duration 3116 days 00:00:00 Exposure Time [%] 97.067039 Equity Final [$] 68221.96986 Equity Peak [$] 68991.21986 Return [%] 582.219699 Buy & Hold Return [%] 703.458242 Return (Ann.) [%] 25.266427 Volatility (Ann.) [%] 38.383008 Sharpe Ratio 0.658271 Sortino Ratio 1.288779 Calmar Ratio 0.763748 Max. Drawdown [%] -33.082172 Avg. Drawdown [%] -5.581506 Max. Drawdown Duration 688 days 00:00:00 Avg. Drawdown Duration 41 days 00:00:00 # Trades 94 Win Rate [%] 54.255319 Best Trade [%] 57.11931 Worst Trade [%] -16.629898 Avg. Trade [%] 2.074326 Max. Trade Duration 121 days 00:00:00 Avg. Trade Duration 33 days 00:00:00 Profit Factor 2.190805 Expectancy [%] 2.606294 SQN 1.990216 _strategy SmaCross _equity_curve ... _trades Size EntryB... dtype: object
--------------------------------------------------------------error/warning message ----------------------------------------------------------------------------------------------------- BokehDeprecationWarning: Passing lists of formats for DatetimeTickFormatter scales was deprecated in Bokeh 3.0. Configure a single string format for each scale C:\Users\User\anaconda3\Lib\site-packages\backtesting\_plotting.py:250: UserWarning: DatetimeFormatter scales now only accept a single format. Using the first provided: '%d %b' formatter=DatetimeTickFormatter(days=['%d %b', '%a %d'], BokehDeprecationWarning: Passing lists of formats for DatetimeTickFormatter scales was deprecated in Bokeh 3.0. Configure a single string format for each scale C:\Users\User\anaconda3\Lib\site-packages\backtesting\_plotting.py:250: UserWarning: DatetimeFormatter scales now only accept a single format. Using the first provided: '%m/%Y' formatter=DatetimeTickFormatter(days=['%d %b', '%a %d'],
On 2024年6月21日 at 21:16, Paul Ryder ***@***.***> wrote: @JJTittles <https://github.com/JJTittles>; I can assure you that the plotting function works just fine. Here is a chart that I just ran. Screenshot.2024年06月22日.110916.png (view on web) <https://github.com/kernc/backtesting.py/assets/5959696/ef22d39d-42ed-44c1-b54e-133b80dfa9d1> I'm running the backtesting.py library from pycharm without any problems, as well as a range of many other bokeh plots. If you'd like help getting it working, you're going to need to do better than "your thing doesn't work, so I'm throwing my toys out of the cot". If you can describe what you've tried, what you're seeing and what errors are appearing, it might be possible for others to offer their free time to assist you. — Reply to this email directly, view it on GitHub <#1150 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/APNLQRTMKRYS27RQVJJWTZTZITF6DAVCNFSM6AAAAABJQ7GJWCVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TQNBUGY3DA> . You are receiving this because you were mentioned.Message ID: ***@***.***>
You must be logged in to vote
0 replies
Comment options

Hey @JJTittles; no worries. Writing code can be a lonely and frustrating road. I'm a complete noob, not a software eng, so I understand how you feel.

Noting the above...your error looks like its coming from Bokeh itself. If you navigate through backtesting.py to _plotting.py in your IDE, on about line 250 you'll see something like:

if is_datetime_index: fig_ohlc.xaxis.formatter = CustomJSTickFormatter( args=dict(axis=fig_ohlc.xaxis[0], formatter=DatetimeTickFormatter(days=['%d %b', '%a %d'] months=['%m/%Y', "%b'%y"]), source=source)

Bokeh now doesn't like inputs to DatetimeTickFormatter to be a list.

Go into this line of code, comment out the list input (so you can go back) and instead write:
formatter=DatetimeTickFormatter(days='%a, %d %b', # ["%d %b', '%a %d"] months='%m/%Y'), # ["%m/%Y', "%b'%y"]

This should work for you. Let me know how you go. As a tip, I paid the 10ドル/mth for GitHub CoPilot...for someone like me who is clueless with code, its saved a lot of time. Works directly from with pycharm, or whatever IDE you use.

You must be logged in to vote
3 replies
Comment options

...the months input is on a new line. I can never get markdown to show the format right.

Comment options

Dear Rydo111,
Thanks. I struggled for hours to figure out the error....
Your tip saved me. Thanks a ton Bro.

Comment options

Thank you Rydo111

Comment options

Thanks for the tip!
...
On 2024年6月28日 at 20:53, Paul Ryder ***@***.***> wrote: Hey @JJTittles <https://github.com/JJTittles>; no worries. Writing code can be a lonely and frustrating road. I'm a complete noob, not a software eng, so I understand how you feel. Noting the above...your error looks like its coming from Bokeh itself. If you navigate through backtesting.py to _plotting.py in your IDE, on about line 250 you'll see something like: if is_datetime_index: fig_ohlc.xaxis.formatter = CustomJSTickFormatter( args=dict(axis=fig_ohlc.xaxis[0], formatter=DatetimeTickFormatter(days=['%d %b', '%a %d'] months=['%m/%Y', "%b'%y"]), source=source) Bokeh now doesn't like inputs to DatetimeTickFormatter to be a list. Go into this line of code, comment out the list input (so you can go back) and instead write: formatter=DatetimeTickFormatter(days='%a, %d %b', # ['%d %b', '%a %d]' months='%m/%Y'), # ['%m/%Y', "%b'%y"] This should work for you. Let me know how you go. As a tip, I paid the 10ドル/mth for GitHub CoPilot...for someone like me who is clueless with code, its saved a lot of time. Works directly from with pycharm, or whatever IDE you use. — Reply to this email directly, view it on GitHub <#1150 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/APNLQRWFAXLVPQRAM6QNS63ZJYARJAVCNFSM6AAAAABJQ7GJWCVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TSMBZGY3TI> . You are receiving this because you were mentioned.Message ID: ***@***.***>
You must be logged in to vote
1 reply
Comment options

Worked for me thanks alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /