Skip to content

Commit

Permalink
feat: add wealth_index_with_assets to Portfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
chilango74 committed Sep 20, 2024
1 parent ea32d18 commit 733dca3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
12 changes: 6 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
rebalancing_period="year",
)

link = pf.okamaio_link
# Fixed Percentage strategy
pc = ok.PercentageStrategy(pf)
pc.frequency = "year"
Expand All @@ -28,8 +27,8 @@
# Fixed Amount strategy
ind = ok.IndexationStrategy(pf)
ind.initial_investment = 100
ind.frequency = "month"
ind.amount = -0.5
ind.frequency = "year"
ind.amount = -0.5 * 12
ind.indexation = "inflation"

# TimeSeries strategy
Expand All @@ -45,7 +44,7 @@
# Assign a strategy
pf.dcf.cashflow_parameters = ind
pf.dcf.discount_rate = 0.10
pf.dcf.use_discounted_values = True
pf.dcf.use_discounted_values = False

# df = pf.dcf.wealth_index

Expand All @@ -67,10 +66,11 @@
# print(w)


# df = pf.dcf.monte_carlo_wealth
df = pf.dcf.wealth_with_assets
df.plot()
# print("portfolio balance \n", df.iloc[-1, :].describe())

pf.dcf.plot_forecast_monte_carlo(backtest=True)
# pf.dcf.plot_forecast_monte_carlo(backtest=True)



Expand Down
58 changes: 51 additions & 7 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2430,21 +2430,17 @@ def set_mc_parameters(self,
@property
def wealth_index(self) -> pd.DataFrame:
"""
Calculate wealth index time series for the portfolio with contributions and
withdrawals.
Calculate wealth index time series for the portfolio with cash flow (contributions and
withdrawals).
Wealth index (Cumulative Wealth Index) is a time series that presents the value of portfolio over
historical time period considering cash flows.
Accumulated inflation time series is added if `inflation=True` in the Portfolio.
If there are no cashflows, Wealth index is obtained from the accumulated return multiplicated
If there is no cash flow, Wealth index is obtained from the accumulated return multiplicated
by the initial investments. That is: initial_amount_pv * (Acc_Return + 1)
initial_amount_pv is the discounted value of the initial investments (initial_amount).
Values of the wealth index correspond to the beginning of the month.
Returns
-------
Time series of wealth index values for portfolio and accumulated inflation.
Expand All @@ -2468,6 +2464,54 @@ def wealth_index(self) -> pd.DataFrame:
self._wealth_index = self.parent._make_df_if_series(df)
return self._wealth_index

@property
def wealth_index_with_assets(self) -> pd.DataFrame:
"""
Calculate wealth index time series for the portfolio and all assets considering cash flow (contributions and
withdrawals).
Wealth index (Cumulative Wealth Index) is a time series that presents the value of portfolio over
historical time period. Accumulated inflation time series is added if `inflation=True` in the Portfolio.
Wealth index is obtained from the accumulated return multiplicated by the initial investments.
initial_amount_pv * (Acc_Return + 1)
If there is no cash flow, Wealth index is obtained from the accumulated return multiplicated
by the initial investments. That is: initial_amount_pv * (Acc_Return + 1)
Returns
-------
DataFrame
Time series of wealth index values for portfolio, each asset and accumulated inflation.
Examples
--------
>>> import matplotlib.pyplot as plt
>>> pf = ok.Portfolio(['VOO.US', 'GLD.US'], weights=[0.8, 0.2])
>>> ind = ok.IndexationStrategy(pf) # Set Cash Flow Strategy parameters
>>> ind.initial_investment = 100 # initial investments value
>>> ind.frequency = "year" # withdrawals frequency
>>> ind.amount = -0.5 * 12 # initital withdrawals amount
>>> ind.indexation = "inflation" # the idexation is equal to inflation
>>> pf.dcf.cashflow_parameters = ind # assign the strategy to Portfolio
>>> pf.dcf.wealth_index_with_assets.plot()
>>> plt.show()
"""
ls = [self.parent.ror, self.parent.assets_ror]
if hasattr(self.parent, "inflation"):
ls.append(self.parent.inflation_ts)
ror_df = pd.concat(ls, axis=1, join="inner", copy="false")
wealth_df = ror_df.apply(
helpers.Frame.get_wealth_indexes_with_cashflow,
axis=0,
args=(
None, # symbol
None, # inflation_symbol
self.cashflow_parameters,
),
)
return wealth_df

@property
def survival_period_hist(self) -> float:
"""
Expand Down

0 comments on commit 733dca3

Please sign in to comment.