Skip to content

Commit 1ac7290

Browse files
committed
change the way to compute returns
1 parent 57cb7d2 commit 1ac7290

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/empyrial/main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,25 @@ def get_returns(stocks, wts, start_date, end_date=TODAY):
122122
if len(stocks) > 1:
123123
assets = yf.download(stocks, start=start_date, end=end_date, progress=False)["Adj Close"]
124124
assets = assets.filter(stocks)
125-
ret_data = assets.pct_change()[1:]
126-
returns = (ret_data * wts).sum(axis=1)
125+
initial_alloc = wts/assets.iloc[0]
126+
if initial_alloc.isna().any():
127+
raise ValueError("Some stock is not available at initial state!")
128+
portfolio_value = (assets * initial_alloc).sum(axis=1)
129+
returns = portfolio_value.pct_change()[1:]
127130
return returns
128131
else:
129132
df = yf.download(stocks, start=start_date, end=end_date, progress=False)["Adj Close"]
130133
df = pd.DataFrame(df)
131-
returns = df.pct_change()
134+
returns = df.pct_change()[1:]
132135
return returns
133136

134137

135138
def get_returns_from_data(data, wts):
136-
ret_data = data.pct_change()[1:]
137-
returns = (ret_data * wts).sum(axis=1)
139+
initial_alloc = wts/data.iloc[0]
140+
if initial_alloc.isna().any():
141+
raise ValueError("Some stock is not available at initial state!")
142+
portfolio_value = (data * initial_alloc).sum(axis=1)
143+
returns = portfolio_value.pct_change()[1:]
138144
return returns
139145

140146

0 commit comments

Comments
 (0)