question

Upvotes
Accepted
488 12 13 27

Tradelift - Python API

Do we have any documentation or python script to work with the API?
pythontick-history-rest-api
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

1 Answer

Upvotes
Accepted
791 9 7 14
Here is a Python script which tests the Tick History API by calling the GetVersion() method, followed by the SearchRICs method. It also contains some code to support the ExpandChain, SubmitFTPRequest, GetLookBackPeriod, and SubmitRequest methods. It requires the python suds module to be installed, which is a lightweight SOAP library.
    from suds.client import Client
    from suds import WebFault

    import logging
    import getpass
    import datetime
    import time

    #Begin test gzip format
    import gzip
    from cStringIO import StringIO
    import binascii
    #End test gzip format

    WSDL_URL = "https://trth-api.thomsonreuters.com/TRTHApi-5.8/wsdl/TRTHApi.wsdl"

   
  #===============================================================================
    def createDataObject( type, field, value, longName = "" ):
      data = client.factory.create(
  'ns0:Data' )
      data.type.value = type
      data.field.value = field
      data.value = value
      data.longName = longName
      return data

    #===============================================================================
    def getTokenId( message ):
      startIndex = str( message ).find(
  "<typens:tokenId>" )
      endIndex  = str( message ).find(
  "</typens:tokenId>" )
      return str( message
  )[startIndex+16:endIndex]

    #===============================================================================
    # Based on the SEARCHRICS example on page 37 of the TRTH API User Guide
  5.7.2
   
  #===============================================================================
    def searchRICs( client ):
      print
  "---------------------"
      print
  "SearchRICs()"
      print
  "---------------------"

      # set date range
      dateRange =
  client.factory.create( 'ns0:DateRange' )
      dateRange.start = "2000-01-01"
      dateRange.end =
  "2006-12-31"

      # create data objects
      domain  = createDataObject( "Text",
  "Domain", "EQU" )
      exchange = createDataObject(
  "Text", "Exchange", "NMS" )
      ric  = createDataObject( "Text",
  "RICRegex", "^MSFT" )

      # create criteria and add data
  objects
      criteria = client.factory.create(
  'ns0:ArrayOfData' )
      criteria.data.append( domain
  )
      criteria.data.append( exchange
  )
      criteria.data.append( ric )

      # search RICs
      try:
      result =
  client.service.SearchRICs( dateRange, criteria, False )
      #print "Result
  length:", len(result['instrument'])
      #for res in
  result['instrument']:
      #  print res.code
      print "result:",
  result

      except WebFault, f:
      print "f:", f
      print "f.fault:",
  f.fault  

   
  #===============================================================================
    # Based on the EXPANDCHAIN example on page 39 of the TRTH API User Guide
  5.7.2
   
  #===============================================================================
    def expandChain( client ):
      print "---------------------"
      print
  "ExpandChain()"
      print
  "---------------------"

      # create an instrument
      instrument =
  client.factory.create( 'ns0:Instrument' )
      instrument.code =
  "0#.DJI"
      instrument.status = None

      # create a date range
      dateRange =
  client.factory.create( 'ns0:DateRange' )
      dateRange.start =
  "2006-07-01"
      dateRange.end =
  "2006-07-31"

      # create a time range
      timeRange =
  client.factory.create( 'ns0:TimeRange' )
      timeRange.start =
  "0:00"
      timeRange.end =
  "23:59:59.999"

      # expand chain
      try:
      result =
  client.service.ExpandChain( instrument,
     
  dateRange,
     
  timeRange,
     
  True )
      print "result:",
  result
      except WebFault, f:
      print "f:", f
      print "f.fault:",
  f.fault

   
  #===============================================================================
    # Get the Look Back Period Information
    #===============================================================================
    def GetLookBackPeriod( client ):
      print
  "---------------------"
      print "GetLookBackPeriod()"
      print
  "---------------------"

      # expand chain
      try:
      result =
  client.service.GetLookBackPeriod()
      print "result:",
  result
      except WebFault, f:
      print "f:", f
      print "f.fault:",
  f.fault


    #===============================================================================
    # Based on the SUBMITFTPREQUEST example on page 46 of the TRTH API User
  Guide 5.7.2
   
  #===============================================================================
    def submitFTPRequest( client ):
      print
  "---------------------"
      print
  "submitFTPRequest()"
      print "---------------------"

      # create an instrument
      instrument =
  client.factory.create( 'ns0:Instrument' )
      instrument.code =
  "BHP.AX"
      instrument.status = None

      instrumentList =
  client.factory.create("ns0:ArrayOfInstrument")
      instrumentList.instrument =
  [instrument]

      # create a date range
      dateRange =
  client.factory.create( 'ns0:DateRange' )
      dateRange.start =
  "2006-07-01"
      dateRange.end =
  "2006-07-31"

      # create a time range
      timeRange =
  client.factory.create( 'ns0:TimeRange' )
      timeRange.start =
  "0:00"
      timeRange.end =
  "23:59:59.999"

      # create message type
      fields =
  ["Open","High", "Low"]
      messageType =
  client.factory.create("ns0:MessageType")
      messageType.name = "Intraday
  10Min"
      fieldArray =
  client.factory.create("ns0:ArrayOfString")
      fieldArray.string = fields
      messageType.fieldList =
  fieldArray

      typesArray =
  client.factory.create("ns0:ArrayOfMessageType")
      typesArray.messageType =
  [messageType]


      # create a LargeRequestSpec
      request =
  client.factory.create("ns0:LargeRequestSpec")
      request.friendlyName =
  "Intraday FTP Request"
      request.requestType =
  (client.factory.create("ns0:RequestType")).Intraday
      request.instrumentList =
  instrumentList
      request.dateRange =
  dateRange
      request.timeRange =
  timeRange
      request.messageTypeList = typesArray
      request.requestInGMT =
  False
      request.displayInGMT =
  False
      request.marketDepth = 0
      request.splitSize = 500
      request.delivery =
  (client.factory.create("ns0:RequestDelivery")).Pull
      request.sortType =
  (client.factory.create("ns0:RequestSortType")).RICSequence
      request.fileFormat =
  (client.factory.create("ns0:RequestFileFormat")).Single
      request.dateFormat =
  (client.factory.create("ns0:RequestDateFormat")).DDMMYYYY
      request.applyCorrections =
  False
      request.displayMicroseconds =
  False
      request.disableDataPersistence =
  True
      request.includeCurrentRIC=
  True
      print request

      # submit request
      try:
      reqID =
  client.service.SubmitFTPRequest(request)
      print "Request:",
  reqID

      except WebFault, f:
      print "f:", f
      print "f.fault:",
  f.fault

   
  #===============================================================================
    # 
    #===============================================================================
    def submitRequest( client ):
      print
  "---------------------"
      print
  "submitRequest()"
      print
  "---------------------"

      # create an instrument
      instrument =
  client.factory.create( 'ns0:Instrument' )
      instrument.code =
  "AUD=D3"
      instrument.status = None

      # create a date range
      date = client.factory.create(
  'xsd:date' )
      date =
  "2014-01-02"

      # create a time range
      timeRange =
  client.factory.create( 'ns0:TimeRange' )
      timeRange.start =
  "0:00"
      timeRange.end = "23:59:59.999"

      # create a LargeRequestSpec
      request =
  client.factory.create("ns0:RequestSpec")
      request.friendlyName =
  "Single Day Request"
      request.requestType =
  (client.factory.create("ns0:RequestType")).TimeAndSales
      request.instrument =
  instrument
      request.date = date
      request.timeRange =
  timeRange
      #request.messageTypeList =
  typesArray
      request.requestInGMT =
  False
      request.displayInGMT =
  False
      request.disableHeader =
  False
      request.marketDepth = 0
      request.dateFormat =
  (client.factory.create("ns0:RequestDateFormat")).DDMMYYYY
      request.applyCorrections =
  False
      request.displayMicroseconds =
  False
      request.disableDataPersistence =
  True
      request.includeCurrentRIC=
  True
      print request

      # submit request
      try:
      reqID =
  client.service.SubmitRequest(request)
      print "Request:",
  reqID[1]

      except WebFault, f:
      print "f:", f
      print "f.fault:",
  f.fault

      complete = False
      while(True):
      res =
  client.service.GetRequestResult(reqID[1]);
      print "======Status:
  ", res
      if(res[1]['status'] ==
  (client.factory.create("ns0:RequestStatusCode")).Complete or
  res[1]['status'] == (client.factory.create("ns0:RequestStatusCode")).Aborted):
      if(res[1]['status'] ==
  (client.factory.create("ns0:RequestStatusCode")).Complete):
      complete = True
      break;
      time.sleep(10)

      if(complete == True):  
      fileR = open('result.csv.gz',
  'w')
     
  fileR.write(res[1]['data'])
      fileR.close()

   
  #===============================================================================
    if __name__ == "__main__":
      # create suds client from TRTH
  API url
      client = Client( WSDL_URL,
  faults=False )

      logging.basicConfig()
      logging.getLogger('suds.client').setLevel(logging.DEBUG)

      # get username
      username = raw_input( "Enter
  username:" )

      # get password (TRTH)
      password = getpass.getpass(
  'Enter a password:' )

      # create credentials object
      credentials =
  client.factory.create( 'ns0:CredentialsHeader' )
      credentials.username =
  username
      credentials.password =
  password

      # add credentials to header
      client.set_options( soapheaders =
  credentials )

      # get version
      version =
  client.service.GetVersion()

      # add tokenID to credentials and
  add to header
      credentials.tokenId = getTokenId(
  client.last_received() )
      client.set_options( soapheaders =
  credentials )

      # attempt to make requests using
  the API
      searchRICs( client )
      # expandChain( client )
      # submitFTPRequest( client
  )
      # GetLookBackPeriod( client
  )
      # submitRequest( client 
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea