diff --git a/CHANGELOG.md b/CHANGELOG.md index 33d92a6..4773470 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Change Log 0.1.13 ------- +- Added ``show_average`` parameter (default ``True``) to + ``plot_support_resistance`` and ``plot_sup_res_date``; set to ``False`` to + hide the average support/resistance lines from the plot (closes #16) - Added upfront validation of the ``accuracy`` parameter in ``calc_support_resistance`` and ``get_extrema``: raises a clear ``ValueError('accuracy must be a positive even integer')`` instead of a diff --git a/tests/test_trendln.py b/tests/test_trendln.py index 43757e3..9c934d2 100644 --- a/tests/test_trendln.py +++ b/tests/test_trendln.py @@ -19,6 +19,7 @@ get_extrema, get_levels, pandas_to_ohlc, + plot_support_resistance, METHOD_NAIVE, METHOD_NAIVECONSEC, METHOD_NUMDIFF, @@ -250,6 +251,42 @@ def test_separate_low_high(self): # Input validation # --------------------------------------------------------------------------- +# --------------------------------------------------------------------------- +# plot_support_resistance options +# --------------------------------------------------------------------------- + +import os +os.environ.setdefault('MPLBACKEND', 'Agg') + +class TestPlotOptions: + """Tests for plot_support_resistance keyword parameters.""" + + def test_show_average_true_returns_figure(self): + import matplotlib + fig = plot_support_resistance(DATA_SIMPLE, show_average=True) + assert isinstance(fig, matplotlib.figure.Figure) + + def test_show_average_false_returns_figure(self): + import matplotlib + fig = plot_support_resistance(DATA_SIMPLE, show_average=False) + assert isinstance(fig, matplotlib.figure.Figure) + + def test_show_average_false_fewer_lines(self): + """With show_average=False the average lines (Avg. Support/Resistance) + should not appear as legend entries.""" + import matplotlib + import matplotlib.pyplot as plt + plt.figure() + fig_with = plot_support_resistance(DATA_SIMPLE, show_average=True) + labels_with = [t.get_text() for t in fig_with.axes[0].get_legend().get_texts()] + plt.figure() + fig_without = plot_support_resistance(DATA_SIMPLE, show_average=False) + labels_without = [t.get_text() for t in fig_without.axes[0].get_legend().get_texts()] + plt.close('all') + assert any('Avg.' in l for l in labels_with) + assert not any('Avg.' in l for l in labels_without) + + class TestValidation: def test_invalid_h_type(self): with pytest.raises(ValueError): diff --git a/trendln/__init__.py b/trendln/__init__.py index 2cc9c12..55f0665 100644 --- a/trendln/__init__.py +++ b/trendln/__init__.py @@ -946,17 +946,20 @@ def _extract(trend): def plot_sup_res_date(hist, idx, numbest = 2, fromwindows = True, pctbound=0.1, extmethod = METHOD_NUMDIFF, method=METHOD_NSQUREDLOGN, window=125, errpct = 0.005, hough_scale=0.01, hough_prob_iter=10, sortError=False, accuracy=2, - title='Prices with Support/Resistance Trend Lines', y_axis_label='Price', series_label=None): + title='Prices with Support/Resistance Trend Lines', y_axis_label='Price', series_label=None, + show_average=True): import matplotlib.ticker as ticker return plot_support_resistance(hist, ticker.FuncFormatter(datefmt(idx)), numbest, fromwindows, pctbound, extmethod, method, window, errpct, hough_scale, hough_prob_iter, sortError, accuracy, - title=title, y_axis_label=y_axis_label, series_label=series_label) + title=title, y_axis_label=y_axis_label, series_label=series_label, + show_average=show_average) def plot_support_resistance(hist, xformatter = None, numbest = 2, fromwindows = True, pctbound=0.1, extmethod = METHOD_NUMDIFF, method=METHOD_NSQUREDLOGN, window=125, errpct = 0.005, hough_scale=0.01, hough_prob_iter=10, sortError=False, accuracy=2, - title='Prices with Support/Resistance Trend Lines', y_axis_label='Price', series_label=None): + title='Prices with Support/Resistance Trend Lines', y_axis_label='Price', series_label=None, + show_average=True): import matplotlib.pyplot as plt import matplotlib.ticker as ticker ret = calc_support_resistance(hist, extmethod, method, window, errpct, hough_scale, hough_prob_iter, sortError, accuracy) @@ -991,7 +994,8 @@ def plot_support_resistance(hist, xformatter = None, numbest = 2, fromwindows = plt.plot(range(len_h), hist[1 if hist[0] is None else 0], 'k--', label= ('High' if hist[0] is None else 'Low') + ' Price') for h, idxs, pm, clrp, lbl, clrl in disp: plt.plot(idxs, [h[x] for x in idxs], clrp) - plt.plot([0, len_h-1],[pm[1],pm[0] * (len_h-1) + pm[1]],clrl, label=lbl) + if show_average: + plt.plot([0, len_h-1],[pm[1],pm[0] * (len_h-1) + pm[1]],clrl, label=lbl) def add_trend(h, trend, lbl, clr, bFirst): for ln in trend[:numbest]: maxx = ln[0][-1]+1