From 0581c550e0f654fd683f6fce8fa4db2b7db859cf Mon Sep 17 00:00:00 2001 From: Zeel Patel Date: Sat, 22 May 2021 19:30:20 -0700 Subject: [PATCH 1/6] Auto-scale Y-axis for indicators when zooming #356 --- backtesting/_plotting.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index bec6757a..1b01d385 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -523,6 +523,12 @@ def __eq__(self, other): 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_{j}_range_max') + source.add(indicator_min,f'indicator_{j}_range_min') + for j, arr in enumerate(value, 1): color = next(colors) source_name = f'{legend_label}_{i}_{j}' @@ -609,6 +615,12 @@ def __eq__(self, other): source=source) if plot_volume: custom_js_args.update(volume_range=fig_volume.y_range) + + indicator_ranges = {} + for idx,indicator in enumerate(indicator_figs): + indicator_range_key = f'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)) From 779258895bc8f6836d9b5483b03244c9d156222f Mon Sep 17 00:00:00 2001 From: Zeel Patel Date: Sat, 22 May 2021 19:31:43 -0700 Subject: [PATCH 2/6] Auto-scale Y-axis for indicators when zooming #356 --- backtesting/autoscale_cb.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backtesting/autoscale_cb.js b/backtesting/autoscale_cb.js index da888ecf..63135615 100644 --- a/backtesting/autoscale_cb.js +++ b/backtesting/autoscale_cb.js @@ -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 Date: Sat, 22 May 2021 19:36:10 -0700 Subject: [PATCH 3/6] Auto-scale Y-axis for indicators when zooming #356 --- backtesting/_plotting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index 1b01d385..85c3a251 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -526,8 +526,8 @@ def __eq__(self, other): indicator_max=value.df.max(axis='columns') indicator_min=value.df.min(axis='columns') - source.add(indicator_max, f'indicator_{j}_range_max') - source.add(indicator_min,f'indicator_{j}_range_min') + 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) From c0dcbbedced95f08b61d9789b09a0b316cbbd99a Mon Sep 17 00:00:00 2001 From: Zeel Patel Date: Sat, 22 May 2021 20:19:37 -0700 Subject: [PATCH 4/6] Auto-scale Y-axis for indicators when zooming #356 --- backtesting/_plotting.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index 85c3a251..86c1697a 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -523,12 +523,10 @@ def __eq__(self, other): 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') - + 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}' @@ -619,9 +617,8 @@ def __eq__(self, other): indicator_ranges = {} for idx,indicator in enumerate(indicator_figs): indicator_range_key = f'indicator_{idx}_range' - indicator_ranges.update({indicator_range_key:indicator.y_range}) - custom_js_args.update({'indicator_ranges':indicator_ranges}) - + 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)) From 6b1c436a459d46d9a5e25f07ab232377004f69cf Mon Sep 17 00:00:00 2001 From: Zeel Patel Date: Sat, 22 May 2021 20:23:02 -0700 Subject: [PATCH 5/6] Auto-scale Y-axis for indicators when zooming #356 --- backtesting/_plotting.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index 86c1697a..e22358b0 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -523,8 +523,8 @@ def __eq__(self, other): 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') + 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): @@ -612,10 +612,9 @@ def __eq__(self, other): custom_js_args = dict(ohlc_range=fig_ohlc.y_range, source=source) if plot_volume: - custom_js_args.update(volume_range=fig_volume.y_range) - + custom_js_args.update(volume_range=fig_volume.y_range) indicator_ranges = {} - for idx,indicator in enumerate(indicator_figs): + for idx, indicator in enumerate(indicator_figs): indicator_range_key = f'indicator_{idx}_range' indicator_ranges.update({indicator_range_key: indicator.y_range}) custom_js_args.update({'indicator_ranges': indicator_ranges}) From bf03b63503d28a568b80a440c018e4dc1432f91a Mon Sep 17 00:00:00 2001 From: Zeel Patel Date: Sat, 22 May 2021 20:34:29 -0700 Subject: [PATCH 6/6] Auto-scale Y-axis for indicators when zooming #356 --- backtesting/_plotting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index e22358b0..424dd18a 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -523,8 +523,8 @@ def __eq__(self, other): 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') + 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): @@ -612,7 +612,7 @@ def __eq__(self, other): custom_js_args = dict(ohlc_range=fig_ohlc.y_range, source=source) if plot_volume: - custom_js_args.update(volume_range=fig_volume.y_range) + custom_js_args.update(volume_range=fig_volume.y_range) indicator_ranges = {} for idx, indicator in enumerate(indicator_figs): indicator_range_key = f'indicator_{idx}_range'