Analysis: Dividend of the month
In the following example we compute the dividends received this month in 2 different way. The first one simply aggregates the dividend. The second examlpe aggregates it by security.
from pyfolio_performance import Portfolio, Filters
from datetime import datetime
portfolio = Portfolio("portfolio.xml")
currentNow = datetime.now()
def filter_month(entry, month, year):
if year != entry.getYear() or month != entry.getMonth():
return False
return True
filter_dividend = Filters.fAnd(Filters.fEnsureTypeList(['DIVIDENDS']),
lambda entry: filter_month(entry, currentNow.month, currentNow.year) )
# different clustering
def cluster_dividend(allCluster, entry):
return "val"
def aggregate_dividend(cluster, entry):
return cluster + entry.getValue()
divicluster = {'val': 0}
portfolio.evaluateCluster(divicluster, filter_dividend, cluster_dividend, aggregate_dividend)
print(divicluster)
# Dividends are clustered by their name
def cluster_dividend2(allCluster, entry):
k = entry.getSourceName()
if k not in allCluster:
allCluster[k] = 0
return k
divicluster = {}
portfolio.evaluateCluster(divicluster, filter_dividend, cluster_dividend2, aggregate_dividend)
print(divicluster)
The code leads to the following two outputs. The first one simply gives the sum of all dividends. The second one aggregates it by the security that distributes the dividend.
The value is returned in cents. There were 13,79 received through the securities AT + T, Realty Income and Proctor and Gamble. The displayed name and corresponding key used in the code reflects the name given in portfolio performance’s security section.
{'val': 1379}
{'AT + T': 190, 'Realty Income': 781, 'Proctor and Gamble': 408}