Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from __future__ import print_function
- import sys
- import socket
- import json
- TEAMNAME = "TOMANDJERRYPLUSRAHUL"
- order_id = 0
- marketSellPrices={}
- marketBuyPrices={}
- currentBuyOrders={}
- currentSellOrders={}
- pendingOrders={}
- currentStocksInPortfolio={}
- currentStocksInPortfolio['BOND']=0
- currentStocksInPortfolio['VALBZ']=0
- currentStocksInPortfolio['VALE']=0
- currentStocksInPortfolio['GS']=0
- currentStocksInPortfolio['MS']=0
- currentStocksInPortfolio['WFC']=0
- currentStocksInPortfolio['XLF']=0
- tradingIsOpen=True
- exchange=0
- def connect(mode):
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- if mode == 'test':
- s.connect(("test-exch-TOMANDJERRYPLUSRAHUL", 25000))
- elif mode == 'production':
- s.connect(("production", 25000))
- return s.makefile('rw', 1)
- # def connect(mode):
- # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # s.connect(("test-exch-TOMANDJERRYPLUSRAHUL", 25000))
- # return s.makefile('rw', 1)
- def write(exchange, obj):
- json.dump(obj, exchange)
- exchange.write("\n")
- def read(exchange):
- msg = exchange.readline()
- try:
- return json.loads(msg)
- except:
- return None
- def buyOrder(stockName, price, quantity):
- global order_id
- write(exchange, {"type": "add", "team": TEAMNAME, "order_id": order_id,
- "symbol": stockName, "dir": "BUY", "price": price, "size": quantity})
- pendingOrders[order_id]=["BUY",order_id,stockName,price,quantity]
- order_id+=1
- def sellOrder(stockName, price, quantity):
- global order_id
- write(exchange, {"type": "add", "team": TEAMNAME, "order_id": order_id,
- "symbol": stockName, "dir": "SELL", "price": price, "size": quantity})
- pendingOrders[order_id]=["SELL",order_id,stockName,price,quantity]
- order_id+=1
- def processAckOrder(orderNum):
- data=pendingOrders[orderNum]
- #print("processing order",orderNum)
- #print("data ", data)
- if data[0]=='BUY':
- currentBuyOrders[orderNum]=[data[1],data[2],data[3],data[4]]
- if data[0]=='SELL':
- currentSellOrders[orderNum]=[data[1],data[2],data[3],data[4]]
- def bondArb():
- if tradingIsOpen:
- if 'BOND' in marketBuyPrices:
- bondPrices = marketBuyPrices['BOND']
- if len(bondPrices) >= 1 and bondPrices[0][0] > 1000:
- sellOrder('BOND', bondPrices[0][0], bondPrices[0][1])
- print("currentStocksInPortfolio", currentStocksInPortfolio)
- if 'BOND' in marketSellPrices:
- bondPrices = marketSellPrices['BOND']
- if len(bondPrices) >= 1 and bondPrices[0][0] < 1000:
- buyOrder('BOND', bondPrices[0][0], bondPrices[0][1])
- print("currentStocksInPortfolio", currentStocksInPortfolio)
- def bondLiquidity():
- return 0
- def main(mode):
- global exchange
- global tradingIsOpen
- exchange = connect(mode)
- write(exchange, {"type": "hello", "team": TEAMNAME})
- hello_from_exchange = read(exchange)
- print("The exchange replied:", hello_from_exchange, file=sys.stderr)
- print("starting trading")
- while True:
- msg = read(exchange)
- if msg is None:
- continue
- if msg['type']=='book':#update book
- # print("updating book")
- currentStock=msg['symbol']
- buyPrices=msg['buy']
- sellPrices=msg['sell']
- marketBuyPrices[currentStock]=buyPrices
- marketSellPrices[currentStock]=sellPrices
- #print("book updated")
- #print("market Buy Positions")
- #print(marketBuyPrices)
- #print(marketSellPrices)
- if msg['type']=='ack':
- ackOrder=msg['order_id']
- processAckOrder(ackOrder)
- print("order ack: ", ackOrder)
- if msg['type']=='fill':
- orderNum=msg['order_id']
- numFilled=msg['size']
- direction=msg['dir']
- stockNameFilled=msg['symbol']
- print("order filled: ", stockNameFilled, " ", direction, " ", numFilled)
- if orderNum in pendingOrders:# if we never received ack
- processAckOrder(orderNum)
- if direction=='BUY':
- currentBuyOrders[orderNum][3]-=numFilled
- currentStocksInPortfolio[stockNameFilled]+=numFilled
- if direction=='SELL':
- currentSellOrders[orderNum][3]-=numFilled
- currentStocksInPortfolio[stockNameFilled]-=numFilled
- if msg['type']=="out":
- orderNum=msg['order_id']
- print("order out ", orderNum)
- if orderNum in currentBuyOrders:
- del currentBuyOrders[orderNum]
- if orderNum in currentSellOrders:
- del currentSellOrders[orderNum]
- if msg['type']=='close':
- tradingIsOpen=False
- # call your strategies here
- bondArb()
- if __name__ == "__main__":
- if len(sys.argv) != 2:
- print("Use case: python [filename.py] [test/production]")
- else:
- main(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement