Skip to content

Commit

Permalink
Adding plot_group argument to indicator constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Vlasenko committed Feb 12, 2025
1 parent 915bd09 commit b7018eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 9 additions & 2 deletions backtesting/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ def __eq__(self, other):

ohlc_colors = colorgen()
indicator_figs = []
plot_group_to_fig = {}

for i, value in enumerate(indicators):
value = np.atleast_2d(value)
Expand All @@ -541,8 +542,14 @@ def __eq__(self, other):
if is_overlay:
fig = fig_ohlc
else:
fig = new_indicator_figure()
indicator_figs.append(fig)
if (value._opts['plot_group'] is not None and
value._opts['plot_group'] in plot_group_to_fig):
fig = plot_group_to_fig[value._opts['plot_group']]
else:
fig = new_indicator_figure()
indicator_figs.append(fig)
if value._opts['plot_group'] is not None:
plot_group_to_fig[value._opts['plot_group']] = fig
tooltips = []
colors = value._opts['color']
colors = colors and cycle(_as_list(colors)) or (
Expand Down
8 changes: 7 additions & 1 deletion backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def _check_params(self, params):
def I(self, # noqa: E743
func: Callable, *args,
name=None, plot=True, overlay=None, color=None, scatter=False,
plot_group=None,
**kwargs) -> np.ndarray:
"""
Declare an indicator. An indicator is just an array of values
Expand All @@ -105,6 +106,11 @@ def I(self, # noqa: E743
candlestick chart. By default, a heuristic is used which decides
correctly most of the time.
For non-overlaying indicators, if `plot_group` is set,
indicators with the same `plot_group` value will be combined
on one chart which is useful for displaying crossing indicators
like Aroon.
`color` can be string hex RGB triplet or X11 color name.
By default, the next available color is assigned.
Expand Down Expand Up @@ -179,7 +185,7 @@ def _format_name(name: str) -> str:
value = _Indicator(value, name=name, plot=plot, overlay=overlay,
color=color, scatter=scatter,
# _Indicator.s Series accessor uses this:
index=self.data.index)
index=self.data.index, plot_group=plot_group)
self._indicators.append(value)
return value

Expand Down

0 comments on commit b7018eb

Please sign in to comment.