Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions backtesting/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def __eq__(self, other):

ohlc_colors = colorgen()
indicator_figs = []
non_overlay_indicator_idxs = []

for i, value in enumerate(indicators):
value = np.atleast_2d(value)
Expand All @@ -518,11 +519,16 @@ def __eq__(self, other):
else:
fig = new_indicator_figure()
indicator_figs.append(fig)
non_overlay_indicator_idxs.append(i)
tooltips = []
colors = value._opts['color']
colors = colors and cycle(_as_list(colors)) or (
cycle([next(ohlc_colors)]) if is_overlay else colorgen())
legend_label = LegendStr(value.name)
indicator_max = value.df.max(axis='columns')
indicator_min = value.df.min(axis='columns')
source.add(indicator_max, f'indicator_{i}_range_max')
source.add(indicator_min, f'indicator_{i}_range_min')
for j, arr in enumerate(value, 1):
color = next(colors)
source_name = f'{legend_label}_{i}_{j}'
Expand Down Expand Up @@ -570,7 +576,7 @@ def __eq__(self, other):
# have the legend only contain text without the glyph
if len(value) == 1:
fig.legend.glyph_width = 0
return indicator_figs
return (indicator_figs, non_overlay_indicator_idxs)

# Construct figure ...

Expand All @@ -584,7 +590,8 @@ def __eq__(self, other):
figs_above_ohlc.append(_plot_drawdown_section())

if plot_pl:
figs_above_ohlc.append(_plot_pl_section())
fig_pl = _plot_pl_section()
figs_above_ohlc.append(fig_pl)

if plot_volume:
fig_volume = _plot_volume_section()
Expand All @@ -595,9 +602,10 @@ def __eq__(self, other):

ohlc_bars = _plot_ohlc()
_plot_ohlc_trades()
indicator_figs = _plot_indicators()
indicator_figs, non_overlay_indicator_idxs = _plot_indicators()
if reverse_indicators:
indicator_figs = indicator_figs[::-1]
non_overlay_indicator_idxs = non_overlay_indicator_idxs[::-1]
figs_below_ohlc.extend(indicator_figs)

set_tooltips(fig_ohlc, ohlc_tooltips, vline=True, renderers=[ohlc_bars])
Expand All @@ -607,9 +615,16 @@ def __eq__(self, other):

custom_js_args = dict(ohlc_range=fig_ohlc.y_range,
source=source)
if plot_pl:
custom_js_args.update(pl_range=fig_pl.y_range)
if plot_volume:
custom_js_args.update(volume_range=fig_volume.y_range)

indicator_ranges = {}
for idx, (indicator,
indicator_idx) in enumerate(zip(indicator_figs, non_overlay_indicator_idxs)):
indicator_range_key = f'indicator_{indicator_idx}_range'
indicator_ranges.update({indicator_range_key: indicator.y_range})
custom_js_args.update({'indicator_ranges': indicator_ranges})
fig_ohlc.x_range.js_on_change('end', CustomJS(args=custom_js_args,
code=_AUTOSCALE_JS_CALLBACK))

Expand Down
13 changes: 13 additions & 0 deletions backtesting/autoscale_cb.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,18 @@ window._bt_autoscale_timeout = setTimeout(function () {
max = Math.max.apply(null, source.data['Volume'].slice(i, j));
_bt_scale_range(volume_range, 0, max * 1.03, false);
}

if(indicator_ranges){
let keys = Object.keys(indicator_ranges);
for(var count=0;count<keys.length;count++){
if(keys[count]){
max = Math.max.apply(null, source.data[keys[count]+'_max'].slice(i, j));
min = Math.min.apply(null, source.data[keys[count]+'_min'].slice(i, j));
if(min && max){
_bt_scale_range(indicator_ranges[keys[count]], min, max, true);
}
}
}
}

}, 50);