Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove cumulative input argument and use only compound option #329

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 8 additions & 10 deletions quantstats/_plotting/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ def plot_timeseries(
benchmark=None,
title="Returns",
compound=False,
cumulative=True,
fill=False,
returns_label="Strategy",
hline=None,
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions quantstats/_plotting/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -381,7 +390,6 @@ def returns(
lw=1.5,
match_volatility=False,
compound=True,
cumulative=True,
resample=None,
ylabel="Cumulative Returns",
subtitle=True,
Expand Down Expand Up @@ -413,7 +421,6 @@ def returns(
log_scale=False,
resample=resample,
compound=compound,
cumulative=cumulative,
lw=lw,
figsize=figsize,
fontname=fontname,
Expand All @@ -435,7 +442,6 @@ def log_returns(
lw=1.5,
match_volatility=False,
compound=True,
cumulative=True,
resample=None,
ylabel="Cumulative Returns",
subtitle=True,
Expand Down Expand Up @@ -470,7 +476,6 @@ def log_returns(
log_scale=True,
resample=resample,
compound=compound,
cumulative=cumulative,
lw=lw,
figsize=figsize,
fontname=fontname,
Expand Down Expand Up @@ -564,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(
Expand Down
19 changes: 16 additions & 3 deletions quantstats/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1290,6 +1292,7 @@ def plots(
show=True,
ylabel=False,
prepare_returns=False,
compound=compounded,
)

_plots.log_returns(
Expand All @@ -1300,6 +1303,7 @@ def plots(
show=True,
ylabel=False,
prepare_returns=False,
compound=compounded,
)

if benchmark is not None:
Expand All @@ -1312,6 +1316,7 @@ def plots(
show=True,
ylabel=False,
prepare_returns=False,
compound=compounded,
)

_plots.yearly_returns(
Expand All @@ -1322,6 +1327,7 @@ def plots(
show=True,
ylabel=False,
prepare_returns=False,
compounded=compounded,
)

_plots.histogram(
Expand All @@ -1332,6 +1338,7 @@ def plots(
show=True,
ylabel=False,
prepare_returns=False,
compounded=compounded,
)

small_fig_size = (figsize[0], figsize[0] * 0.35)
Expand Down Expand Up @@ -1401,6 +1408,7 @@ def plots(
show=True,
ylabel=False,
prepare_returns=False,
compounded=compounded,
)
elif isinstance(returns, _pd.DataFrame):
for col in returns.columns:
Expand All @@ -1412,6 +1420,7 @@ def plots(
ylabel=False,
title=col,
prepare_returns=False,
compounded=compounded,
)

_plots.drawdown(
Expand All @@ -1420,6 +1429,7 @@ def plots(
figsize=(figsize[0], figsize[0] * 0.4),
show=True,
ylabel=False,
compound=compounded,
)

if isinstance(returns, _pd.Series):
Expand All @@ -1431,6 +1441,7 @@ def plots(
returns_label=returns.name,
show=True,
ylabel=False,
compounded=compounded,
active=active,
)
elif isinstance(returns, _pd.DataFrame):
Expand All @@ -1456,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:
Expand All @@ -1467,6 +1479,7 @@ def plots(
title=col,
ylabel=False,
prepare_returns=False,
compounded=compounded,
)


Expand Down