Skip to content

Commit

Permalink
ENH: Add bt.plot(reverse_indicators=) param
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Mar 25, 2020
1 parent 9c3357a commit b8da2c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
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 @@ -1290,7 +1290,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 @@ -1349,6 +1349,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 @@ -1379,5 +1382,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 @@ -426,6 +426,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

0 comments on commit b8da2c3

Please sign in to comment.