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

Update core.py & utils.py #382

Open
wants to merge 2 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
14 changes: 6 additions & 8 deletions quantstats/_plotting/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,9 @@ def plot_timeseries(
benchmark = benchmark.cumsum()

if resample:
returns = returns.resample(resample)
returns = returns.last() if compound is True else returns.sum(axis=0)
returns = returns.resample(resample).last() if compound else returns.resample(resample).sum()
if isinstance(benchmark, _pd.Series):
benchmark = benchmark.resample(resample)
benchmark = benchmark.last() if compound is True else benchmark.sum(axis=0)
benchmark = benchmark.resample(resample).last() if compound else benchmark.resample(resample).sum()
# ---------------

fig, ax = _plt.subplots(figsize=figsize)
Expand Down Expand Up @@ -1013,16 +1011,16 @@ def plot_distribution(
apply_fnc = _stats.comp if compounded else _np.sum

port["Weekly"] = port["Daily"].resample("W-MON").apply(apply_fnc)
port["Weekly"].ffill(inplace=True)
port["Weekly"] = port["Weekly"].ffill()

port["Monthly"] = port["Daily"].resample("ME").apply(apply_fnc)
port["Monthly"].ffill(inplace=True)
port["Monthly"] = port["Monthly"].ffill()

port["Quarterly"] = port["Daily"].resample("QE").apply(apply_fnc)
port["Quarterly"].ffill(inplace=True)
port["Quarterly"] = port["Quarterly"].ffill()

port["Yearly"] = port["Daily"].resample("YE").apply(apply_fnc)
port["Yearly"].ffill(inplace=True)
port["Yearly"] = port["Yearly"].ffill()

fig, ax = _plt.subplots(figsize=figsize)
ax.spines["top"].set_visible(False)
Expand Down
6 changes: 6 additions & 0 deletions quantstats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ def _prepare_returns(data, rf=0.0, nperiods=None):


def download_returns(ticker, period="max", proxy=None):
"""
Period parse in as yfinance format "max" or
as pd.DatetimeIndex([...]) array.
"""
params = {
"tickers": ticker,
"proxy": proxy,
Expand All @@ -245,6 +249,8 @@ def download_returns(ticker, period="max", proxy=None):
}
if isinstance(period, _pd.DatetimeIndex):
params["start"] = period[0]
if len(period) > 1:
params["end"] = period[-1]
else:
params["period"] = period
df = _yf.download(**params)["Close"].pct_change()
Expand Down