Skip to content

Commit 086247f

Browse files
committed
ENH: Add bt.plot(reverse_indicators=) param
1 parent 1219913 commit 086247f

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

backtesting/_plotting.py

Lines changed: 8 additions & 2 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13151315
plot_volume=True, plot_drawdown=False,
13161316
smooth_equity=False, relative_equity=True,
13171317
superimpose: Union[bool, str] = True,
1318-
resample=True,
1318+
resample=True, reverse_indicators=False,
13191319
show_legend=True, open_browser=True):
13201320
"""
13211321
Plot the progression of the last backtest run.
@@ -1374,6 +1374,9 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13741374
used to resample, overriding above numeric limitation.
13751375
Note, all this only works for data with a datetime index.
13761376
1377+
If `reverse_indicators` is `True`, the indicators below the OHLC chart
1378+
are plotted in reverse order of declaration.
1379+
13771380
[Pandas offset string]: \
13781381
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects
13791382
@@ -1404,5 +1407,6 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
14041407
relative_equity=relative_equity,
14051408
superimpose=superimpose,
14061409
resample=resample,
1410+
reverse_indicators=reverse_indicators,
14071411
show_legend=show_legend,
14081412
open_browser=open_browser)

backtesting/test/_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ def test_params(self):
455455
resample='1W',
456456
smooth_equity=False,
457457
relative_equity=False,
458+
reverse_indicators=True,
458459
show_legend=False).items():
459460
with self.subTest(param=p[0]):
460461
bt.plot(**dict([p]), filename=f, open_browser=False)

0 commit comments

Comments
 (0)