-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
90 lines (78 loc) · 2.64 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import print_function
from connection import connect, sendHello, write_to_exchange, read_from_exchange, sendAdd, sendCancel, sendConvert
from time import sleep
import sys
sys.path.insert(0, '.')
from bank import Bank
from allStocks import AllStocks
from order import Order
from trade import handleTrade
limits = {
'BOND' : 100,
'AAPL' : 100,
'MSFT' : 100,
'GOOG' : 100,
'XLK' : 100,
'BABZ' : 10,
'BABA' : 10
}
stocks = ['BOND','AAPL','MSFT','GOOG','XLK','BABZ','BABA']
# ~~~~~============== MAIN LOOP ==============~~~~~
def updateBank(exchange, bank):
helloResponse = sendHello(exchange)
bank.updateStateFromResponse(helloResponse)
def main():
exchange = connect()
bank = Bank()
helloResponse = sendHello(exchange)
stocksState = AllStocks(helloResponse)
bank.updateStateFromResponse(helloResponse)
orderCounter = 1
while True:
#sleep(0.1)
read = read_from_exchange(exchange)
#if 'symbol' in read and read['symbol'] == 'BOND':
# print(read)
if read['type'] == 'open':
stocksState.openStocks(read)
elif read['type'] == 'book':
stocksState.updateStocks(read)
elif read['type'] == 'trade':
# SOMEONE ELSE TRADED DO SOMETHING
#print("Trade has happened")
some = 9
elif read['type'] == 'close':
stocksState.closeStocks(read)
elif read['type'] == 'ack':
orderID = read['order_id']
if orderID in bank.getOrders():
order = bank.getOrders()[orderID]
print("Success " + order.stock + " " + order.orderType + " " + str(order.price)+" "+str(order.quantity))
order.orderStatus = "success"
elif read['type'] == 'error' or read['type'] == 'reject':
print("Got an Error: " + read['error'], file=sys.stderr)
OrderID = -1
if read['type'] == 'reject':
orderID = read['order_id']
if orderID in bank.getOrders():
order = bank.getOrders()[orderID]
print("here is why: " + order.stock)
order.orderStatus = "cancel"
sendCancel(exchange, orderID)
elif read['type'] == 'out':
orderID=read['order_id']
if orderID in bank.getOrders():
order = bank.getOrders()[orderID]
if order.orderType == "SELL" and order.orderStatus == "cancel":
bank.deltaQuantity(order.stock, order.quantity)
bank.deleteOrder(read['order_id'])
elif read['type'] == 'fill':
orderID = read['order_id']
if orderID in bank.getOrders():
order = bank.getOrders()[orderID]
print("FILLED " + order.stock + " " + order.orderType + " " + str(order.price)+" "+str(order.quantity))
order.orderStatus = "fill"
bank.deltaState(read['dir'], read['price'], read['size'], read['symbol'])
orderCounter = handleTrade(orderCounter,exchange,stocksState,bank)
if __name__ == "__main__":
main()