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 Quote request
54+ def RetrieveChart (token , appid ):
55+
56+ ricName = raw_input ('Please input Symbol: ' )
57+ chartRequestMsg = {'GetChart_Request_2' : {'chartRequest' : {
58+ 'TimeSeries' : {'TimeSeriesRequest_typehint' : ['TimeSeriesRequest' ],
59+ 'TimeSeriesRequest' : [{'Symbol' : ricName ,
60+ 'Reference' : 'd1' }]},
61+ 'Analyses' : {'Analysis_typehint' : ['Analysis' , 'Analysis' ],
62+ 'Analysis' : [{'Reference' : 'a1' ,
63+ 'OHLC' : {'Instrument1' : {'Reference' : 'd1' }}},
64+ {'Reference' : 'a2' ,
65+ 'Vol' : {'Instrument1' : {'Reference' : 'd1' }}}]},
66+ 'StandardTemplate' : {
67+ 'Interval' : {'CommonType' : 'Days' , 'Multiplier' : '1' },
68+ 'ShowNonTradedPeriods' : False ,
69+ 'ShowHolidays' : False ,
70+ 'ShowGaps' : True ,
71+ 'XAxis' : {'Range' : {'Fixed' : {'First' : '2015-09-22T00:00:00' ,
72+ 'Last' : '2016-09-22T00:00:00' }}, 'Visible' : True ,
73+ 'Position' : 'Bottom' },
74+ 'Subchart' : [{'YAxis' : [{
75+ 'Analysis' : [{'Reference' : 'a1' }],
76+ 'Visible' : True ,
77+ 'Position' : 'Right' ,
78+ 'Invert' : False ,
79+ 'Logarithmic' : False ,
80+ 'Display' : {'Mode' : 'Automatic' },
81+ 'Range' : {'Automatic' : '' },
82+ }], 'Weight' : 5.0 }, {'YAxis' : [{
83+ 'Analysis' : [{'Reference' : 'a2' }],
84+ 'Visible' : True ,
85+ 'Position' : 'Right' ,
86+ 'Invert' : False ,
87+ 'Logarithmic' : False ,
88+ 'Display' : {'Mode' : 'Automatic' },
89+ 'Range' : {'Automatic' : '' },
90+ }], 'Weight' : 2.0 }],
91+ 'Title' : {'Caption' : {'Visible' : True , 'Customized' : False },
92+ 'Range' : {'Visible' : True }},
93+ 'Legend' : {
94+ 'Visible' : True ,
95+ 'Information' : 'Long' ,
96+ 'Layout' : 'MultiLine' ,
97+ 'Position' : 'Overlaid' ,
98+ },
99+ 'Instrument' : 'Symbol' ,
100+ 'Delimiter' : '%' ,
101+ 'GridLines' : 'None' ,
102+ 'YAxisMarkers' : 'None' ,
103+ 'YAxisTitles' : 'All' ,
104+ 'Brand' : 'None' ,
105+ },
106+ 'Scheme' : {
107+ 'Background' : {
108+ 'BackgroundMode' : 'Solid' ,
109+ 'StartColor' : {'Named' : 'White' },
110+ 'EndColor' : {'Named' : 'White' },
111+ 'HatchStyle' : 'LargeGrid' ,
112+ 'GradientMode' : 'ForwardDiagonal' ,
113+ 'ImageMode' : 'Centered' ,
114+ },
115+ 'Border' : {'Color' : {'RGB' : '139;139;155' },
116+ 'DashStyle' : 'Solid' , 'Width' : 1.0 },
117+ 'GridLines' : {'Color' : {'RGB' : '139;139;155' },
118+ 'DashStyle' : 'Dot' , 'Width' : 1.0 },
119+ 'Title' : {'Caption' : {
120+ 'Color' : {'Named' : 'Black' },
121+ 'Family' : 'Arial' ,
122+ 'Style' : 'Bold' ,
123+ 'Size' : 12.0 ,
124+ }, 'Range' : {
125+ 'Color' : {'Named' : 'Black' },
126+ 'Family' : 'Arial' ,
127+ 'Style' : 'Regular' ,
128+ 'Size' : 8.25 ,
129+ }},
130+ 'Legend' : {
131+ 'Color' : {'Named' : 'Black' },
132+ 'Family' : 'Arial' ,
133+ 'Style' : 'Regular' ,
134+ 'Size' : 8.25 ,
135+ },
136+ 'XAxis' : {'Major' : {
137+ 'Color' : {'Named' : 'Black' },
138+ 'Family' : 'Arial' ,
139+ 'Style' : 'Bold' ,
140+ 'Size' : 9.75 ,
141+ }, 'Minor' : {
142+ 'Color' : {'Named' : 'Black' },
143+ 'Family' : 'Arial' ,
144+ 'Style' : 'Regular' ,
145+ 'Size' : 8.25 ,
146+ }},
147+ 'YAxis' : {'Major' : {
148+ 'Color' : {'Named' : 'Black' },
149+ 'Family' : 'Arial' ,
150+ 'Style' : 'Bold' ,
151+ 'Size' : 9.75 ,
152+ }, 'Minor' : {
153+ 'Color' : {'Named' : 'Black' },
154+ 'Family' : 'Arial' ,
155+ 'Style' : 'Regular' ,
156+ 'Size' : 8.25 ,
157+ }, 'Title' : {
158+ 'Color' : {'Named' : 'Black' },
159+ 'Family' : 'Arial' ,
160+ 'Style' : 'Regular' ,
161+ 'Size' : 8.25 ,
162+ }},
163+ 'Series' : [
164+ {
165+ 'Color' : {'Named' : 'Black' },
166+ 'DashStyle' : 'Solid' ,
167+ 'Width' : 0.0 ,
168+ 'FillColor' : {'Named' : 'Black' },
169+ 'FillStyle' : 'Percent20' ,
170+ },
171+ {
172+ 'Color' : {'Named' : 'Red' },
173+ 'DashStyle' : 'Solid' ,
174+ 'Width' : 0.0 ,
175+ 'FillColor' : {'Named' : 'Red' },
176+ 'FillStyle' : 'Percent20' ,
177+ },
178+ {
179+ 'Color' : {'RGB' : '62;169;0' },
180+ 'DashStyle' : 'Solid' ,
181+ 'Width' : 0.0 ,
182+ 'FillColor' : {'RGB' : '62;169;0' },
183+ 'FillStyle' : 'Percent20' ,
184+ },
185+ {
186+ 'Color' : {'RGB' : '156;38;115' },
187+ 'DashStyle' : 'Solid' ,
188+ 'Width' : 0.0 ,
189+ 'FillColor' : {'RGB' : '156;38;115' },
190+ 'FillStyle' : 'Percent20' ,
191+ },
192+ {
193+ 'Color' : {'RGB' : '255;120;0' },
194+ 'DashStyle' : 'Solid' ,
195+ 'Width' : 0.0 ,
196+ 'FillColor' : {'RGB' : '255;120;0' },
197+ 'FillStyle' : 'Percent20' ,
198+ },
199+ {
200+ 'Color' : {'RGB' : '25;108;229' },
201+ 'DashStyle' : 'Solid' ,
202+ 'Width' : 0.0 ,
203+ 'FillColor' : {'RGB' : '25;108;229' },
204+ 'FillStyle' : 'Percent20' ,
205+ },
206+ {
207+ 'Color' : {'RGB' : '60;117;28' },
208+ 'DashStyle' : 'Solid' ,
209+ 'Width' : 0.0 ,
210+ 'FillColor' : {'RGB' : '60;117;28' },
211+ 'FillStyle' : 'Percent20' ,
212+ },
213+ {
214+ 'Color' : {'RGB' : '230;176;18' },
215+ 'DashStyle' : 'Solid' ,
216+ 'Width' : 0.0 ,
217+ 'FillColor' : {'RGB' : '230;176;18' },
218+ 'FillStyle' : 'Percent20' ,
219+ },
220+ {
221+ 'Color' : {'RGB' : '0;186;193' },
222+ 'DashStyle' : 'Solid' ,
223+ 'Width' : 0.0 ,
224+ 'FillColor' : {'RGB' : '0;186;193' },
225+ 'FillStyle' : 'Percent20' ,
226+ },
227+ {
228+ 'Color' : {'RGB' : '255;178;127' },
229+ 'DashStyle' : 'Solid' ,
230+ 'Width' : 0.0 ,
231+ 'FillColor' : {'RGB' : '255;178;127' },
232+ 'FillStyle' : 'Percent20' ,
233+ },
234+ {
235+ 'Color' : {'RGB' : '100;79;190' },
236+ 'DashStyle' : 'Solid' ,
237+ 'Width' : 0.0 ,
238+ 'FillColor' : {'RGB' : '100;79;190' },
239+ 'FillStyle' : 'Percent20' ,
240+ },
241+ {
242+ 'Color' : {'RGB' : '209;36;33' },
243+ 'DashStyle' : 'Solid' ,
244+ 'Width' : 0.0 ,
245+ 'FillColor' : {'RGB' : '209;36;33' },
246+ 'FillStyle' : 'Percent20' ,
247+ },
248+ {
249+ 'Color' : {'RGB' : '38;87;135' },
250+ 'DashStyle' : 'Solid' ,
251+ 'Width' : 0.0 ,
252+ 'FillColor' : {'RGB' : '38;87;135' },
253+ 'FillStyle' : 'Percent20' ,
254+ },
255+ {
256+ 'Color' : {'RGB' : '94;176;176' },
257+ 'DashStyle' : 'Solid' ,
258+ 'Width' : 0.0 ,
259+ 'FillColor' : {'RGB' : '94;176;176' },
260+ 'FillStyle' : 'Percent20' ,
261+ },
262+ ],
263+ 'LevelLine' : [{'Color' : {'RGB' : '0;0;153' }, 'DashStyle' : 'Solid'
264+ , 'Width' : 1.0 }, {'Color' : {'RGB' : '120;120;120'
265+ }, 'DashStyle' : 'Solid' , 'Width' : 1.0 }],
266+ },
267+ 'ImageType' : 'PNG' ,
268+ 'Width' : 500 ,
269+ 'Height' : 400 ,
270+ 'Culture' : 'en-US' ,
271+ 'ReturnPrivateNetworkURL' : False ,
272+ }}}
273+
274+ interdayURL = 'http://api.rkd.reuters.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2'
275+ headers = {'content-type' : 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID' : appid , 'X-Trkd-Auth-Token' : token }
276+
277+ print '############### Sending Chart request message to TRKD ###############'
278+ chartResult = doSendRequest (interdayURL , chartRequestMsg ,headers )
279+ if chartResult is not None and chartResult .status_code == 200 :
280+ print 'Time Series Interday response message: '
281+ print chartResult .json ()
282+ server = chartResult .json ()['GetChart_Response_2' ]['ChartImageResult' ]['Server' ]
283+ print '\n Server: %s' % (server )
284+ tag = chartResult .json ()['GetChart_Response_2' ]['ChartImageResult' ]['Tag' ]
285+ print 'Tag: %s' % (tag )
286+ imageUrl = chartResult .json ()['GetChart_Response_2' ]['ChartImageResult' ]['Url' ]
287+ print 'Url: %s' % (imageUrl )
288+ return imageUrl
289+
290+ def downloadChartImage (chartURL ):
291+ user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
292+ headers = { 'User-Agent' : user_agent }
293+ print '\n Downlading chart.png file from %s' % (chartURL )
294+ downloadResult = urllib2 .Request (chartURL , headers = headers )
295+ imgData = urllib2 .urlopen (downloadResult ).read ()
296+ fileName = './chart.png'
297+ with open (fileName ,'wb' ) as outfile :
298+ outfile .write (imgData )
299+ print 'save chart.png file complete'
300+
301+
302+
303+ ## ------------------------------------------ Main App ------------------------------------------ ##
304+ ##Get username, password and applicationid
305+ username = raw_input ('Please input username: ' )
306+ ##use getpass.getpass to hide user inputted password
307+ password = getpass .getpass (prompt = 'Please input password: ' )
308+ appid = raw_input ('Please input appid: ' )
309+
310+ token = CreateAuthorization (username ,password ,appid )
311+ print 'Token = %s' % (token )
312+ ## if authentiacation success, continue subscribing Time Series intraday
313+ if token is not None :
314+ chartURL = RetrieveChart (token ,appid )
315+ if chartURL is not None :
316+ print '############### Downloading Chart file from TRKD ###############'
317+ downloadChartImage (chartURL )
318+
319+
320+
321+
322+
323+
324+
325+
326+
327+
328+
329+
330+
331+
0 commit comments