-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhibor.py
125 lines (108 loc) · 3.47 KB
/
hibor.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
import urllib.request, json, argparse, datetime
PROG_DESC = 'Retrieves and outputs 1-Month HIBOR rates from the HKMA API.\nFORMAT:\nYYYY-MM-DD\tHIBOR_RATE'
BASE_API_URL = 'https://api.hkma.gov.hk/public/market-data-and-statistics'
BASE_API_URL += '/daily-monetary-statistics/daily-figures-interbank-liquidity'
DEFAULT_RECORD_COUNT = 10
MAX_RECORD_COUNT = 1000
#DELIMITER = '\t'
DELIMITER = ', '
OUTPUT_DATE_FORMAT = '%Y/%m/%d'
def getHiborDataFromApi(
datefrom = 0,
dateto = 0,
recordcount = 0,
show_date = True,
only_date = False,
timestamp_flag = False
):
timestamp:str = ""
if not recordcount or recordcount > MAX_RECORD_COUNT:
recordcount = DEFAULT_RECORD_COUNT
url = BASE_API_URL + '?fields=end_of_date,hibor_fixing_1m'
if datefrom or dateto:
if not datefrom: datefrom = dateto
if not dateto: dateto = datetime.datetime.now().strftime('%Y%m%d')
datefrom = datetime.datetime.strptime(str(datefrom),'%Y%m%d').strftime('%Y-%m-%d')
dateto = datetime.datetime.strptime(str(dateto),'%Y%m%d').strftime('%Y-%m-%d')
url += f'&choose=end_of_date&from={datefrom}&to={dateto}'
url += f'&sortby=end_of_date&sortorder=asc'
in_reverse = False
else:
in_reverse = True
url += f'&pagesize={recordcount}'
#url += '&choose=end_of_date&from=2022-06-01&to=2022-06-30'
try:
j = json.loads(urllib.request.urlopen(url).read())
except Exception as e:
print(f'ERROR: {e}')
exit()
l = j["result"]["records"]
if in_reverse: l.reverse()
if j["header"]["success"]:
if len(l):
for r in l:
date = datetime.datetime.strptime(r['end_of_date'],'%Y-%m-%d').strftime(OUTPUT_DATE_FORMAT)
rate = f"{r['hibor_fixing_1m']}%"
if only_date:
rate = ''
elif show_date:
date += DELIMITER
else:
date = ''
if timestamp_flag:
timestamp = f"{datetime.datetime.today():%Y/%m/%d %H:%M}: "
print(f"{timestamp}{date}{rate}")
else:
print(f'No data')
else:
print(f'ERROR: {j["header"]["err_msg"]}')
#print(f'{url = }')
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = PROG_DESC,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
date_group = parser.add_mutually_exclusive_group()
parser.add_argument('-f',
help = 'The FROM date to retrieve data',
type = int,
metavar = 'YYYYMMDD',
dest = 'datefrom'
)
parser.add_argument('-t',
help = 'The TO date to retrieve data (default: today)',
type = int,
metavar = 'YYYYMMDD',
dest = 'dateto'
)
parser.add_argument('-ts', '-timestamp',
help = 'Display timestamp (default: false)',
dest = 'timestamp_flag',
action = 'store_true'
)
parser.add_argument('-c', '-count',
help = 'Max number of records to retrieve (default: 10)',
type = int,
metavar = 'N',
dest = 'recordcount',
default = DEFAULT_RECORD_COUNT
)
date_group.add_argument('-nd', '-no_date',
help = 'Do not display date in output (default: false)',
action = 'store_false',
dest = 'no_date'
)
date_group.add_argument('-od', '-only_date',
help = 'Display ONLY the date in output (default: false)',
action = 'store_true',
dest = 'only_date'
)
args = parser.parse_args()
getHiborDataFromApi(
datefrom=args.datefrom,
dateto = args.dateto,
recordcount = args.recordcount,
show_date = args.no_date,
only_date = args.only_date,
timestamp_flag = args.timestamp_flag
)