Bokeh Integration - Interactive Webbrowser Plotting
-
Please provide stacktrace and environment information. And please add your code if you did introduce modifications of any kind to the minimal example.
-
@vbs I think my issue is similar to @杨一帆 . I have a MA cross strategy class defined in my code. I read from here (https://github.com/verybadsoldier/backtrader_plotting/issues/59) mentioning about the inability of inspect module to read the source of the class in Jupyter notebook. So the way for now is to remove the inspect module? How do we do so and how does it impact the accuracy or completeness of the plotting? Sorry, my questions might sound silly as I'm very new to python.
~\anaconda3\envs\env_backtrader\lib\inspect.py in getfile(object) 651 if getattr(module, '__file__', None): 652 return module.__file__ --> 653 raise TypeError('{!r} is a built-in class'.format(object)) 654 if ismethod(object): 655 object = object.__func__ TypeError: <class '__main__.HmaCross'> is a built-in class
-
Please try to comment out these two lines in the
backtrader_plotting
sources:md += 'Source Code:\n' md += f'\n```\n{inspect.getsource(strategy.__class__)}\n```\n\n'
I will provide a config option in future versions.
-
@vbs tks for your reply. I tried commenting out the lines under metadata.py but still having the same error. Full stacktrace provided below:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-e28863b962f3> in <module> 250 # plot_mode= single/multi ; scheme=Blackly/Tradimo 251 b = Bokeh(style='bar', plot_mode='single', scheme=Tradimo()) --> 252 cerebro.plot(b) 253 # cerebro.plot(iplot=False) ~\anaconda3\envs\env_backtrader\lib\site-packages\backtrader\cerebro.py in plot(self, plotter, numfigs, iplot, start, end, width, height, dpi, tight, use, **kwargs) 994 figs.append(rfig) 995 --> 996 plotter.show() 997 998 return figs ~\anaconda3\envs\env_backtrader\lib\site-packages\backtrader_plotting\bokeh\bokeh.py in show(self) 525 # as the plot() function only created the figures and the columndatasources with no data -> now we fill it 526 for idx in range(len(self.figurepages)): --> 527 model = self.generate_model(idx) 528 529 if self.p.output_mode in ['show', 'save']: ~\anaconda3\envs\env_backtrader\lib\site-packages\backtrader_plotting\bokeh\bokeh.py in generate_model(self, figurepage_idx) 311 if not self._is_optreturn: 312 assert figurepage.strategy is not None --> 313 meta = Div(text=metadata.get_metadata_div(figurepage.strategy)) 314 metapanel = Panel(child=meta, title="Meta") 315 tabs.append(metapanel) ~\anaconda3\envs\env_backtrader\lib\site-packages\backtrader_plotting\html\metadata.py in get_metadata_div(strategy) 98 def get_metadata_div(strategy: bt.Strategy) -> str: 99 md = "" --> 100 101 md += _get_strategy(strategy) 102 md += '* * *' ~\anaconda3\envs\env_backtrader\lib\site-packages\backtrader_plotting\html\metadata.py in _get_strategy(strategy) 67 68 # md += 'Source Code:\n' ---> 69 # md += f'\n```\n{inspect.getsource(strategy.__class__)}\n```\n\n' 70 71 ~\anaconda3\envs\env_backtrader\lib\inspect.py in getsource(object) 971 or code object. The source code is returned as a single string. An 972 OSError is raised if the source code cannot be retrieved.""" --> 973 lines, lnum = getsourcelines(object) 974 return ''.join(lines) 975 ~\anaconda3\envs\env_backtrader\lib\inspect.py in getsourcelines(object) 953 raised if the source code cannot be retrieved.""" 954 object = unwrap(object) --> 955 lines, lnum = findsource(object) 956 957 if istraceback(object): ~\anaconda3\envs\env_backtrader\lib\inspect.py in findsource(object) 766 is raised if the source code cannot be retrieved.""" 767 --> 768 file = getsourcefile(object) 769 if file: 770 # Invalidate cache if needed. ~\anaconda3\envs\env_backtrader\lib\inspect.py in getsourcefile(object) 682 Return None if no way can be identified to get the source. 683 """ --> 684 filename = getfile(object) 685 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:] 686 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:] ~\anaconda3\envs\env_backtrader\lib\inspect.py in getfile(object) 651 if getattr(module, '__file__', None): 652 return module.__file__ --> 653 raise TypeError('{!r} is a built-in class'.format(object)) 654 if ismethod(object): 655 object = object.__func__ TypeError: <class '__main__.HmaCross'> is a built-in class
-
@kian-hong-Tan said in Bokeh Integration - Interactive Webbrowser Plotting:
@vbs tks for your reply. I tried commenting out the lines under metadata.py but still having the same error. Full stacktrace provided below:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\anaconda3\envs\env_backtrader\lib\site-packages\backtrader_plotting\html\metadata.py in _get_strategy(strategy) 67 68 # md += 'Source Code:\n' ---> 69 # md += f'\n```\n{inspect.getsource(strategy.__class__)}\n```\n\n' 70 71
Well, according to your stacktrace the code gets execute even though commented out :) I have no good idea about that, sorry. I would suggest to debug that. That line should not be executed.
-
@vbs i uninstall and re-install everything because of some other issue. Then I run again and it works this time. :) Tks
-
Hello @vbs , sorry to bother you again. Would like to ask you another question. The Interactive Plot is now shown in the cell (jupyter notebook) instead of a new browser. Is this the expected behavior? Below is my code. I have tried all output mode - show, memory, save, but I also couldn't see any file being saved.
b = Bokeh(filename='backtrader_plotting.html', style='bar', plot_mode='single', scheme=Blackly(),output_mode='show') cerebro.plot(b)
-
Hi @vbs, Excellent work! Really grateful for this nice tool. I do have a question about zoom, how can I do that? I see in the earlier replies you said there will be a toolbar on the left, but I don't see one in my plots.
I see from Bokeh's help that you can do this, but don't know where to add it to Bokeh in your model.
from bokeh.models import BoxSelectTool plot = figure(tools="pan,wheel_zoom,box_zoom,reset") plot.add_tools(BoxSelectTool(dimensions="width"))
This is what I see in Chrome. I can pan the plot and see tooltips OK. But no toolbar appearing on the plot.
-
@kian-hong-Tan said in Bokeh Integration - Interactive Webbrowser Plotting:
Hello @vbs , sorry to bother you again. Would like to ask you another question. The Interactive Plot is now shown in the cell (jupyter notebook) instead of a new browser. Is this the expected behavior? Below is my code. I have tried all output mode - show, memory, save, but I also couldn't see any file being saved.
Sorry I have never used notebooks and cannot help here.
@Yang-Fu said in Bokeh Integration - Interactive Webbrowser Plotting:
Hi @vbs, Excellent work! Really grateful for this nice tool. I do have a question about zoom, how can I do that? I see in the earlier replies you said there will be a toolbar on the left, but I don't see one in my plots.
Take a look at one of the demos please:
https://verybadsoldier.github.io/backtrader_plotting/demos/blackly_single.htmlYou will find the toolbar on the right (a bit cropped sadly):
-
@vbs Thank you for the quick reply! I did look on the right side of the plot, but no toolbar there. Also it doesn't plot in tabs even if I use the "multi" option. feel like something weird is going on with my setup. I am using windows10 + bokeh 2.0.2 + backtrader_plotting 1.1.0.
I do see in backtrader_plotting requirements says bokeh ~= 2.0.0. Wondering if anything in 2.0.2 breaks it?
Requirement already satisfied: bokeh~=2.0.0 in c:\users\yang fu\appdata\roaming\python\python37\site-packages (from backtrader_plotting) (2.0.2)
b = Bokeh(style='bar', plot_mode='multi', scheme=Tradimo()) cerebro.plot(b)
-
@vbs I have found a solution by using cerebro.plot(b,iplot=False), as suggested from this link https://community.backtrader.com/topic/1563/plot-doesn-t-show-up-when-running-the-code/2 . It looks great now. tks!
b = Bokeh(filename='backtrader_plotting.html', style='bar', plot_mode='single', scheme=Blackly(),output_mode='show') cerebro.plot(b,iplot=False)
-
@Yang-Fu said in Bokeh Integration - Interactive Webbrowser Plotting:
@vbs Thank you for the quick reply! I did look on the right side of the plot, but no toolbar there. Also it doesn't plot in tabs even if I use the "multi" option. feel like something weird is going on with my setup. I am using windows10 + bokeh 2.0.2 + backtrader_plotting 1.1.0.
I do see in backtrader_plotting requirements says bokeh ~= 2.0.0. Wondering if anything in 2.0.2 breaks it?
Requirement already satisfied: bokeh~=2.0.0 in c:\users\yang fu\appdata\roaming\python\python37\site-packages (from backtrader_plotting) (2.0.2)
b = Bokeh(style='bar', plot_mode='multi', scheme=Tradimo()) cerebro.plot(b)
I dont't think that Bokeh 2.0.2 breaks it. Cannot say whats wrong there. I would suggest you start with a new/clean Python environment to test with.
-
@vbs Finally made it work. It appears that I can only get the toolbar to show up with "left/above/below" locations, not "right" location. Don't know why but it works, oh well... Thanks again for the package. It is really nice to have an interactive plotting tool. The zoom and pan feels great!
Tried different versions of bokeh and its dependencies, python version, also a new PC with Ubuntu/win10, different browser, anaconda or not. They all have the same behavior.
b = Bokeh(style='bar', tabs='multi', scheme=Tradimo(), filename='test_bt_plot.html', toolbar_location='left')
-
Well, that is interesting. The point is that we are talking about a static HTML-file. So I am wondering if the HTML is correct but you still don't see the toolbar if you the HTML you generate differs from mine.
Do you see the toolbar when you navigate your browser here?
https://verybadsoldier.github.io/backtrader_plotting/demos/blackly_single.htmlCould you please run the demo "blackly_single" and send me the output HTML?
https://github.com/verybadsoldier/backtrader_plotting/blob/master/demos/blackly_single.py -
Hi @vbs
When i add resample data(5min) based on original data(1min) then do the plotting using Bokeh, it won't show the resample(5min) graph,
is it an expected behavior? Thanks! -
I think that highly depends on how you implemented it but in general I would say: no.
-
@vbs still great thanks to your contributions on making the plotting much nicer! it seems if I use a single replaydata instead of origin + resample data, backtrader_plotting would do the plotting as usual :-)
-
If you think there is a bug you can always post minimal runnable code that reproduces the problem and I will look into it.
-
@vbs Sorry for not replying sooner. Got swamped with work.
Yes, I can see the toolbar on the demo page of github. And it is also correct when I run the demo "blackly_single". I have generated the htmls, but how can I send them to you? I don't see a message/mail function on the forum. -
A general question: Does someone know how to edit posts on this forums? Quite sure a while ago I managed to do that. Did something change or ist it generally not possible anymore?