%matplotlib inline
from datetime import date
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [20,10]
Read all SET market data first (this may take a while ~ 10 seconds).
import pandas as pd
set_data = pd.read_csv("../data/set_1975_2016_close.csv.xz", parse_dates=[0])
Select only the SET50 ticker. Starting from 2006 because there's data missing between 2004-2005.
set50 = set_data[set_data['ticker']=='SET50']
set50 = set50[set50['date'] >= date(2006,1,1)]
Extract SET50 Min and Max by Year
set50_max = set50.loc[set50.groupby(set50.date.dt.year)["close"].idxmax()]
set50_min = set50.loc[set50.groupby(set50.date.dt.year)["close"].idxmin()]
Plot SET50 data with min-max value
set50_ax = set50.plot(x="date",y="close", legend=False, title='SET50 with Yearly Min and Max')
set50_min.plot(x="date",y="close", linestyle="None", legend=False, marker='o',markersize=15, ax=set50_ax)
set50_max.plot(x="date",y="close", linestyle="None", legend=False, marker='o',markersize=15, ax=set50_ax)
for year in range(2001,2017):
set50_ax.axvline(date(year,1,1), color='k', linestyle=':', alpha=0.4)
We can now compute a hypothetical scenario, in which we are
Given cash amount, units we are able to buy are
cash / SET50Index
Total Current Asset Value of our investment would simply be
total units * SET50IndexAtPresent
computed for both cases.
set50_min["year"] = set50_min["date"].dt.year
set50_max["year"] = set50_max["date"].dt.year
set50_min = set50_min.set_index("year")
set50_max = set50_max.set_index("year")
set50_minmax = pd.concat([set50_min['close'], set50_max['close']], axis=1, keys=['min', 'max'])
cash = 10000
set50_today = 1089
set50_minmax["unit_min"] = cash / set50_minmax["min"]
set50_minmax["unit_max"] = cash / set50_minmax["max"]
set50_minmax["value_min"] = set50_minmax["unit_min"] * set50_today
set50_minmax["value_max"] = set50_minmax["unit_max"] * set50_today
set50_minmax["invested"] = cash
set50_minmax
summary = (set50_minmax.sum()[-3:])
# r + r^1 + r^2 + .. + r^n
def geo(r, n):
return r * (1 - r ** n) / (1-r)
# invest every year with interest rate
def calc(years, percent, yearly_invest):
return yearly_invest * geo(1 + percent/100, years)
print(" Total Invested: {:.0f}".format(summary.invested))
print("Always Buy Max Scenario: {:.0f}".format(summary.value_max))
print("Always Buy Min Scenario: {:.0f}".format(summary.value_min))
print()
print(" {}% Yield Investment: {:.0f}".format(3, calc(11,3,cash)))
print(" {}% Yield Investment: {:.0f}".format(4, calc(11,4,cash)))
print(" {}% Yield Investment: {:.0f}".format(5, calc(11,5,cash)))
print(" {}% Yield Investment: {:.0f}".format(6, calc(11,6,cash)))
print()
This works because in the long run the market tracks macroeconomics of the country.
set50.groupby(set50.date.dt.year)["close"].mean().plot(title="Average SET50 Price")