-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_index.py
153 lines (98 loc) · 3.92 KB
/
make_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import datetime as dt
from datetime import date
from datetime import datetime
from dateutil.rrule import rrule, DAILY
from dateutil import parser
import pandas as pd
import os
import math
def getCountries(currencyName):
countries = os.listdir(currencyName + "_data")
for i in range(0, len(countries)):
countries[i] = countries[i][:-4] #remove file extension
return countries
def getGDPData(country):
df = pd.read_csv("shared_gdp_data\\" + country + ".csv", index_col = 1, names = ['Country', 'GDP']);
return df
def getForexData(country, currencyName):
df = pd.read_csv(currencyName + "_data\\" + country + ".csv", names = ['Date', 'Price']);
df = getForexDataChange(df)
df.set_index('Date', inplace = True)
return df
def getForexDataChange(df):
changes = [1.0];
for i in range(0, len(df['Price']) - 1):
change = float(df['Price'].iloc[i + 1]) / float(df['Price'].iloc[i])
changes.append(change)
df['Change'] = pd.Series(changes, index=df.index)
return df
def getIndexChanges(forex, gdp):
dateTimes = []
indexChanges = []
gdp.drop(gdp.tail(1).index,inplace=True)
#Loop through every day
for dateTime in rrule(DAILY, dtstart=dt.datetime(2003, 12, 1), until=dt.datetime(2017, 10, 23)):
dtStr = str(dateTime.year).zfill(4) + "-" + str(dateTime.month).zfill(2) + "-" + str(dateTime.day).zfill(2)
if(dtStr in forex.index):
dateTimes.append(dateTime)
year = dateTime.year;
if year == 2017:
year = 2016
indexChanges.append(calculateWeightedGeometricMean(forex.loc[dtStr], gdp.loc[str(year)]))
df = pd.DataFrame({'Date' : dateTimes, 'Change': indexChanges})
return df
def calculateWeightedGeometricMean(forex, gdp):
a = 1;
#Loop through every country/currency
for i in range(0, len(forex)):
#clean data
try:
val = float(forex.iloc[i])
except ValueError:
forex.iloc[i] = '1'
if forex.iloc[i] == '0':
forex.iloc[i] = '1';
if(math.isnan(float(forex.iloc[i]))):
forex.iloc[i] = '1'
try:
val = float(gdp.iloc[i])
except ValueError:
gdp.iloc[i] = '1'
if gdp.iloc[i] == '0':
gdp.iloc[i] = '1';
if(math.isnan(float(gdp.iloc[i]))):
gdp.iloc[i] = '1'
a = a * pow(float(forex.iloc[i]), float(gdp.iloc[i]) / 1000000000000.0) #divide 1,000,000,000,000 1 trillion
a = pow(a, 1 / calculateSumOfGDP(gdp))
return a
def calculateSumOfGDP(gdps):
a = 0.0
for gdp in gdps:
if not (math.isnan(float(gdp))):
a = a + float(gdp)
return a / 1000000000000.0
def getIndex(indexChanges):
indices = []
index = 100.0
for i in range(0, len(indexChanges)):
#Clean data
if indexChanges['Change'].iloc[i] != indexChanges['Change'].iloc[i]:
indexChanges['Change'].iloc[i] = 1.0
index = index * indexChanges['Change'].iloc[i]
indices.append([indexChanges['Date'].iloc[i], index])
return indices
print("Enter the currency code (eg. SGD)")
currencyName = input()
countries = getCountries(currencyName)
gdpAllDf = pd.DataFrame()
forexAllDf = pd.DataFrame()
for i in range(0, len(countries)):
gdpDf = getGDPData(countries[i])
gdpDf = gdpDf.iloc[::-1] #Reverse, to be in ascending date
forexDf = getForexData(countries[i], currencyName)
gdpAllDf[countries[i]] = gdpDf['GDP'];
forexAllDf[countries[i]] = forexDf['Change'];
indexChanges = getIndexChanges(forexAllDf, gdpAllDf)
indices = getIndex(indexChanges)
df = pd.DataFrame(indices);
df.to_csv(currencyName + '_Index.csv');