Skip to content

Commit aeeabb8

Browse files
committed
ENH: Add bt.plot(reverse_indicators=) param
1 parent 1cf1159 commit aeeabb8

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
@@ -1314,7 +1314,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13141314
plot_volume=True, plot_drawdown=False,
13151315
smooth_equity=False, relative_equity=True,
13161316
superimpose: Union[bool, str] = True,
1317-
resample=True,
1317+
resample=True, reverse_indicators=False,
13181318
show_legend=True, open_browser=True):
13191319
"""
13201320
Plot the progression of the last backtest run.
@@ -1373,6 +1373,9 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
13731373
used to resample, overriding above numeric limitation.
13741374
Note, all this only works for data with a datetime index.
13751375
1376+
If `reverse_indicators` is `True`, the indicators below the OHLC chart
1377+
are plotted in reverse order of declaration.
1378+
13761379
[Pandas offset string]: \
13771380
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects
13781381
@@ -1403,5 +1406,6 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
14031406
relative_equity=relative_equity,
14041407
superimpose=superimpose,
14051408
resample=resample,
1409+
reverse_indicators=reverse_indicators,
14061410
show_legend=show_legend,
14071411
open_browser=open_browser)

backtesting/test/_test.py

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

0 commit comments

Comments
 (0)