"""
Obtain the annaulized implied dividend yield of Singapore Straits Times index
by reverse engineering (Financial Reverse Engineering)
from price return and total return indexes
Christopher Ting
2018-07-06, 07-08
"""

from __future__ import print_function
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# current director
DATA_DIR = '.'

###############################################################################
def read_xlsx(fname):
   fn = DATA_DIR + '/' + fname
   data = pd.read_excel(fn)
   dates = pd.to_datetime(data.Date, '%Y-%m-%d')
   data.index = dates
   data = data.drop(['Date','Total Ret','Price Ret','Impl Div Yld'], axis=1)
 
   return data

###############################################################################
# Read data downloaded from Bloomberg.
# S&P 500 price return index and Total return index

data = read_xlsx('FSSTITR--Index.xlsx')

# Plot the price return and total return indexes
data.plot()
plt.show()

# Simple return
r = data.pct_change()
r = r.dropna()

# Reverse engineer daily dividend yield
idy = (r['Total Ret Index'] - r['Price Ret Index'])/(1 + r['Price Ret Index'])

# Daily dividend in index points
div = idy*data['Price Ret Index'][1:]

# Do it from 2008 through 2016
years = range(2008,2017)
n = len(div)
for year in years:
   d = 0
   for i in range(n):
      if div.index[i].year == year:
         if div[i] > 0:
            d += div[i]

   dy = float(d)/data['Price Ret Index'][i+1]
   print("year = %d, implied dividend yield = %0.2f%%" % (year, dy*100))
   




