-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgd_index.py
143 lines (97 loc) · 3.15 KB
/
sgd_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
import datetime as dt
from dateutil import parser
import pandas as pd
import pandas_datareader.data as web
tickers = ['SGDUSD=X',
'SGDEUR=X',
'SGDCNY=X',
'SGDJPY=X',
'SGDGBP=X',
'SGDBRL=X',
'SGDCAD=X',
'SGDRUB=X',
'SGDAUD=X',
'SGDMXN=X',
'SGDIDR=X',
'SGDTRY=X',
'SGDCHF=X',
'SGDSAR=X']
countries = ['US',
'EU',
'CN',
'JP',
'GB',
'BR',
'CA',
'RU',
'AU',
'MX',
'ID',
'TR',
'CH',
'SA']
def getGDPData(country):
df = pd.read_csv("data\\gdp\\" + country + ".csv", index_col = 1, names = ['Country', 'GDP']);
return df
def getForexData(ticker):
df = pd.read_csv("data\\forex\\" + ticker + ".csv", index_col = 0, names = ['Price', 'Change']);
return df
def getIndexChanges(forex, gdp):
gdp.drop(gdp.tail(1).index,inplace=True)
forex.drop(forex.tail(1).index,inplace=True)
indexChanges = []
#Loop through every day
for i in range(0, len(forex)):
dateTime = parser.parse(forex.index.values[i])
year = dateTime.year;
if year == 2017:
year = 2016
indexChanges.append(calculateWeightedGeometricMean(forex.iloc[i], gdp.loc[str(year)]))
return indexChanges
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';
try:
val = float(gdp.iloc[i])
except ValueError:
gdp.iloc[i] = '1'
if gdp.iloc[i] == '0':
gdp.iloc[i] = '1';
a = a * pow(float(forex.iloc[i]), float(gdp.iloc[i]) / 10000000000.0) #divide 10,000,000,000 ten billion
a = pow(a, 1 / calculateSumOfGDP(gdp))
return a
def calculateSumOfGDP(gdps):
a = 0.0
for gdp in gdps:
a = a + float(gdp)
return a / 10000000000.0
def getIndex(indexChanges):
indices = []
index = 100.0
for i in range(0, len(indexChanges)):
#Clean data
if indexChanges[i] != indexChanges[i]:
indexChanges[i] = 1.0
index = index * indexChanges[i]
indices.append(index)
return indices
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(tickers[i])
gdpAllDf[countries[i]] = gdpDf['GDP'];
forexAllDf[tickers[i]] = forexDf['Change'];
indexChanges = getIndexChanges(forexAllDf, gdpAllDf)
indices = getIndex(indexChanges)
df = pd.DataFrame(indices);
df.to_csv('SGD_Index.csv');