tws_protocol_helper.py 4.1 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. aeMethods = ('ae_req_greeks')
  16. aeEvents = ('ae_req_ack', 'ae_greeks','ae_greeks_snapshot')
  17. def json_loads_ascii(self, data):
  18. def ascii_encode_dict(data):
  19. ascii_encode = lambda x: x.encode('ascii') if isinstance(x, unicode) else x
  20. return dict(map(ascii_encode, pair) for pair in data.items())
  21. return json.loads(data, object_hook=ascii_encode_dict)
  22. class Message(object):
  23. """ This class is taken out from IBpy
  24. everything is the same except it doesn't use __slots__
  25. and thus allow setting variable values
  26. """
  27. def __init__(self, **kwds):
  28. """ Constructor.
  29. @param **kwds keywords and values for instance
  30. """
  31. for name in kwds.keys():
  32. setattr(self, name, kwds[name])
  33. #assert not kwds
  34. def __len__(self):
  35. """ x.__len__() <==> len(x)
  36. """
  37. return len(self.keys())
  38. def __str__(self):
  39. """ x.__str__() <==> str(x)
  40. """
  41. name = self.typeName
  42. items = str.join(', ', ['%s=%s' % item for item in self.items()])
  43. return '<%s%s>' % (name, (' ' + items) if items else '')
  44. def items(self):
  45. """ List of message (slot, slot value) pairs, as 2-tuples.
  46. @return list of 2-tuples, each slot (name, value)
  47. """
  48. return zip(self.keys(), self.values())
  49. def values(self):
  50. """ List of instance slot values.
  51. @return list of each slot value
  52. """
  53. return [getattr(self, key, None) for key in self.keys()]
  54. def keys(self):
  55. """ List of instance slots.
  56. @return list of each slot.
  57. """
  58. return self.__dict__
  59. def dummy():
  60. topicsEvents = ['accountDownloadEnd', 'execDetailsEnd', 'updateAccountTime', 'deltaNeutralValidation', 'orderStatus',\
  61. 'updateAccountValue', 'historicalData', 'openOrderEnd', 'updatePortfolio', 'managedAccounts', 'contractDetailsEnd',\
  62. 'positionEnd', 'bondContractDetails', 'accountSummary', 'updateNewsBulletin', 'scannerParameters', \
  63. 'tickString', 'accountSummaryEnd', 'scannerDataEnd', 'commissionReport', 'error', 'tickGeneric', 'tickPrice', \
  64. 'nextValidId', 'openOrder', 'realtimeBar', 'contractDetails', 'execDetails', 'tickOptionComputation', \
  65. 'updateMktDepth', 'scannerData', 'currentTime', 'error_0', 'error_1', 'tickSnapshotEnd', 'tickSize', \
  66. 'receiveFA', 'connectionClosed', 'position', 'updateMktDepthL2', 'fundamentalData', 'tickEFP']
  67. #s = ''.join('\tdef %s(self, items):\n\t\tself.handlemessage("%s", items)\n\n' % (s,s) for s in topicsEvents)
  68. 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)
  69. return s