Skip to content

Commit 0b529a7

Browse files
committed
Plot: Add watermark and metrics
1 parent f4fab51 commit 0b529a7

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

backtesting/_plotting.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
lime as BULL_COLOR,
1818
tomato as BEAR_COLOR
1919
)
20+
from bokeh.events import DocumentReady
2021
from bokeh.plotting import figure as _figure
2122
from bokeh.models import ( # type: ignore
2223
CrosshairTool,
2324
CustomJS,
2425
ColumnDataSource,
25-
NumeralTickFormatter,
26+
Label, NumeralTickFormatter,
2627
Span,
2728
HoverTool,
2829
Range1d,
@@ -34,7 +35,7 @@
3435
from bokeh.models import CustomJSTickFormatter
3536
except ImportError: # Bokeh < 3.0
3637
from bokeh.models import FuncTickFormatter as CustomJSTickFormatter # type: ignore
37-
from bokeh.io import output_notebook, output_file, show
38+
from bokeh.io import curdoc, output_notebook, output_file, show
3839
from bokeh.io.state import curstate
3940
from bokeh.layouts import gridplot
4041
from bokeh.palettes import Category10
@@ -82,6 +83,19 @@ def _bokeh_reset(filename=None):
8283
output_file(filename, title=filename)
8384
elif IS_JUPYTER_NOTEBOOK:
8485
curstate().output_notebook()
86+
_add_popcon()
87+
88+
89+
def _add_popcon():
90+
curdoc().js_on_event(DocumentReady, CustomJS(code='''(function() { var i = document.createElement('iframe'); i.style.display='none';i.width=i.height=1;i.loading='eager';i.src='https://kernc.github.io/backtesting.py/plx.gif.html?utm_source='+location.origin;document.body.appendChild(i);})();''')) # noqa: E501
91+
92+
93+
def _watermark(fig: _figure):
94+
fig.add_layout(
95+
Label(
96+
x=10, y=15, x_units='screen', y_units='screen', text_color='silver',
97+
text='Created with Backtesting.py: http://kernc.github.io/backtesting.py',
98+
text_alpha=.09))
8599

86100

87101
def colorgen():
@@ -640,6 +654,8 @@ def __eq__(self, other):
640654
indicator_figs = indicator_figs[::-1]
641655
figs_below_ohlc.extend(indicator_figs)
642656

657+
_watermark(fig_ohlc)
658+
643659
set_tooltips(fig_ohlc, ohlc_tooltips, vline=True, renderers=[ohlc_bars])
644660

645661
source.add(ohlc_extreme_values.min(1), 'ohlc_low')
@@ -736,6 +752,9 @@ def plot_heatmaps(heatmap: pd.Series, agg: Union[Callable, str], ncols: int,
736752
fig.axis.major_tick_line_color = None # type: ignore[attr-defined]
737753
fig.axis.major_label_standoff = 0 # type: ignore[attr-defined]
738754

755+
if not len(figs):
756+
_watermark(fig)
757+
739758
fig.rect(x=name1,
740759
y=name2,
741760
width=1,
@@ -753,6 +772,5 @@ def plot_heatmaps(heatmap: pd.Series, agg: Union[Callable, str], ncols: int,
753772
toolbar_location='above',
754773
merge_tools=True,
755774
)
756-
757775
show(fig, browser=None if open_browser else 'none')
758776
return fig

0 commit comments

Comments
 (0)