SET50 Min-Max

In [29]:
%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).

In [30]:
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.

In [31]:
set50 = set_data[set_data['ticker']=='SET50']
set50 = set50[set50['date'] >= date(2006,1,1)]

Extract SET50 Min and Max by Year

In [32]:
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

In [33]:
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

  1. always buying at the lowest point of the year (most lucky - highest profit)
  2. always buying at the highest point of the year (least lucky - lowest profit)

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.

In [34]:
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
Out[34]:
min max unit_min unit_max value_min value_max invested
year
2006 430.96 547.53 23.204010 18.263839 25269.166512 19889.321133 10000
2007 428.73 681.82 23.324703 14.666628 25400.601777 15971.957408 10000
2008 261.30 641.94 38.270188 15.577780 41676.234214 16964.202262 10000
2009 284.25 537.02 35.180299 18.621280 38311.345646 20278.574355 10000
2010 480.60 733.22 20.807324 13.638471 22659.176030 14852.295355 10000
2011 592.57 801.44 16.875643 12.477540 18377.575645 13588.041525 10000
2012 724.43 951.29 13.803956 10.512042 15032.508317 11447.613241 10000
2013 873.62 1092.27 11.446624 9.155245 12465.373961 9970.062347 10000
2014 829.89 1074.80 12.049790 9.304057 13122.221017 10132.117603 10000
2015 801.81 1074.39 12.471783 9.307607 13581.771243 10135.984140 10000
2016 761.75 990.71 13.127667 10.093771 14296.028881 10992.116765 10000
In [35]:
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()
         Total Invested: 110000
Always Buy Max Scenario: 154222
Always Buy Min Scenario: 240192

    3% Yield Investment: 131920
    4% Yield Investment: 140258
    5% Yield Investment: 149171
    6% Yield Investment: 158699

This works because in the long run the market tracks macroeconomics of the country.

In [36]:
set50.groupby(set50.date.dt.year)["close"].mean().plot(title="Average SET50 Price")
Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0x109d58a90>