Skip to content

Commit 776e55a

Browse files
committed
ENH: Add bt.plot(reverse_indicators=) param
1 parent 9fd86da commit 776e55a

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

backtesting/_plotting.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def plot(*, results: pd.Series,
150150
plot_volume=True, plot_drawdown=False,
151151
smooth_equity=False, relative_equity=True,
152152
superimpose=True, resample=True,
153+
reverse_indicators=True,
153154
show_legend=True, open_browser=True):
154155
"""
155156
Like much of GUI code everywhere, this is a mess.
@@ -494,6 +495,7 @@ def __eq__(self, other):
494495
return self is other
495496

496497
ohlc_colors = colorgen()
498+
indicator_figs = []
497499

498500
for i, value in enumerate(indicators):
499501
value = np.atleast_2d(value)
@@ -509,7 +511,7 @@ def __eq__(self, other):
509511
fig = fig_ohlc
510512
else:
511513
fig = new_indicator_figure()
512-
figs_below_ohlc.append(fig)
514+
indicator_figs.append(fig)
513515
tooltips = []
514516
colors = value._opts['color']
515517
colors = colors and cycle(_as_list(colors)) or (
@@ -562,6 +564,7 @@ def __eq__(self, other):
562564
# have the legend only contain text without the glyph
563565
if len(value) == 1:
564566
fig.legend.glyph_width = 0
567+
return indicator_figs
565568

566569
# Construct figure ...
567570

@@ -583,7 +586,10 @@ def __eq__(self, other):
583586

584587
ohlc_bars = _plot_ohlc()
585588
_plot_ohlc_trades()
586-
_plot_indicators()
589+
indicator_figs = _plot_indicators()
590+
if reverse_indicators:
591+
indicator_figs = indicator_figs[::-1]
592+
figs_below_ohlc.extend(indicator_figs)
587593

588594
set_tooltips(fig_ohlc, ohlc_tooltips, vline=True, renderers=[ohlc_bars])
589595

backtesting/backtesting.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
12801280
plot_volume=True, plot_drawdown=False,
12811281
smooth_equity=False, relative_equity=True,
12821282
superimpose: Union[bool, str] = True,
1283-
resample=True,
1283+
resample=True, reverse_indicators=False,
12841284
show_legend=True, open_browser=True):
12851285
"""
12861286
Plot the progression of the last backtest run.
@@ -1339,6 +1339,9 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13391339
used to resample, overriding above numeric limitation.
13401340
Note, all this only works for data with a datetime index.
13411341
1342+
If `reverse_indicators` is `True`, the indicators below the OHLC chart
1343+
are plotted in reverse order of declaration.
1344+
13421345
[Pandas offset string]: \
13431346
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects
13441347
@@ -1369,5 +1372,6 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13691372
relative_equity=relative_equity,
13701373
superimpose=superimpose,
13711374
resample=resample,
1375+
reverse_indicators=reverse_indicators,
13721376
show_legend=show_legend,
13731377
open_browser=open_browser)

backtesting/test/_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ def test_params(self):
421421
resample='1W',
422422
smooth_equity=False,
423423
relative_equity=False,
424+
reverse_indicators=True,
424425
show_legend=False).items():
425426
with self.subTest(param=p[0]):
426427
bt.plot(**dict([p]), filename=f, open_browser=False)

0 commit comments

Comments
 (0)