-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrkd_chart.py
341 lines (325 loc) · 12.8 KB
/
trkd_chart.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
'''
The RKD API sample code is provided for informational purposes only
and without knowledge or assumptions of the end users development environment.
We offer this code to provide developers practical and useful guidance while developing their own code.
However, we do not offer support and troubleshooting of issues that are related to the use of this code
in a particular environment; it is offered solely as sample code for guidance.
Please see the Refinitiv Knowledge Direct (RKD) API (formerly known as TRKD API) product page at https://my.refinitiv.com
for additional information regarding the RKD API.'''
import os
import sys
import requests
import json
import getpass
import urllib
from dotenv import load_dotenv
# Send HTTP request for all services
def doSendRequest(url, requestMsg, headers):
result = None
try:
##send request
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
# print('outgoing message is %s'%(json.dumps(requestMsg)))
## handle error
if result.status_code != 200:
print('Request fail')
print('response status %s'%(result.status_code))
if result.status_code == 500: ## if username or password or appid is wrong
#print('Error: %s'%(result.json()))
print('Error: %s' % (json.dumps(result.json(),sort_keys=True, indent=2, separators=(',', ':'))))
result.raise_for_status()
except requests.exceptions.RequestException as e:
print('Exception!!!')
print(e)
sys.exit(1)
return result
## Perform authentication
def CreateAuthorization(username, password, appid):
token = None
##create authentication request URL, message and header
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
authenURL = 'https://api.rkd.refinitiv.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
headers = {'content-type': 'application/json;charset=utf-8'}
print('############### Sending Authentication request message to TRKD ###############')
authenResult = doSendRequest(authenURL, authenMsg, headers)
if authenResult is not None and authenResult.status_code == 200:
print('Authen success')
print('response status %s'%(authenResult.status_code))
##get Token
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
return token
## Perform Chart request
def RetrieveChart(token, appid):
##construct a Chart request message
ricName = input('Please input Symbol: ')
chartRequestMsg = {'GetChart_Request_2': {'chartRequest': {
'TimeSeries': {'TimeSeriesRequest_typehint': ['TimeSeriesRequest'],
'TimeSeriesRequest': [{'Symbol': ricName,
'Reference': 'd1'}]},
'Analyses': {'Analysis_typehint': ['Analysis', 'Analysis'],
'Analysis': [{'Reference': 'a1',
'OHLC': {'Instrument1': {'Reference': 'd1'}}},
{'Reference': 'a2',
'Vol': {'Instrument1': {'Reference': 'd1'}}}]},
'StandardTemplate': {
'Interval': {'CommonType': 'Days', 'Multiplier': '1'},
'ShowNonTradedPeriods': False,
'ShowHolidays': False,
'ShowGaps': True,
'XAxis': {'Range': {'Fixed': {'First': '2015-09-22T00:00:00',
'Last': '2016-09-22T00:00:00'}}, 'Visible': True,
'Position': 'Bottom'},
'Subchart': [{'YAxis': [{
'Analysis': [{'Reference': 'a1'}],
'Visible': True,
'Position': 'Right',
'Invert': False,
'Logarithmic': False,
'Display': {'Mode': 'Automatic'},
'Range': {'Automatic': ''},
}], 'Weight': 5.0}, {'YAxis': [{
'Analysis': [{'Reference': 'a2'}],
'Visible': True,
'Position': 'Right',
'Invert': False,
'Logarithmic': False,
'Display': {'Mode': 'Automatic'},
'Range': {'Automatic': ''},
}], 'Weight': 2.0}],
'Title': {'Caption': {'Visible': True, 'Customized': False},
'Range': {'Visible': True}},
'Legend': {
'Visible': True,
'Information': 'Long',
'Layout': 'MultiLine',
'Position': 'Overlaid',
},
'Instrument': 'Symbol',
'Delimiter': '%',
'GridLines': 'None',
'YAxisMarkers': 'None',
'YAxisTitles': 'All',
'Brand': 'None',
},
'Scheme': {
'Background': {
'BackgroundMode': 'Solid',
'StartColor': {'Named': 'White'},
'EndColor': {'Named': 'White'},
'HatchStyle': 'LargeGrid',
'GradientMode': 'ForwardDiagonal',
'ImageMode': 'Centered',
},
'Border': {'Color': {'RGB': '139;139;155'},
'DashStyle': 'Solid', 'Width': 1.0},
'GridLines': {'Color': {'RGB': '139;139;155'},
'DashStyle': 'Dot', 'Width': 1.0},
'Title': {'Caption': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Bold',
'Size': 12.0,
}, 'Range': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Regular',
'Size': 8.25,
}},
'Legend': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Regular',
'Size': 8.25,
},
'XAxis': {'Major': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Bold',
'Size': 9.75,
}, 'Minor': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Regular',
'Size': 8.25,
}},
'YAxis': {'Major': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Bold',
'Size': 9.75,
}, 'Minor': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Regular',
'Size': 8.25,
}, 'Title': {
'Color': {'Named': 'Black'},
'Family': 'Arial',
'Style': 'Regular',
'Size': 8.25,
}},
'Series': [
{
'Color': {'Named': 'Black'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'Named': 'Black'},
'FillStyle': 'Percent20',
},
{
'Color': {'Named': 'Red'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'Named': 'Red'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '62;169;0'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '62;169;0'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '156;38;115'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '156;38;115'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '255;120;0'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '255;120;0'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '25;108;229'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '25;108;229'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '60;117;28'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '60;117;28'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '230;176;18'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '230;176;18'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '0;186;193'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '0;186;193'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '255;178;127'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '255;178;127'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '100;79;190'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '100;79;190'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '209;36;33'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '209;36;33'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '38;87;135'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '38;87;135'},
'FillStyle': 'Percent20',
},
{
'Color': {'RGB': '94;176;176'},
'DashStyle': 'Solid',
'Width': 0.0,
'FillColor': {'RGB': '94;176;176'},
'FillStyle': 'Percent20',
},
],
'LevelLine': [{'Color': {'RGB': '0;0;153'}, 'DashStyle': 'Solid'
, 'Width': 1.0}, {'Color': {'RGB': '120;120;120'
}, 'DashStyle': 'Solid', 'Width': 1.0}],
},
'ImageType': 'PNG',
'Width': 500,
'Height': 400,
'Culture': 'en-US',
'ReturnPrivateNetworkURL': False,
}}}
##construct Chart URL and header
chartURL = 'http://api.rkd.refinitiv.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2'
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
print('############### Sending Chart request message to TRKD ###############')
chartResult = doSendRequest(chartURL, chartRequestMsg,headers)
if chartResult is not None and chartResult.status_code == 200:
print('Time Series Interday response message: ')
print(chartResult.json())
##print returned server, tag and image url
server = chartResult.json()['GetChart_Response_2']['ChartImageResult']['Server']
print('\nServer: %s'%(server))
tag = chartResult.json()['GetChart_Response_2']['ChartImageResult']['Tag']
print('Tag: %s'%(tag))
imageUrl = chartResult.json()['GetChart_Response_2']['ChartImageResult']['Url']
print('Url: %s'%(imageUrl))
return imageUrl
##download image url from the TRKD Chart service as chart.png
def downloadChartImage(chartURL):
##create header
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
print('\nDownlading chart.png file from %s'%(chartURL))
##download image using Python3 urllib
downloadResult = urllib.request.Request(chartURL, headers=headers)
imgData = urllib.request.urlopen(downloadResult).read()
##write file
fileName = './chart.png'
with open(fileName,'wb') as outfile:
outfile.write(imgData)
print('save chart.png file complete')
## ------------------------------------------ Main App ------------------------------------------ ##
if __name__ == '__main__':
# Load Environment Variables
load_dotenv()
# Get username, password and application_id from Environment Variables or .env
username = os.getenv('RKD_USERNAME')
# use getpass.getpass to hide user inputted password
password = os.getenv('RKD_PASSWORD')
appid = os.getenv('RKD_APP_ID')
#If not Environment Variables or .env
if not (username and password and appid):
## Get username, password and applicationid
username = input('Please input username: ')
## Use getpass.getpass to hide user inputted password
password = getpass.getpass(prompt='Please input password: ')
appid = input('Please input appid: ')
token = CreateAuthorization(username,password,appid)
print('Token = %s'%(token))
## if authentication success, continue subscribing Chart
if token:
chartURL = RetrieveChart(token,appid)
## if chart request success, continue downloading Chart image
if chartURL:
print('############### Downloading Chart file from TRKD ###############')
downloadChartImage(chartURL)