Skip to content

Commit 4128b12

Browse files
committed
ENH: Do plot plot=False, overlay=True indicators, but muted
1 parent 91e5558 commit 4128b12

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

backtesting/_plotting.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,19 @@ def __eq__(self, other):
549549

550550
for i, value in enumerate(indicators):
551551
value = np.atleast_2d(value)
552+
if _too_many_dims(value):
553+
continue
552554

553555
# Use .get()! A user might have assigned a Strategy.data-evolved
554556
# _Array without Strategy.I()
555-
if not value._opts.get('plot') or _too_many_dims(value):
557+
is_overlay = value._opts.get('overlay')
558+
is_scatter = value._opts.get('scatter')
559+
is_muted = not value._opts.get('plot')
560+
561+
# is overlay => show muted, hide legend item. non-overlay => don't show at all
562+
if is_muted and not is_overlay:
556563
continue
557564

558-
is_overlay = value._opts['overlay']
559-
is_scatter = value._opts['scatter']
560565
if is_overlay:
561566
fig = fig_ohlc
562567
else:
@@ -581,30 +586,31 @@ def __eq__(self, other):
581586
arr = arr.astype(int)
582587
source.add(arr, source_name)
583588
tooltips.append(f'@{{{source_name}}}{{0,0.0[0000]}}')
589+
kwargs = {}
590+
if not is_muted:
591+
kwargs['legend_label'] = legend_labels[j]
584592
if is_overlay:
585593
ohlc_extreme_values[source_name] = arr
586594
if is_scatter:
587-
fig.circle(
595+
r2 = fig.circle(
588596
'index', source_name, source=source,
589-
legend_label=legend_labels[j], color=color,
590-
line_color='black', fill_alpha=.8,
591-
radius=BAR_WIDTH / 2 * .9)
597+
color=color, line_color='black', fill_alpha=.8,
598+
radius=BAR_WIDTH / 2 * .9, **kwargs)
592599
else:
593-
fig.line(
600+
r2 = fig.line(
594601
'index', source_name, source=source,
595-
legend_label=legend_labels[j], line_color=color,
596-
line_width=1.3)
602+
line_color=color, line_width=1.4 if is_muted else 1.5, **kwargs)
603+
# r != r2
604+
r2.muted = is_muted
597605
else:
598606
if is_scatter:
599607
r = fig.circle(
600608
'index', source_name, source=source,
601-
legend_label=legend_labels[j], color=color,
602-
radius=BAR_WIDTH / 2 * .6)
609+
color=color, radius=BAR_WIDTH / 2 * .6, **kwargs)
603610
else:
604611
r = fig.line(
605612
'index', source_name, source=source,
606-
legend_label=legend_labels[j], line_color=color,
607-
line_width=1.3)
613+
line_color=color, line_width=1.3, **kwargs)
608614
# Add dashed centerline just because
609615
mean = try_(lambda: float(pd.Series(arr).mean()), default=np.nan)
610616
if not np.isnan(mean) and (abs(mean) < .1 or

backtesting/backtesting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _format_name(name: str) -> str:
164164
f'length as `data` (data shape: {self._data.Close.shape}; indicator "{name}" '
165165
f'shape: {getattr(value, "shape", "")}, returned value: {value})')
166166

167-
if plot and overlay is None and np.issubdtype(value.dtype, np.number):
167+
if overlay is None and np.issubdtype(value.dtype, np.number):
168168
x = value / self._data.Close
169169
# By default, overlay if strong majority of indicator values
170170
# is within 30% of Close

0 commit comments

Comments
 (0)