Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: Add bt.plot(reverse_indicators=) param
  • Loading branch information
kernc committed Jul 13, 2020
commit 086247f201363c8952f133d1b9bbb7d252f12921
10 changes: 8 additions & 2 deletions backtesting/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def plot(*, results: pd.Series,
plot_volume=True, plot_drawdown=False,
smooth_equity=False, relative_equity=True,
superimpose=True, resample=True,
reverse_indicators=True,
show_legend=True, open_browser=True):
"""
Like much of GUI code everywhere, this is a mess.
Expand Down Expand Up @@ -488,6 +489,7 @@ def __eq__(self, other):
return self is other

ohlc_colors = colorgen()
indicator_figs = []

for i, value in enumerate(indicators):
value = np.atleast_2d(value)
Expand All @@ -503,7 +505,7 @@ def __eq__(self, other):
fig = fig_ohlc
else:
fig = new_indicator_figure()
figs_below_ohlc.append(fig)
indicator_figs.append(fig)
tooltips = []
colors = value._opts['color']
colors = colors and cycle(_as_list(colors)) or (
Expand Down Expand Up @@ -556,6 +558,7 @@ def __eq__(self, other):
# have the legend only contain text without the glyph
if len(value) == 1:
fig.legend.glyph_width = 0
return indicator_figs

# Construct figure ...

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

ohlc_bars = _plot_ohlc()
_plot_ohlc_trades()
_plot_indicators()
indicator_figs = _plot_indicators()
if reverse_indicators:
indicator_figs = indicator_figs[::-1]
figs_below_ohlc.extend(indicator_figs)

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

Expand Down
6 changes: 5 additions & 1 deletion backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
plot_volume=True, plot_drawdown=False,
smooth_equity=False, relative_equity=True,
superimpose: Union[bool, str] = True,
resample=True,
resample=True, reverse_indicators=False,
show_legend=True, open_browser=True):
"""
Plot the progression of the last backtest run.
Expand Down Expand Up @@ -1374,6 +1374,9 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
used to resample, overriding above numeric limitation.
Note, all this only works for data with a datetime index.

If `reverse_indicators` is `True`, the indicators below the OHLC chart
are plotted in reverse order of declaration.

[Pandas offset string]: \
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#dateoffset-objects

Expand Down Expand Up @@ -1404,5 +1407,6 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
relative_equity=relative_equity,
superimpose=superimpose,
resample=resample,
reverse_indicators=reverse_indicators,
show_legend=show_legend,
open_browser=open_browser)
1 change: 1 addition & 0 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ def test_params(self):
resample='1W',
smooth_equity=False,
relative_equity=False,
reverse_indicators=True,
show_legend=False).items():
with self.subTest(param=p[0]):
bt.plot(**dict([p]), filename=f, open_browser=False)
Expand Down