Skip to content

Commit 50fd012

Browse files
committed
Auto-scale Y-axis for indicators when zooming kernc#356
1 parent 0a76e96 commit 50fd012

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

backtesting/_plotting.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ def __eq__(self, other):
523523
colors = colors and cycle(_as_list(colors)) or (
524524
cycle([next(ohlc_colors)]) if is_overlay else colorgen())
525525
legend_label = LegendStr(value.name)
526+
indicator_max = value.df.max(axis='columns')
527+
indicator_min = value.df.min(axis='columns')
528+
source.add(indicator_max, f'indicator_{i}_range_max')
529+
source.add(indicator_min, f'indicator_{i}_range_min')
526530
for j, arr in enumerate(value, 1):
527531
color = next(colors)
528532
source_name = f'{legend_label}_{i}_{j}'
@@ -584,7 +588,8 @@ def __eq__(self, other):
584588
figs_above_ohlc.append(_plot_drawdown_section())
585589

586590
if plot_pl:
587-
figs_above_ohlc.append(_plot_pl_section())
591+
fig_pl = _plot_pl_section()
592+
figs_above_ohlc.append(fig_pl)
588593

589594
if plot_volume:
590595
fig_volume = _plot_volume_section()
@@ -607,9 +612,15 @@ def __eq__(self, other):
607612

608613
custom_js_args = dict(ohlc_range=fig_ohlc.y_range,
609614
source=source)
615+
if plot_pl:
616+
custom_js_args.update(pl_range=fig_pl.y_range)
610617
if plot_volume:
611618
custom_js_args.update(volume_range=fig_volume.y_range)
612-
619+
indicator_ranges = {}
620+
for idx, indicator in enumerate(indicator_figs):
621+
indicator_range_key = f'indicator_{idx}_range'
622+
indicator_ranges.update({indicator_range_key: indicator.y_range})
623+
custom_js_args.update({'indicator_ranges': indicator_ranges})
613624
fig_ohlc.x_range.js_on_change('end', CustomJS(args=custom_js_args,
614625
code=_AUTOSCALE_JS_CALLBACK))
615626

backtesting/autoscale_cb.js

+13
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,18 @@ window._bt_autoscale_timeout = setTimeout(function () {
3131
max = Math.max.apply(null, source.data['Volume'].slice(i, j));
3232
_bt_scale_range(volume_range, 0, max * 1.03, false);
3333
}
34+
35+
if(indicator_ranges){
36+
let keys = Object.keys(indicator_ranges);
37+
for(var count=0;count<keys.length;count++){
38+
if(keys[count]){
39+
max = Math.max.apply(null, source.data[keys[count]+'_max'].slice(i, j));
40+
min = Math.min.apply(null, source.data[keys[count]+'_min'].slice(i, j));
41+
if(min && max){
42+
_bt_scale_range(indicator_ranges[keys[count]], min, max, true);
43+
}
44+
}
45+
}
46+
}
3447

3548
}, 50);

0 commit comments

Comments
 (0)