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+
17+
18+ def doSendRequest (url , requestMsg , headers ):
19+ result = None
20+ try :
21+ ##send request
22+ result = requests .post (url , data = json .dumps (requestMsg ), headers = headers )
23+ if result .status_code == 500 :
24+ print 'Request fail'
25+ print 'response status %s' % result .status_code
26+ print 'Error: %s' % result .json ()
27+ sys .exit (1 )
28+ except requests .exceptions .RequestException , e :
29+ print 'Exception!!!'
30+ print e
31+ sys .exit (1 )
32+ return result
33+
34+
35+ ## Perform authentication
36+ def CreateAuthorization (username , password , appid ):
37+ token = None
38+ ##create authentication request URL, message and header
39+ authenMsg = {'CreateServiceToken_Request_1' : { 'ApplicationID' :appid , 'Username' :username ,'Password' :password }}
40+ authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
41+ headers = {'content-type' : 'application/json;charset=utf-8' }
42+ print '############### Sending Authentication request message to TRKD ###############'
43+ authenResult = doSendRequest (authenURL , authenMsg , headers )
44+ if authenResult is not None and authenResult .status_code == 200 :
45+ print 'Authen success'
46+ print 'response status %s' % (authenResult .status_code )
47+ ##get Token
48+ token = authenResult .json ()['CreateServiceToken_Response_1' ]['Token' ]
49+
50+ return token
51+
52+ ## Perform Quote request
53+ def RetrieveIntraday (token , appid ):
54+
55+ ''''
56+ ricName = raw_input('Please input Symbol: ')
57+ fieldFiltering = raw_input('Subscribe all Field? (Yes|No)')
58+ intradayRequestMsg = None
59+ fieldsName = 'CF_LAST:CF_HIGH:CF_LOW:CF_BID:CF_ASK:CF_YIELD:CF_SOURCE:CF_SRC_PAGE:CF_LOTSIZE:CF_DATE:CF_TIME:CF_TICK:CF_NETCHNG:CF_EXCHNG:CF_VOLUME:CF_CLOSE:CF_OPEN:CF_NAME:CF_CURRENCY'
60+ if fieldFiltering == 'Yes':
61+ ## Request all Fields
62+ intradayRequestMsg = \
63+ {'RetrieveItem_Request_3': {'TrimResponse': False,
64+ 'ItemRequest': [{'RequestKey': [{'Name': ricName, 'NameType': 'RIC'}], 'Scope': 'All',
65+ 'ProvideChainLinks': True}]}}
66+ elif fieldFiltering == 'No':
67+ ## Request specific Fields
68+ fieldsName = raw_input('Input interested Field Name in the following format (BID:ASK:TRDPRC_1)')
69+ intradayRequestMsg = \
70+ {'RetrieveItem_Request_3': {'TrimResponse': False,
71+ 'ItemRequest': [{
72+ 'RequestKey': [{'Name': ricName, 'NameType': 'RIC'}],
73+ 'Fields': fieldsName,
74+ 'Scope': 'List',
75+ 'ProvideChainLinks': True,
76+ }]}}
77+ '''
78+ ricName = raw_input ('Please input Symbol: ' )
79+ intradayRequestMsg = None
80+ intradayRequestMsg = {
81+ 'GetIntradayTimeSeries_Request_4' :{
82+ 'Field' : ['OPEN' ,'HIGH' ,'LOW' ,'CLOSE' ,'CLOSEYIELD' ,'VOLUME' ,'BID' ,'ASK' ],
83+ 'TrimResponse' : True ,
84+ 'Symbol' : ricName ,
85+ 'StartTime' :'2016-09-12T00:00:00' ,
86+ 'EndTime' :'2016-09-19T23:59:00' ,
87+ 'Interval' :'MINUTE'
88+ }
89+ }
90+
91+ intradayURL = 'http://api.rkd.reuters.com/api/TimeSeries/TimeSeries.svc/REST/TimeSeries_1/GetIntradayTimeSeries_4'
92+ headers = {'content-type' : 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID' : appid , 'X-Trkd-Auth-Token' : token }
93+
94+ print '############### Sending Time Series Intraday request message to TRKD ###############'
95+ intradayResult = doSendRequest (intradayURL , intradayRequestMsg ,headers )
96+ if intradayResult is not None and intradayResult .status_code == 200 :
97+ print 'Time Series Intraday response message: '
98+ print intradayResult .json ()
99+
100+
101+ ## ------------------------------------------ Main App ------------------------------------------ ##
102+ ##Get username, password and applicationid
103+ username = raw_input ('Please input username: ' )
104+ ##use getpass.getpass to hide user inputted password
105+ password = getpass .getpass (prompt = 'Please input password: ' )
106+ appid = raw_input ('Please input appid: ' )
107+
108+
109+ token = CreateAuthorization (username ,password ,appid )
110+ print 'Token = %s' % (token )
111+ ## if authentiacation success, continue subscribing Time Series intraday
112+ if token is not None :
113+ RetrieveIntraday (token ,appid )
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
0 commit comments