Skip to content

Commit 0f29fbc

Browse files
author
Wasin Waeosri
committed
Merge branch 'v9_addcomments'
2 parents 32cb1bf + 3e40bf3 commit 0f29fbc

File tree

7 files changed

+433
-11
lines changed

7 files changed

+433
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
chart.png

trkd_chart.py

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
'''
2+
The TRKD API sample code is provided for informational purposes only
3+
and without knowledge or assumptions of the end users development environment.
4+
We offer this code to provide developers practical and useful guidance while developing their own code.
5+
However, we do not offer support and troubleshooting of issues that are related to the use of this code
6+
in a particular environment; it is offered solely as sample code for guidance.
7+
Please see the Thomson Reuters Knowledge Direct product page at http://customers.thomsonreuters.com
8+
for additional information regarding the TRKD API.'''
9+
10+
11+
import os
12+
import sys
13+
import requests
14+
import json
15+
import getpass
16+
import urllib2
17+
18+
19+
def doSendRequest(url, requestMsg, headers):
20+
result = None
21+
try:
22+
##send request
23+
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
24+
if result.status_code == 500:
25+
print 'Request fail'
26+
print 'response status %s' % result.status_code
27+
print 'Error: %s' % result.json()
28+
sys.exit(1)
29+
except requests.exceptions.RequestException, e:
30+
print 'Exception!!!'
31+
print e
32+
sys.exit(1)
33+
return result
34+
35+
36+
## Perform authentication
37+
def CreateAuthorization(username, password, appid):
38+
token = None
39+
##create authentication request URL, message and header
40+
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
41+
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
42+
headers = {'content-type': 'application/json;charset=utf-8'}
43+
print '############### Sending Authentication request message to TRKD ###############'
44+
authenResult = doSendRequest(authenURL, authenMsg, headers)
45+
if authenResult is not None and authenResult.status_code == 200:
46+
print 'Authen success'
47+
print 'response status %s'%(authenResult.status_code)
48+
##get Token
49+
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
50+
51+
return token
52+
53+
## Perform Chart request
54+
def RetrieveChart(token, appid):
55+
##construct a Chart request message
56+
ricName = raw_input('Please input Symbol: ')
57+
58+
chartRequestMsg = {'GetChart_Request_2': {'chartRequest': {
59+
'TimeSeries': {'TimeSeriesRequest_typehint': ['TimeSeriesRequest'],
60+
'TimeSeriesRequest': [{'Symbol': ricName,
61+
'Reference': 'd1'}]},
62+
'Analyses': {'Analysis_typehint': ['Analysis', 'Analysis'],
63+
'Analysis': [{'Reference': 'a1',
64+
'OHLC': {'Instrument1': {'Reference': 'd1'}}},
65+
{'Reference': 'a2',
66+
'Vol': {'Instrument1': {'Reference': 'd1'}}}]},
67+
'StandardTemplate': {
68+
'Interval': {'CommonType': 'Days', 'Multiplier': '1'},
69+
'ShowNonTradedPeriods': False,
70+
'ShowHolidays': False,
71+
'ShowGaps': True,
72+
'XAxis': {'Range': {'Fixed': {'First': '2015-09-22T00:00:00',
73+
'Last': '2016-09-22T00:00:00'}}, 'Visible': True,
74+
'Position': 'Bottom'},
75+
'Subchart': [{'YAxis': [{
76+
'Analysis': [{'Reference': 'a1'}],
77+
'Visible': True,
78+
'Position': 'Right',
79+
'Invert': False,
80+
'Logarithmic': False,
81+
'Display': {'Mode': 'Automatic'},
82+
'Range': {'Automatic': ''},
83+
}], 'Weight': 5.0}, {'YAxis': [{
84+
'Analysis': [{'Reference': 'a2'}],
85+
'Visible': True,
86+
'Position': 'Right',
87+
'Invert': False,
88+
'Logarithmic': False,
89+
'Display': {'Mode': 'Automatic'},
90+
'Range': {'Automatic': ''},
91+
}], 'Weight': 2.0}],
92+
'Title': {'Caption': {'Visible': True, 'Customized': False},
93+
'Range': {'Visible': True}},
94+
'Legend': {
95+
'Visible': True,
96+
'Information': 'Long',
97+
'Layout': 'MultiLine',
98+
'Position': 'Overlaid',
99+
},
100+
'Instrument': 'Symbol',
101+
'Delimiter': '%',
102+
'GridLines': 'None',
103+
'YAxisMarkers': 'None',
104+
'YAxisTitles': 'All',
105+
'Brand': 'None',
106+
},
107+
'Scheme': {
108+
'Background': {
109+
'BackgroundMode': 'Solid',
110+
'StartColor': {'Named': 'White'},
111+
'EndColor': {'Named': 'White'},
112+
'HatchStyle': 'LargeGrid',
113+
'GradientMode': 'ForwardDiagonal',
114+
'ImageMode': 'Centered',
115+
},
116+
'Border': {'Color': {'RGB': '139;139;155'},
117+
'DashStyle': 'Solid', 'Width': 1.0},
118+
'GridLines': {'Color': {'RGB': '139;139;155'},
119+
'DashStyle': 'Dot', 'Width': 1.0},
120+
'Title': {'Caption': {
121+
'Color': {'Named': 'Black'},
122+
'Family': 'Arial',
123+
'Style': 'Bold',
124+
'Size': 12.0,
125+
}, 'Range': {
126+
'Color': {'Named': 'Black'},
127+
'Family': 'Arial',
128+
'Style': 'Regular',
129+
'Size': 8.25,
130+
}},
131+
'Legend': {
132+
'Color': {'Named': 'Black'},
133+
'Family': 'Arial',
134+
'Style': 'Regular',
135+
'Size': 8.25,
136+
},
137+
'XAxis': {'Major': {
138+
'Color': {'Named': 'Black'},
139+
'Family': 'Arial',
140+
'Style': 'Bold',
141+
'Size': 9.75,
142+
}, 'Minor': {
143+
'Color': {'Named': 'Black'},
144+
'Family': 'Arial',
145+
'Style': 'Regular',
146+
'Size': 8.25,
147+
}},
148+
'YAxis': {'Major': {
149+
'Color': {'Named': 'Black'},
150+
'Family': 'Arial',
151+
'Style': 'Bold',
152+
'Size': 9.75,
153+
}, 'Minor': {
154+
'Color': {'Named': 'Black'},
155+
'Family': 'Arial',
156+
'Style': 'Regular',
157+
'Size': 8.25,
158+
}, 'Title': {
159+
'Color': {'Named': 'Black'},
160+
'Family': 'Arial',
161+
'Style': 'Regular',
162+
'Size': 8.25,
163+
}},
164+
'Series': [
165+
{
166+
'Color': {'Named': 'Black'},
167+
'DashStyle': 'Solid',
168+
'Width': 0.0,
169+
'FillColor': {'Named': 'Black'},
170+
'FillStyle': 'Percent20',
171+
},
172+
{
173+
'Color': {'Named': 'Red'},
174+
'DashStyle': 'Solid',
175+
'Width': 0.0,
176+
'FillColor': {'Named': 'Red'},
177+
'FillStyle': 'Percent20',
178+
},
179+
{
180+
'Color': {'RGB': '62;169;0'},
181+
'DashStyle': 'Solid',
182+
'Width': 0.0,
183+
'FillColor': {'RGB': '62;169;0'},
184+
'FillStyle': 'Percent20',
185+
},
186+
{
187+
'Color': {'RGB': '156;38;115'},
188+
'DashStyle': 'Solid',
189+
'Width': 0.0,
190+
'FillColor': {'RGB': '156;38;115'},
191+
'FillStyle': 'Percent20',
192+
},
193+
{
194+
'Color': {'RGB': '255;120;0'},
195+
'DashStyle': 'Solid',
196+
'Width': 0.0,
197+
'FillColor': {'RGB': '255;120;0'},
198+
'FillStyle': 'Percent20',
199+
},
200+
{
201+
'Color': {'RGB': '25;108;229'},
202+
'DashStyle': 'Solid',
203+
'Width': 0.0,
204+
'FillColor': {'RGB': '25;108;229'},
205+
'FillStyle': 'Percent20',
206+
},
207+
{
208+
'Color': {'RGB': '60;117;28'},
209+
'DashStyle': 'Solid',
210+
'Width': 0.0,
211+
'FillColor': {'RGB': '60;117;28'},
212+
'FillStyle': 'Percent20',
213+
},
214+
{
215+
'Color': {'RGB': '230;176;18'},
216+
'DashStyle': 'Solid',
217+
'Width': 0.0,
218+
'FillColor': {'RGB': '230;176;18'},
219+
'FillStyle': 'Percent20',
220+
},
221+
{
222+
'Color': {'RGB': '0;186;193'},
223+
'DashStyle': 'Solid',
224+
'Width': 0.0,
225+
'FillColor': {'RGB': '0;186;193'},
226+
'FillStyle': 'Percent20',
227+
},
228+
{
229+
'Color': {'RGB': '255;178;127'},
230+
'DashStyle': 'Solid',
231+
'Width': 0.0,
232+
'FillColor': {'RGB': '255;178;127'},
233+
'FillStyle': 'Percent20',
234+
},
235+
{
236+
'Color': {'RGB': '100;79;190'},
237+
'DashStyle': 'Solid',
238+
'Width': 0.0,
239+
'FillColor': {'RGB': '100;79;190'},
240+
'FillStyle': 'Percent20',
241+
},
242+
{
243+
'Color': {'RGB': '209;36;33'},
244+
'DashStyle': 'Solid',
245+
'Width': 0.0,
246+
'FillColor': {'RGB': '209;36;33'},
247+
'FillStyle': 'Percent20',
248+
},
249+
{
250+
'Color': {'RGB': '38;87;135'},
251+
'DashStyle': 'Solid',
252+
'Width': 0.0,
253+
'FillColor': {'RGB': '38;87;135'},
254+
'FillStyle': 'Percent20',
255+
},
256+
{
257+
'Color': {'RGB': '94;176;176'},
258+
'DashStyle': 'Solid',
259+
'Width': 0.0,
260+
'FillColor': {'RGB': '94;176;176'},
261+
'FillStyle': 'Percent20',
262+
},
263+
],
264+
'LevelLine': [{'Color': {'RGB': '0;0;153'}, 'DashStyle': 'Solid'
265+
, 'Width': 1.0}, {'Color': {'RGB': '120;120;120'
266+
}, 'DashStyle': 'Solid', 'Width': 1.0}],
267+
},
268+
'ImageType': 'PNG',
269+
'Width': 500,
270+
'Height': 400,
271+
'Culture': 'en-US',
272+
'ReturnPrivateNetworkURL': False,
273+
}}}
274+
##construct Chart URL and header
275+
chartURL = 'http://api.rkd.reuters.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2'
276+
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
277+
278+
print '############### Sending Chart request message to TRKD ###############'
279+
chartResult = doSendRequest(chartURL, chartRequestMsg,headers)
280+
if chartResult is not None and chartResult.status_code == 200:
281+
print 'Time Series Interday response message: '
282+
print chartResult.json()
283+
##print returned server, tag and image url
284+
server = chartResult.json()['GetChart_Response_2']['ChartImageResult']['Server']
285+
print '\nServer: %s'%(server)
286+
tag = chartResult.json()['GetChart_Response_2']['ChartImageResult']['Tag']
287+
print 'Tag: %s'%(tag)
288+
imageUrl = chartResult.json()['GetChart_Response_2']['ChartImageResult']['Url']
289+
print 'Url: %s'%(imageUrl)
290+
return imageUrl
291+
292+
##download image url from the TRKD Chart service as chart.png
293+
def downloadChartImage(chartURL):
294+
##create header
295+
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
296+
headers = { 'User-Agent' : user_agent }
297+
print '\nDownlading chart.png file from %s'%(chartURL)
298+
##download image using Python urllib2
299+
downloadResult = urllib2.Request(chartURL, headers=headers)
300+
imgData = urllib2.urlopen(downloadResult).read()
301+
##write file
302+
fileName = './chart.png'
303+
with open(fileName,'wb') as outfile:
304+
outfile.write(imgData)
305+
print 'save chart.png file complete'
306+
307+
308+
309+
## ------------------------------------------ Main App ------------------------------------------ ##
310+
##Get username, password and applicationid
311+
username = raw_input('Please input username: ')
312+
##use getpass.getpass to hide user inputted password
313+
password = getpass.getpass(prompt='Please input password: ')
314+
appid = raw_input('Please input appid: ')
315+
316+
token = CreateAuthorization(username,password,appid)
317+
print 'Token = %s'%(token)
318+
## if authentiacation success, continue subscribing Chart
319+
if token is not None:
320+
chartURL = RetrieveChart(token,appid)
321+
## if chart request success, continue downloading Chart image
322+
if chartURL is not None:
323+
print '############### Downloading Chart file from TRKD ###############'
324+
downloadChartImage(chartURL)
325+
326+
327+
328+
329+
330+
331+
332+
333+
334+
335+
336+
337+
338+

