From 219b94aff05078f220c35ffd893f70dc462e688e Mon Sep 17 00:00:00 2001 From: Katsuhisa Kawaguchi Date: Sun, 21 Jan 2024 15:50:03 +0900 Subject: [PATCH 1/3] remove cumulative input argument and use only compound option --- quantstats/_plotting/core.py | 18 ++++++++---------- quantstats/_plotting/wrappers.py | 4 ---- quantstats/reports.py | 8 +++++--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/quantstats/_plotting/core.py b/quantstats/_plotting/core.py index 4a29f499..3dce7f99 100644 --- a/quantstats/_plotting/core.py +++ b/quantstats/_plotting/core.py @@ -245,7 +245,6 @@ def plot_timeseries( benchmark=None, title="Returns", compound=False, - cumulative=True, fill=False, returns_label="Strategy", hline=None, @@ -279,15 +278,14 @@ def plot_timeseries( returns = (returns / returns.std()) * bmark_vol # --------------- - if compound is True: - if cumulative: - returns = _stats.compsum(returns) - if isinstance(benchmark, _pd.Series): - benchmark = _stats.compsum(benchmark) - else: - returns = returns.cumsum() - if isinstance(benchmark, _pd.Series): - benchmark = benchmark.cumsum() + if compound: + returns = _stats.compsum(returns) + if isinstance(benchmark, _pd.Series): + benchmark = _stats.compsum(benchmark) + else: + returns = returns.cumsum() + if isinstance(benchmark, _pd.Series): + benchmark = benchmark.cumsum() if resample: returns = returns.resample(resample) diff --git a/quantstats/_plotting/wrappers.py b/quantstats/_plotting/wrappers.py index c4a06501..0697d115 100644 --- a/quantstats/_plotting/wrappers.py +++ b/quantstats/_plotting/wrappers.py @@ -381,7 +381,6 @@ def returns( lw=1.5, match_volatility=False, compound=True, - cumulative=True, resample=None, ylabel="Cumulative Returns", subtitle=True, @@ -413,7 +412,6 @@ def returns( log_scale=False, resample=resample, compound=compound, - cumulative=cumulative, lw=lw, figsize=figsize, fontname=fontname, @@ -435,7 +433,6 @@ def log_returns( lw=1.5, match_volatility=False, compound=True, - cumulative=True, resample=None, ylabel="Cumulative Returns", subtitle=True, @@ -470,7 +467,6 @@ def log_returns( log_scale=True, resample=resample, compound=compound, - cumulative=cumulative, lw=lw, figsize=figsize, fontname=fontname, diff --git a/quantstats/reports.py b/quantstats/reports.py index de34b9c3..f2298832 100644 --- a/quantstats/reports.py +++ b/quantstats/reports.py @@ -221,7 +221,7 @@ def html( savefig={"fname": figfile, "format": figfmt}, show=False, ylabel=False, - cumulative=compounded, + compound=compounded, prepare_returns=False, ) tpl = tpl.replace("{{returns}}", _embed_figure(figfile, figfmt)) @@ -236,7 +236,7 @@ def html( savefig={"fname": figfile, "format": figfmt}, show=False, ylabel=False, - cumulative=compounded, + compound=compounded, prepare_returns=False, ) tpl = tpl.replace("{{log_returns}}", _embed_figure(figfile, figfmt)) @@ -253,7 +253,7 @@ def html( savefig={"fname": figfile, "format": figfmt}, show=False, ylabel=False, - cumulative=compounded, + compound=compounded, prepare_returns=False, ) tpl = tpl.replace("{{vol_returns}}", _embed_figure(figfile, figfmt)) @@ -631,6 +631,7 @@ def full( grayscale=grayscale, figsize=figsize, mode="full", + compounded=compounded, periods_per_year=periods_per_year, prepare_returns=False, benchmark_title=benchmark_title, @@ -710,6 +711,7 @@ def basic( grayscale=grayscale, figsize=figsize, mode="basic", + compounded=compounded, periods_per_year=periods_per_year, prepare_returns=False, benchmark_title=benchmark_title, From d274ab536a2ae1658ea194543da35876895a2ca7 Mon Sep 17 00:00:00 2001 From: Katsuhisa Kawaguchi Date: Sun, 21 Jan 2024 22:21:29 +0900 Subject: [PATCH 2/3] [add] compound option as long as each function has its option --- quantstats/_plotting/wrappers.py | 13 +++++++++++-- quantstats/reports.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/quantstats/_plotting/wrappers.py b/quantstats/_plotting/wrappers.py index 0697d115..f12c3922 100644 --- a/quantstats/_plotting/wrappers.py +++ b/quantstats/_plotting/wrappers.py @@ -151,17 +151,26 @@ def snapshot( axes[0].set_ylabel( "Cumulative Return", fontname=fontname, fontweight="bold", fontsize=12 ) + if isinstance(returns, _pd.Series): + if mode.lower() in ["cumsum", "sum"]: + cum_ret = returns.cumsum() * 100 + else: + cum_ret = _stats.compsum(returns) * 100 axes[0].plot( - _stats.compsum(returns) * 100, + cum_ret, color=colors[1], lw=1 if grayscale else lw, zorder=1, ) elif isinstance(returns, _pd.DataFrame): for col in returns.columns: + if mode.lower() in ["cumsum", "sum"]: + cum_ret = returns[col].cumsum() * 100 + else: + cum_ret = _stats.compsum(returns[col]) * 100 axes[0].plot( - _stats.compsum(returns[col]) * 100, + cum_ret, label=col, lw=1 if grayscale else lw, zorder=1, diff --git a/quantstats/reports.py b/quantstats/reports.py index f2298832..f51e93b7 100644 --- a/quantstats/reports.py +++ b/quantstats/reports.py @@ -1292,6 +1292,7 @@ def plots( show=True, ylabel=False, prepare_returns=False, + compound=compounded, ) _plots.log_returns( @@ -1302,6 +1303,7 @@ def plots( show=True, ylabel=False, prepare_returns=False, + compound=compounded, ) if benchmark is not None: @@ -1314,6 +1316,7 @@ def plots( show=True, ylabel=False, prepare_returns=False, + compound=compounded, ) _plots.yearly_returns( @@ -1324,6 +1327,7 @@ def plots( show=True, ylabel=False, prepare_returns=False, + compounded=compounded, ) _plots.histogram( @@ -1334,6 +1338,7 @@ def plots( show=True, ylabel=False, prepare_returns=False, + compounded=compounded, ) small_fig_size = (figsize[0], figsize[0] * 0.35) @@ -1403,6 +1408,7 @@ def plots( show=True, ylabel=False, prepare_returns=False, + compounded=compounded, ) elif isinstance(returns, _pd.DataFrame): for col in returns.columns: @@ -1414,6 +1420,7 @@ def plots( ylabel=False, title=col, prepare_returns=False, + compounded=compounded, ) _plots.drawdown( @@ -1422,6 +1429,7 @@ def plots( figsize=(figsize[0], figsize[0] * 0.4), show=True, ylabel=False, + compound=compounded, ) if isinstance(returns, _pd.Series): @@ -1433,6 +1441,7 @@ def plots( returns_label=returns.name, show=True, ylabel=False, + compounded=compounded, active=active, ) elif isinstance(returns, _pd.DataFrame): @@ -1458,6 +1467,7 @@ def plots( title=returns.name, ylabel=False, prepare_returns=False, + compounded=compounded, ) elif isinstance(returns, _pd.DataFrame): for col in returns.columns: @@ -1469,6 +1479,7 @@ def plots( title=col, ylabel=False, prepare_returns=False, + compounded=compounded, ) From 6429275008055fd0b45da64966d7635e5500bd05 Mon Sep 17 00:00:00 2001 From: Katsuhisa Kawaguchi Date: Sat, 3 Feb 2024 11:15:28 +0900 Subject: [PATCH 3/3] [fix] compounded False --- quantstats/_plotting/wrappers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantstats/_plotting/wrappers.py b/quantstats/_plotting/wrappers.py index f12c3922..48865825 100644 --- a/quantstats/_plotting/wrappers.py +++ b/quantstats/_plotting/wrappers.py @@ -569,7 +569,7 @@ def yearly_returns( if compounded: returns = returns.resample("A").apply(_stats.comp) else: - returns = returns.resample("A").apply(_df.sum) + returns = returns.resample("A").sum() returns = returns.resample("A").last() fig = _core.plot_returns_bars(