Skip to content

Commit

Permalink
feat: #64 add dividend_yeld_annual property in Portfolio like the sam…
Browse files Browse the repository at this point in the history
…e property in the AssetList
  • Loading branch information
vkhomyakov committed Dec 26, 2023
1 parent 2a9af2e commit 85e00c7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#pf.wealth_index_with_assets.plot()
#plt.show()


import matplotlib.pyplot as plt
pf = ok.Portfolio(['SPY.US', 'BND.US'], ccy='USD', last_date='07-2021', rebalancing_period="monthly")
pf.dividends_annual.plot(kind='bar')
pf = ok.Portfolio(['SPY.US', 'BND.US'], ccy='USD', last_date='07-2021')
pf.dividend_yield_annual.plot(kind='bar')
plt.show()
2 changes: 1 addition & 1 deletion okama/asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def dividend_yield_annual(self):
--------
>>> import matplotlib.pyplot as plt
>>> x = ok.AssetList(['T.US', 'XOM.US'], first_date='2010-01', last_date='2020-12')
>>> x.dividends_annual.plot(kind='bar')
>>> x.dividend_yield_annual.plot(kind='bar')
>>> plt.show()
"""
return self._assets_dividend_yield.resample(rule="Y").last()
Expand Down
36 changes: 36 additions & 0 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,42 @@ def dividends_annual(self) -> pd.DataFrame:
"""
return self._get_assets_dividends().resample("Y").sum()

@property
def dividend_yield_annual(self):
"""
Calculate last twelve months (LTM) dividend yield annual time series.
Time series is based on the dividend yield for the end of calendar year.
LTM dividend yield is the sum trailing twelve months of common dividends per share divided by
the current price per share.
All yields are calculated in the asset list base currency after adjusting the dividends and price time series.
Forecasted (future) dividends are removed.
Returns
-------
DataFrame
Time series of LTM dividend yield for each asset.
See Also
--------
dividend_yield : Dividend yield time series.
dividends_annual : Calendar year dividends time series.
dividend_paying_years : Number of years of consecutive dividend payments.
dividend_growing_years : Number of years when the annual dividend was growing.
get_dividend_mean_yield : Arithmetic mean for annual dividend yield.
get_dividend_mean_growth_rate : Geometric mean of annual dividends growth rate.
Examples
--------
>>> import matplotlib.pyplot as plt
>>> pf = ok.Portfolio(['SPY.US', 'BND.US'], ccy='USD', last_date='07-2021')
>>> pf.dividend_yield_annual.plot(kind='bar')
>>> plt.show()
"""
return self._assets_dividend_yield.resample(rule="Y").last()

@property
def assets_dividend_yield(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def test_dividend_yield(portfolio_dividends):
def test_dividends_annual(portfolio_dividends):
assert portfolio_dividends.dividends_annual.iloc[-1].sum() == approx(32.778668, rel=1e-3)

def test_dividend_yield_annual(portfolio_dividends):
assert portfolio_dividends.dividend_yield_annual.iloc[0, 0] == approx(0.004444, abs=1e-3)

def test_risk(portfolio_rebalanced_month):
assert portfolio_rebalanced_month.risk_monthly == approx(0.02233, rel=1e-1)
Expand Down

0 comments on commit 85e00c7

Please sign in to comment.