Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3.  
  4. import sys
  5. import socket
  6. import json
  7.  
  8. TEAMNAME = "TOMANDJERRYPLUSRAHUL"
  9. order_id = 0
  10. marketSellPrices={}
  11. marketBuyPrices={}
  12. currentBuyOrders={}
  13. currentSellOrders={}
  14. pendingOrders={}
  15. currentStocksInPortfolio={}
  16. currentStocksInPortfolio['BOND']=0
  17. currentStocksInPortfolio['VALBZ']=0
  18. currentStocksInPortfolio['VALE']=0
  19. currentStocksInPortfolio['GS']=0
  20. currentStocksInPortfolio['MS']=0
  21. currentStocksInPortfolio['WFC']=0
  22. currentStocksInPortfolio['XLF']=0
  23.  
  24.  
  25. tradingIsOpen=True
  26.  
  27. exchange=0
  28.  
  29. def connect(mode):
  30. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  31. if mode == 'test':
  32. s.connect(("test-exch-TOMANDJERRYPLUSRAHUL", 25000))
  33. elif mode == 'production':
  34. s.connect(("production", 25000))
  35. return s.makefile('rw', 1)
  36.  
  37. # def connect(mode):
  38. # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  39. # s.connect(("test-exch-TOMANDJERRYPLUSRAHUL", 25000))
  40. # return s.makefile('rw', 1)
  41.  
  42.  
  43. def write(exchange, obj):
  44. json.dump(obj, exchange)
  45. exchange.write("\n")
  46.  
  47. def read(exchange):
  48. msg = exchange.readline()
  49. try:
  50. return json.loads(msg)
  51. except:
  52. return None
  53.  
  54.  
  55. def buyOrder(stockName, price, quantity):
  56. global order_id
  57. write(exchange, {"type": "add", "team": TEAMNAME, "order_id": order_id,
  58. "symbol": stockName, "dir": "BUY", "price": price, "size": quantity})
  59. pendingOrders[order_id]=["BUY",order_id,stockName,price,quantity]
  60. order_id+=1
  61.  
  62. def sellOrder(stockName, price, quantity):
  63. global order_id
  64. write(exchange, {"type": "add", "team": TEAMNAME, "order_id": order_id,
  65. "symbol": stockName, "dir": "SELL", "price": price, "size": quantity})
  66. pendingOrders[order_id]=["SELL",order_id,stockName,price,quantity]
  67. order_id+=1
  68.  
  69. def processAckOrder(orderNum):
  70. data=pendingOrders[orderNum]
  71. #print("processing order",orderNum)
  72. #print("data ", data)
  73. if data[0]=='BUY':
  74. currentBuyOrders[orderNum]=[data[1],data[2],data[3],data[4]]
  75. if data[0]=='SELL':
  76. currentSellOrders[orderNum]=[data[1],data[2],data[3],data[4]]
  77.  
  78.  
  79. def bondArb():
  80. if tradingIsOpen:
  81. if 'BOND' in marketBuyPrices:
  82. bondPrices = marketBuyPrices['BOND']
  83. if len(bondPrices) >= 1 and bondPrices[0][0] > 1000:
  84. sellOrder('BOND', bondPrices[0][0], bondPrices[0][1])
  85. print("currentStocksInPortfolio", currentStocksInPortfolio)
  86. if 'BOND' in marketSellPrices:
  87. bondPrices = marketSellPrices['BOND']
  88. if len(bondPrices) >= 1 and bondPrices[0][0] < 1000:
  89. buyOrder('BOND', bondPrices[0][0], bondPrices[0][1])
  90. print("currentStocksInPortfolio", currentStocksInPortfolio)
  91.  
  92. def bondLiquidity():
  93. return 0
  94.  
  95.  
  96.  
  97.  
  98. def main(mode):
  99. global exchange
  100. global tradingIsOpen
  101. exchange = connect(mode)
  102. write(exchange, {"type": "hello", "team": TEAMNAME})
  103. hello_from_exchange = read(exchange)
  104. print("The exchange replied:", hello_from_exchange, file=sys.stderr)
  105. print("starting trading")
  106. while True:
  107. msg = read(exchange)
  108. if msg is None:
  109. continue
  110. if msg['type']=='book':#update book
  111. # print("updating book")
  112. currentStock=msg['symbol']
  113. buyPrices=msg['buy']
  114. sellPrices=msg['sell']
  115. marketBuyPrices[currentStock]=buyPrices
  116. marketSellPrices[currentStock]=sellPrices
  117. #print("book updated")
  118. #print("market Buy Positions")
  119. #print(marketBuyPrices)
  120. #print(marketSellPrices)
  121. if msg['type']=='ack':
  122. ackOrder=msg['order_id']
  123. processAckOrder(ackOrder)
  124. print("order ack: ", ackOrder)
  125. if msg['type']=='fill':
  126. orderNum=msg['order_id']
  127. numFilled=msg['size']
  128. direction=msg['dir']
  129. stockNameFilled=msg['symbol']
  130. print("order filled: ", stockNameFilled, " ", direction, " ", numFilled)
  131. if orderNum in pendingOrders:# if we never received ack
  132. processAckOrder(orderNum)
  133. if direction=='BUY':
  134. currentBuyOrders[orderNum][3]-=numFilled
  135. currentStocksInPortfolio[stockNameFilled]+=numFilled
  136. if direction=='SELL':
  137. currentSellOrders[orderNum][3]-=numFilled
  138. currentStocksInPortfolio[stockNameFilled]-=numFilled
  139. if msg['type']=="out":
  140. orderNum=msg['order_id']
  141. print("order out ", orderNum)
  142. if orderNum in currentBuyOrders:
  143. del currentBuyOrders[orderNum]
  144. if orderNum in currentSellOrders:
  145. del currentSellOrders[orderNum]
  146. if msg['type']=='close':
  147. tradingIsOpen=False
  148.  
  149. # call your strategies here
  150. bondArb()
  151.  
  152.  
  153.  
  154.  
  155.  
  156. if __name__ == "__main__":
  157. if len(sys.argv) != 2:
  158. print("Use case: python [filename.py] [test/production]")
  159. else:
  160. main(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement