Advertisement
Guest User

Untitled

a guest
Nov 30th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import collections
  5. import datetime
  6. import glob
  7. import simplejson
  8. import time
  9. import traceback
  10.  
  11. from ib.ext.CommissionReport import CommissionReport
  12. from ib.ext.Contract import Contract
  13. from ib.ext.ExecutionFilter import ExecutionFilter
  14. from ib.opt import ibConnection, message
  15.  
  16. import ftp_utils
  17.  
  18.  
  19. class EphemeralIBSession(object):
  20. def __init__(self):
  21. self.conn = ibConnection(clientId=17)
  22. self.conn.register(self.update_next_valid_id, "NextValidId")
  23. self.conn.register(self.update_account_value_handler, "UpdateAccountValue")
  24. self.conn.register(self.update_executions_handler, "ExecDetails")
  25. self.conn.register(self.update_commissions_handler, "CommissionReport")
  26.  
  27. def init(self):
  28. self.account = collections.OrderedDict()
  29. self.fills = []
  30. self.fill_by_exec_id = {}
  31. self.next_order_id = None
  32. self.reset_last_updated()
  33.  
  34. def reset_last_updated(self):
  35. self.last_update_ts = time.time()
  36.  
  37. def time_since_last_update(self):
  38. return time.time() - self.last_update_ts
  39.  
  40. def to_dict(self):
  41. res = collections.OrderedDict()
  42. res["account"] = self.account
  43. res["fills"] = self.fills
  44. return res
  45.  
  46. def update_next_valid_id(self, msg):
  47. self.next_order_id = msg.orderId
  48. self.reset_last_updated()
  49.  
  50. def update_account_value_handler(self, msg):
  51. self.account[msg.key] = collections.OrderedDict(msg.items())
  52. if msg.currency is not None:
  53. try:
  54. self.account[msg.key]["value"] = float(self.account[msg.key]["value"])
  55. except:
  56. pass
  57.  
  58. self.reset_last_updated()
  59.  
  60. def update_executions_handler(self, msg):
  61. fill = {
  62. "ticker": msg.contract.m_symbol,
  63. "currency": msg.contract.m_currency,
  64. "price": msg.execution.m_price,
  65. "sqty": msg.execution.m_shares*(+1 if msg.execution.m_side == "BOT" else -1),
  66. "time": msg.execution.m_time,
  67. "exch": msg.execution.m_exchange,
  68. "order_id": msg.execution.m_permId,
  69. "exec_id": msg.execution.m_execId,
  70. "sec_type": msg.contract.m_secType,
  71. "fees": 0.0, # to be filled out by commissions callback
  72. "fees_currency": None # to be filled out by commissions callback
  73. }
  74.  
  75. self.fills.append(fill)
  76. self.fill_by_exec_id[fill["exec_id"]] = fill
  77. self.reset_last_updated()
  78.  
  79. def update_commissions_handler(self, msg):
  80. self.fill_by_exec_id[msg.commissionReport.m_execId]["fees"] = msg.commissionReport.m_commission
  81. self.fill_by_exec_id[msg.commissionReport.m_execId]["fees_currency"] = msg.commissionReport.m_currency
  82. self.reset_last_updated()
  83.  
  84. def run(self):
  85. self.init()
  86.  
  87. try:
  88. self.conn.connect()
  89.  
  90. start_time = time.time()
  91. while self.next_order_id is None:
  92. if time.time() - start_time >= 10:
  93. raise Exception("unable to connect")
  94. time.sleep(0.1)
  95.  
  96. self.conn.reqAccountUpdates(1, "")
  97. self.conn.reqExecutions(1, ExecutionFilter())
  98. except:
  99. traceback.print_exc()
  100. else:
  101. while self.time_since_last_update() <= 2:
  102. time.sleep(0.1)
  103. finally:
  104. self.conn.disconnect()
  105.  
  106.  
  107. if __name__ == '__main__':
  108. sess = EphemeralIBSession()
  109. sess.run()
  110.  
  111. results = sess.to_dict()
  112. if results["account"]:
  113. with open("{}.json".format(datetime.date.today().strftime("%Y%m%d")), "w") as f:
  114. simplejson.dump(results, f, indent=2)
  115.  
  116. ftp_utils.upload_to_data(glob.glob("*.json"))
  117. dates = sorted(set(x["time"].split(" ")[0] for x in results["fills"]))
  118. print("dates fetched: {}".format(dates))
  119.  
  120. raw_input("Press any key to continue...")
  121.  
  122.  
  123.  
  124.  
  125.  
  126. import os
  127. import paramiko
  128. import traceback
  129.  
  130.  
  131. def upload(filenames, destination, remove_original=True):
  132. if not hasattr(filenames, '__iter__'):
  133. filenames = [filenames]
  134.  
  135. if len(filenames) == 0:
  136. return
  137.  
  138. host = "billtable.com"
  139. port = 22
  140. transport = paramiko.Transport((host, port))
  141.  
  142.  
  143. transport.connect(username=username, password=password)
  144.  
  145. sftp = paramiko.SFTPClient.from_transport(transport)
  146.  
  147. try:
  148. for fname in filenames:
  149. filepath = "{}/{}".format(destination, os.path.basename(fname))
  150. sftp.put(fname, filepath)
  151. print("copied {!r} to {!r}".format(fname, filepath))
  152. if remove_original:
  153. os.unlink(fname)
  154. except:
  155. traceback.print_exc()
  156. finally:
  157. sftp.close()
  158. transport.close()
  159.  
  160.  
  161. def upload_to_data(filenames):
  162. upload(filenames, "/root/projects/tb/data")
  163.  
  164.  
  165. def upload_to_nc(filenames):
  166. upload(filenames, "/root/projects/tb/populate/source")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement