tws_protocol_helper.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. class TWS_Protocol:
  5. topicMethods = ('eConnect', 'reqAccountUpdates', 'reqOpenOrders', 'reqExecutions', 'reqIds', 'reqNewsBulletins', 'cancelNewsBulletins', \
  6. 'setServerLogLevel', 'reqAccountSummary', 'reqPositions', 'reqAutoOpenOrders', 'reqAllOpenOrders', 'reqManagedAccts',\
  7. 'requestFA', 'reqMktData', 'reqHistoricalData', 'placeOrder', 'eDisconnect')
  8. topicEvents = ('accountDownloadEnd', 'execDetailsEnd', 'updateAccountTime', 'deltaNeutralValidation', 'orderStatus',\
  9. 'updateAccountValue', 'historicalData', 'openOrderEnd', 'updatePortfolio', 'managedAccounts', 'contractDetailsEnd',\
  10. 'positionEnd', 'bondContractDetails', 'accountSummary', 'updateNewsBulletin', 'scannerParameters', \
  11. 'tickString', 'accountSummaryEnd', 'scannerDataEnd', 'commissionReport', 'error', 'tickGeneric', 'tickPrice', \
  12. 'nextValidId', 'openOrder', 'realtimeBar', 'contractDetails', 'execDetails', 'tickOptionComputation', \
  13. 'updateMktDepth', 'scannerData', 'currentTime', 'error_0', 'error_1', 'tickSnapshotEnd', 'tickSize', \
  14. 'receiveFA', 'connectionClosed', 'position', 'updateMktDepthL2', 'fundamentalData', 'tickEFP')
  15. gatewayMethods = ('gw_req_subscriptions',)
  16. gatewayEvents = ('gw_subscriptions',)
  17. oceMethods = ()
  18. oceEvents = ('optionAnalytics','optionsSnapshot')
  19. def json_loads_ascii(self, data):
  20. def ascii_encode_dict(data):
  21. ascii_encode = lambda x: x.encode('ascii') if isinstance(x, unicode) else x
  22. return dict(map(ascii_encode, pair) for pair in data.items())
  23. return json.loads(data, object_hook=ascii_encode_dict)
  24. class Message(object):
  25. """ This class is taken out from IBpy
  26. everything is the same except it doesn't use __slots__
  27. and thus allow setting variable values
  28. """
  29. def __init__(self, **kwds):
  30. """ Constructor.
  31. @param **kwds keywords and values for instance
  32. """
  33. for name in kwds.keys():
  34. setattr(self, name, kwds[name])
  35. #assert not kwds
  36. def __len__(self):
  37. """ x.__len__() <==> len(x)
  38. """
  39. return len(self.keys())
  40. def __str__(self):
  41. """ x.__str__() <==> str(x)
  42. """
  43. name = self.typeName
  44. items = str.join(', ', ['%s=%s' % item for item in self.items()])
  45. return '<%s%s>' % (name, (' ' + items) if items else '')
  46. def items(self):
  47. """ List of message (slot, slot value) pairs, as 2-tuples.
  48. @return list of 2-tuples, each slot (name, value)
  49. """
  50. return zip(self.keys(), self.values())
  51. def values(self):
  52. """ List of instance slot values.
  53. @return list of each slot value
  54. """
  55. return [getattr(self, key, None) for key in self.keys()]
  56. def keys(self):
  57. """ List of instance slots.
  58. @return list of each slot.
  59. """
  60. return self.__dict__
  61. def dummy():
  62. topicsEvents = ['accountDownloadEnd', 'execDetailsEnd', 'updateAccountTime', 'deltaNeutralValidation', 'orderStatus',\
  63. 'updateAccountValue', 'historicalData', 'openOrderEnd', 'updatePortfolio', 'managedAccounts', 'contractDetailsEnd',\
  64. 'positionEnd', 'bondContractDetails', 'accountSummary', 'updateNewsBulletin', 'scannerParameters', \
  65. 'tickString', 'accountSummaryEnd', 'scannerDataEnd', 'commissionReport', 'error', 'tickGeneric', 'tickPrice', \
  66. 'nextValidId', 'openOrder', 'realtimeBar', 'contractDetails', 'execDetails', 'tickOptionComputation', \
  67. 'updateMktDepth', 'scannerData', 'currentTime', 'error_0', 'error_1', 'tickSnapshotEnd', 'tickSize', \
  68. 'receiveFA', 'connectionClosed', 'position', 'updateMktDepthL2', 'fundamentalData', 'tickEFP']
  69. #s = ''.join('\tdef %s(self, items):\n\t\tself.handlemessage("%s", items)\n\n' % (s,s) for s in topicsEvents)
  70. s = ''.join('# override this function to perform your own processing\n#\tdef %s(self, items):\n#\t\tpass\n\n' % s for s in topicsEvents)
  71. return s