helpers.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import logging
  4. import threading
  5. from ib.ext.Contract import Contract
  6. from ib.ext.Order import Order
  7. from ib.ext.ExecutionFilter import ExecutionFilter
  8. class BaseHelper():
  9. @staticmethod
  10. def object2kvstring(o):
  11. return json.dumps(o.__dict__)
  12. @staticmethod
  13. def kvstring2object(kvstring, object):
  14. return BaseHelper.kv2object(json.loads(kvstring), object)
  15. @staticmethod
  16. def kv2object(kv, Object):
  17. o = Object()
  18. map(lambda x: o.__setattr__(x, kv[x].encode('ascii') if type(kv[x]) == unicode else kv[x]), kv.keys())
  19. return o
  20. class OrderHelper(BaseHelper):
  21. pass
  22. # @staticmethod
  23. # def object2kvstring(contract):
  24. # return json.dumps(contract.__dict__)
  25. #
  26. # @staticmethod
  27. # def kvstring2object(sm_order):
  28. # return OrderHelper.kv2object(json.loads(sm_order))
  29. #
  30. #
  31. # @staticmethod
  32. # def kv2object(m_order):
  33. # newOrder = Order()
  34. # map(lambda x: newOrder.__setattr__(x, m_order[x].encode('ascii') if type(m_order[x]) == unicode else m_order[x]), m_order.keys())
  35. # return newOrder
  36. class ExecutionFilterHelper(BaseHelper):
  37. pass
  38. class ContractHelper(BaseHelper):
  39. def __init__(self, contractTuple):
  40. self.makeContract(contractTuple)
  41. @staticmethod
  42. def makeContract(contractTuple):
  43. newContract = Contract()
  44. newContract.m_symbol = contractTuple[0]
  45. newContract.m_secType = contractTuple[1]
  46. newContract.m_exchange = contractTuple[2]
  47. newContract.m_currency = contractTuple[3]
  48. newContract.m_expiry = contractTuple[4]
  49. newContract.m_strike = contractTuple[5]
  50. newContract.m_right = contractTuple[6]
  51. logging.debug( 'Contract Values:%s,%s,%s,%s,%s,%s,%s:' % contractTuple)
  52. return newContract
  53. @staticmethod
  54. def convert2Tuple(newContract):
  55. newContractTuple = (newContract.m_symbol,\
  56. newContract.m_secType,\
  57. newContract.m_exchange,\
  58. newContract.m_currency,\
  59. newContract.m_expiry,\
  60. newContract.m_strike,\
  61. newContract.m_right, newContract.m_conId)
  62. logging.debug( 'Contract Values:%s,%s,%s,%s,%s,%s,%s %s:' % newContractTuple)
  63. return newContractTuple
  64. @staticmethod
  65. def contract2kvstring(contract):
  66. return json.dumps(contract.__dict__)
  67. @staticmethod
  68. def kvstring2contract(sm_contract):
  69. return ContractHelper.kv2contract(json.loads(sm_contract))
  70. @staticmethod
  71. def kv2contract(m_contract):
  72. newContract = Contract()
  73. map(lambda x: newContract.__setattr__(x, m_contract[x].encode('ascii') if type(m_contract[x]) == unicode else m_contract[x]), m_contract.keys())
  74. return newContract
  75. @staticmethod
  76. def printContract(contract):
  77. s = '[%s-%s-%s-%s-%s-%s-%s-%s]' % (contract.m_symbol,
  78. contract.m_secType,
  79. contract.m_exchange,
  80. contract.m_currency,
  81. contract.m_expiry,
  82. contract.m_strike,
  83. contract.m_right,
  84. contract.m_conId)
  85. #logging.info(s)
  86. return s
  87. @staticmethod
  88. def makeRedisKey(contract):
  89. #print "makerediskey %s" % ContractHelper.printContract(contract)
  90. #20150904
  91. contract.m_strike = int(contract.m_strike)
  92. #contract.m_strike = contract.m_strike
  93. if contract.m_secType == 'OPT':
  94. s = '%s-%s-%s-%s' % (contract.m_symbol,
  95. contract.m_expiry,
  96. contract.m_strike,
  97. contract.m_right)
  98. else:
  99. s = '%s-%s-%s-%s' % (contract.m_symbol,
  100. contract.m_expiry,
  101. contract.m_secType, '')
  102. return s
  103. @staticmethod
  104. def makeRedisKeyEx(contract, old=False):
  105. # this routine is to circumvent a problem in makeRedisKey with
  106. # the key in position 3 having different meanings under different conditions.
  107. #
  108. #
  109. # to differentiate the keys generated by the old and new functions,
  110. # contract keys created using this routine have their last slot
  111. # hard coded a magic number 102
  112. if (old):
  113. return ContractHelper.makeRedisKey(contract)
  114. #contract.m_strike = int(contract.m_strike)
  115. contract.m_strike = contract.m_strike
  116. # amend 10/22
  117. # add exchange to the key
  118. # s = '%s-%s-%s-%s-%s-%s-%s-%d' % (contract.m_symbol,
  119. # contract.m_expiry,
  120. # contract.m_strike,
  121. # contract.m_right,
  122. # contract.m_secType,
  123. # contract.m_currency,
  124. #
  125. # contract.m_exchange,
  126. #
  127. # 102)
  128. # amend 12/1
  129. #change strike format to 2 dp
  130. s = '%s-%s-%.2f-%s-%s-%s-%s-%d' % (contract.m_symbol,
  131. contract.m_expiry,
  132. float(contract.m_strike),
  133. contract.m_right,
  134. contract.m_secType,
  135. contract.m_currency,
  136. contract.m_exchange,
  137. 102)
  138. return s
  139. # def str2dict(s):
  140. # return ast.literal_eval(s)
  141. def dict2str(dict):
  142. # enclose strings in double quotes
  143. return '{' + ', '.join('"%s" : %s' % (k, '"%s"' % v if type(v) == str else v) for k, v in dict.iteritems()) + '}'