Skip to content

Commit 85e00c7

Browse files
committed
feat: #64 add dividend_yeld_annual property in Portfolio like the same property in the AssetList
1 parent 2a9af2e commit 85e00c7

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#pf.wealth_index_with_assets.plot()
1111
#plt.show()
1212

13-
1413
import matplotlib.pyplot as plt
15-
pf = ok.Portfolio(['SPY.US', 'BND.US'], ccy='USD', last_date='07-2021', rebalancing_period="monthly")
16-
pf.dividends_annual.plot(kind='bar')
14+
pf = ok.Portfolio(['SPY.US', 'BND.US'], ccy='USD', last_date='07-2021')
15+
pf.dividend_yield_annual.plot(kind='bar')
1716
plt.show()

okama/asset_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ def dividend_yield_annual(self):
916916
--------
917917
>>> import matplotlib.pyplot as plt
918918
>>> x = ok.AssetList(['T.US', 'XOM.US'], first_date='2010-01', last_date='2020-12')
919-
>>> x.dividends_annual.plot(kind='bar')
919+
>>> x.dividend_yield_annual.plot(kind='bar')
920920
>>> plt.show()
921921
"""
922922
return self._assets_dividend_yield.resample(rule="Y").last()

okama/portfolio.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,42 @@ def dividends_annual(self) -> pd.DataFrame:
839839
"""
840840
return self._get_assets_dividends().resample("Y").sum()
841841

842+
@property
843+
def dividend_yield_annual(self):
844+
"""
845+
Calculate last twelve months (LTM) dividend yield annual time series.
846+
847+
Time series is based on the dividend yield for the end of calendar year.
848+
849+
LTM dividend yield is the sum trailing twelve months of common dividends per share divided by
850+
the current price per share.
851+
852+
All yields are calculated in the asset list base currency after adjusting the dividends and price time series.
853+
Forecasted (future) dividends are removed.
854+
855+
Returns
856+
-------
857+
DataFrame
858+
Time series of LTM dividend yield for each asset.
859+
860+
See Also
861+
--------
862+
dividend_yield : Dividend yield time series.
863+
dividends_annual : Calendar year dividends time series.
864+
dividend_paying_years : Number of years of consecutive dividend payments.
865+
dividend_growing_years : Number of years when the annual dividend was growing.
866+
get_dividend_mean_yield : Arithmetic mean for annual dividend yield.
867+
get_dividend_mean_growth_rate : Geometric mean of annual dividends growth rate.
868+
869+
Examples
870+
--------
871+
>>> import matplotlib.pyplot as plt
872+
>>> pf = ok.Portfolio(['SPY.US', 'BND.US'], ccy='USD', last_date='07-2021')
873+
>>> pf.dividend_yield_annual.plot(kind='bar')
874+
>>> plt.show()
875+
"""
876+
return self._assets_dividend_yield.resample(rule="Y").last()
877+
842878
@property
843879
def assets_dividend_yield(self):
844880
"""

tests/test_portfolio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ def test_dividend_yield(portfolio_dividends):
145145
def test_dividends_annual(portfolio_dividends):
146146
assert portfolio_dividends.dividends_annual.iloc[-1].sum() == approx(32.778668, rel=1e-3)
147147

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

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

0 commit comments

Comments
 (0)