Skip to content

Commit b8da2c3

Browse files
committed
ENH: Add bt.plot(reverse_indicators=) param
1 parent 9c3357a commit b8da2c3

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.
@@ -488,6 +489,7 @@ def __eq__(self, other):
488489
return self is other
489490

490491
ohlc_colors = colorgen()
492+
indicator_figs = []
491493

492494
for i, value in enumerate(indicators):
493495
value = np.atleast_2d(value)
@@ -503,7 +505,7 @@ def __eq__(self, other):
503505
fig = fig_ohlc
504506
else:
505507
fig = new_indicator_figure()
506-
figs_below_ohlc.append(fig)
508+
indicator_figs.append(fig)
507509
tooltips = []
508510
colors = value._opts['color']
509511
colors = colors and cycle(_as_list(colors)) or (
@@ -556,6 +558,7 @@ def __eq__(self, other):
556558
# have the legend only contain text without the glyph
557559
if len(value) == 1:
558560
fig.legend.glyph_width = 0
561+
return indicator_figs
559562

560563
# Construct figure ...
561564

@@ -577,7 +580,10 @@ def __eq__(self, other):
577580

578581
ohlc_bars = _plot_ohlc()
579582
_plot_ohlc_trades()
580-
_plot_indicators()
583+
indicator_figs = _plot_indicators()
584+
if reverse_indicators:
585+
indicator_figs = indicator_figs[::-1]
586+
figs_below_ohlc.extend(indicator_figs)
581587

582588
set_tooltips(fig_ohlc, ohlc_tooltips, vline=True, renderers=[ohlc_bars])
583589

backtesting/backtesting.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
12901290
plot_volume=True, plot_drawdown=False,
12911291
smooth_equity=False, relative_equity=True,
12921292
superimpose: Union[bool, str] = True,
1293-
resample=True,
1293+
resample=True, reverse_indicators=False,
12941294
show_legend=True, open_browser=True):
12951295
"""
12961296
Plot the progression of the last backtest run.
@@ -1349,6 +1349,9 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13491349
used to resample, overriding above numeric limitation.
13501350
Note, all this only works for data with a datetime index.
13511351
1352+
If `reverse_indicators` is `True`, the indicators below the OHLC chart
1353+
are plotted in reverse order of declaration.
1354+
13521355
[Pandas offset string]: \
13531356
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects
13541357
@@ -1379,5 +1382,6 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13791382
relative_equity=relative_equity,
13801383
superimpose=superimpose,
13811384
resample=resample,
1385+
reverse_indicators=reverse_indicators,
13821386
show_legend=show_legend,
13831387
open_browser=open_browser)

backtesting/test/_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def test_params(self):
426426
resample='1W',
427427
smooth_equity=False,
428428
relative_equity=False,
429+
reverse_indicators=True,
429430
show_legend=False).items():
430431
with self.subTest(param=p[0]):
431432
bt.plot(**dict([p]), filename=f, open_browser=False)

0 commit comments

Comments
 (0)