trkd_interday.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def CreateAuthorization(username, password, appid):
4949

5050
return token
5151

52-
## Perform Quote request
52+
## Perform Interday request
5353
def RetrieveInteraday(token, appid):
54-
54+
##construct Time Series Interday request message
5555
ricName = raw_input('Please input Symbol: ')
5656
interdayRequestMsg = None
5757
fields = ['OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK'] #change your fields (support these 'OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK' fields only)
@@ -71,7 +71,7 @@ def RetrieveInteraday(token, appid):
7171
'MetaField': ['NAME','QOS','CCY','TZ','TZOFFSET','NAME_LL']
7272
}
7373
}
74-
74+
##construct Time Series Interday URL and header
7575
interdayURL = 'http://api.rkd.reuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetInterdayTimeSeries_4'
7676
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
7777

@@ -91,7 +91,7 @@ def RetrieveInteraday(token, appid):
9191

9292
token = CreateAuthorization(username,password,appid)
9393
print 'Token = %s'%(token)
94-
## if authentiacation success, continue subscribing Time Series intraday
94+
## if authentiacation success, continue subscribing Time Series interday
9595
if token is not None:
9696
RetrieveInteraday(token,appid)
9797

trkd_intraday.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def CreateAuthorization(username, password, appid):
4949

5050
return token
5151

52-
## Perform Quote request
52+
## Perform Intraday request
5353
def RetrieveIntraday(token, appid):
54-
54+
##construct Time Series Intraday request message
5555
ricName = raw_input('Please input Symbol: ')
5656
intradayRequestMsg = None
5757
fields = ['OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK'] #change your fields (support these 'OPEN','HIGH','LOW','CLOSE','CLOSEYIELD','VOLUME','BID','ASK' fields only)
@@ -71,7 +71,7 @@ def RetrieveIntraday(token, appid):
7171
'MetaField': ['NAME','QOS','CCY','TZ','TZOFFSET','NAME_LL']
7272
}
7373
}
74-
74+
##construct Time Series Intraday URL and header
7575
intradayURL = 'http://api.rkd.reuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetIntradayTimeSeries_4'
7676
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
7777

0 commit comments

Comments
 (0)