-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetFredAPI.py
56 lines (48 loc) · 1.69 KB
/
getFredAPI.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
from fredapi import Fred
import configparser
import datetime
from datetime import timedelta
config = configparser.ConfigParser()
config.sections()
config.read('setting.ini')
fredToken = config['DEFAULT']['FRED_API_KEY']
def getFredAPI():
fred = Fred(api_key=fredToken)
#'NASDAQCOM' 更新時間太慢 暫時不發
watchList = ['SP500','DJIA']
resultList = []
for watch in watchList:
result = dict()
watchLastInfo = fred.get_series_info(watch)
watchLastIndex = fred.get_series(watch).tail(2)
lastUpdateDate = watchLastInfo.observation_end
indexGap = round(watchLastIndex[1] - watchLastIndex[0], 2)
percent = round(indexGap / watchLastIndex[0] * 100 , 2)
result['title'] = watch
result['value'] = str(watchLastIndex[1])
result['indexGap'] = converterPrefix(indexGap)
converterPercent = str(percent).replace('+','').replace('-','')
result['gapPercent'] = converterPercent + '%'
result['lastUpdateDate'] = lastUpdateDate
resultList.append(result)
return resultList
def getFredData(name):
fred = Fred(api_key=fredToken)
watchLastInfo = fred.get_series_info(name)
watchLastIndex = fred.get_series(name).tail(2)
print(watchLastInfo)
print(watchLastIndex)
def converterPrefix(percent):
converterPercent = str(percent).replace('+','').replace('-','')
converterValue = ''
prefix = ''
if(percent>0):
prefix = '🔺'
elif(percent < 0):
prefix = '🔻'
converterValue = prefix + converterPercent
return converterValue
if __name__ == '__main__':
print(getFredAPI())
print(getFredData('SP500'))
print(getFredData('NASDAQCOM'))