forked from sabvan/UChicagoTradingComp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalCase1.py
312 lines (259 loc) · 13.2 KB
/
finalCase1.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from typing import Optional
from xchangelib import xchange_client
import asyncio
import copy
class MyXchangeClient(xchange_client.XChangeClient):
'''A shell client with the methods that can be implemented to interact with the xchange.'''
def compute_etf_fv(self):
if self.fair_values['EPT'] == None:
return
if self.fair_values['SCP'] != None and self.fair_values['BRV'] != None:
self.fair_values['SCP'] = 0.3 * self.fair_values['EPT'] + 0.3 * self.fair_values['IGM'] + 0.4 * self.fair_values['BRV']
if self.fair_values['DLO'] != None and self.fair_values['MKU'] != None:
self.fair_values['JAK'] = 0.2 * self.fair_values['EPT'] + 0.5 * self.fair_values['DLO'] + 0.3 * self.fair_values['MKU']
def __init__(self, host: str, username: str, password: str):
super().__init__(host, username, password)
self.symbols = xchange_client.SYMBOLS
self.fade = {}
self.fair_values = {}
self.state = {}
for symbol in self.symbols:
self.fair_values[symbol] = 5000
self.state[symbol] = {}
self.fade[symbol] = 0
self.etfs = ['SCP', 'JAK']
self.etf_weights = [
(0, 0, 0.3, 0, 0, 0.3, 0.4, 0),
(0, 0, 0.2, 0.5, 0.3, 0, 0, 0)
]
self.book_other = self.order_books
# desired orders to have
self.desired_orders = {}
def update_state(self):
self.book_other = copy.deepcopy(self.order_books)
print(self.open_orders)
for order in self.open_orders.values():
order_request, qty, is_market = order
if is_market:
continue
symbol = order_request.symbol
side = order_request.side
print(order_request.limit)
px = order_request.limit.px
qty = order_request.limit.qty
if side == xchange_client.Side.BUY:
self.book_other[symbol].bids[px] -= qty
elif side == xchange_client.Side.SELL:
self.book_other[symbol].asks[px] -= qty
for security, book in self.book_other.items():
sorted_bids = sorted((k,v) for k,v in book.bids.items() if v != 0)
sorted_asks = sorted((k,v) for k,v in book.asks.items() if v != 0)
if len(sorted_bids) > 0 and len(sorted_asks) > 0:
self.fair_values[security] = (sorted_bids[-1][0] + sorted_asks[0][0]) / 2
self.state[security] = {
'best_bid' : sorted_bids[-1], # (price, qty)
'best_ask' : sorted_asks[0], # (price, qty)
'mid_prc' : (sorted_bids[-1][0] + sorted_asks[0][0]) / 2
}
# self.compute_etf_fv()
return
def compute_fade(self):
for security, pos in self.positions.items():
if security == 'cash':
continue
self.fade[security] = pos
async def market_making(self):
await self.clear_pos()
while True:
await asyncio.sleep(0.1)
self.update_state()
self.compute_fade()
for symbol in self.symbols:
if self.state[symbol] == {}:
continue
if symbol in ['SCP', 'JMS', 'JAK']:
continue
bb = self.state[symbol]['best_bid'][0]
ba = self.state[symbol]['best_ask'][0]
if ba - bb > 2:
print(bb, ba)
await self.place_order(symbol, 1, xchange_client.Side.BUY, bb+1-self.fade[symbol])
await self.place_order(symbol, 1, xchange_client.Side.SELL, ba-1-self.fade[symbol])
async def arbitrage(self):
# await self.clear_pos()
await asyncio.sleep(2)
while True:
# await asyncio.sleep(0.05)
flag = 0
for security, pos in self.positions.items():
if security == 'cash':
continue
if abs(pos) >= 50:
flag = 1
if flag:
await self.clear_pos()
for security, book in self.order_books.items():
sorted_bids = sorted((k,v) for k,v in book.bids.items() if v != 0)
sorted_asks = sorted((k,v) for k,v in book.asks.items() if v != 0)
if len(sorted_bids) > 0 and len(sorted_asks) > 0:
self.fair_values[security] = (sorted_bids[-1][0] + sorted_asks[0][0]) / 2
self.state[security] = {
'best_bid' : sorted_bids[-1], # (price, qty)
'best_ask' : sorted_asks[0], # (price, qty)
'mid_prc' : (sorted_bids[-1][0] + sorted_asks[0][0]) / 2
}
# self.compute_etf_fv()
# check for creation opportunities
flag = 0
for symbol in self.state:
if symbol == 'JMS':
continue
if self.state[symbol] == {}:
flag = 1
if flag == 1:
continue
create_arb_SCP = 10 * self.state['SCP']['best_bid'][0] - 3 * self.state['EPT']['best_ask'][0] - 3 * self.state['IGM']['best_ask'][0] - 4 * self.state['BRV']['best_ask'][0]
vol = min(self.state['EPT']['best_ask'][1]//3, self.state['IGM']['best_ask'][1]//3, self.state['BRV']['best_ask'][1]//4, self.state['SCP']['best_bid'][1]//10)
vol = min(vol, 5)
if vol < 0:
print("vol is negative", vol)
if create_arb_SCP > 5:
await self.place_swap_order('toSCP', vol)
await self.place_order('EPT', 3 * vol, xchange_client.Side.BUY)
await self.place_order('IGM', 3 * vol, xchange_client.Side.BUY)
await self.place_order('BRV', 4 * vol, xchange_client.Side.BUY)
await self.place_order('SCP', 10 * vol, xchange_client.Side.SELL)
await asyncio.sleep(0.1)
redeem_arb_SCP = 3 * self.state['EPT']['best_bid'][0] + 3 * self.state['IGM']['best_bid'][0] + 4 * self.state['BRV']['best_bid'][0] - 10 * self.state['SCP']['best_ask'][0]
vol = min(self.state['EPT']['best_bid'][1]//3, self.state['IGM']['best_bid'][1]//3, self.state['BRV']['best_bid'][1]//4, self.state['SCP']['best_ask'][1]//10)
vol = min(vol, 5)
if vol < 0:
print("vol is negative", vol)
# vol = 1
if redeem_arb_SCP > 5:
await self.place_swap_order('fromSCP', vol)
await self.place_order('EPT', 3 * vol, xchange_client.Side.SELL)
await self.place_order('IGM', 3 * vol, xchange_client.Side.SELL)
await self.place_order('BRV', 4 * vol, xchange_client.Side.SELL)
await self.place_order('SCP', 10 * vol, xchange_client.Side.BUY)
await asyncio.sleep(0.1)
# vol = 1
create_arb_JAK = 10 * self.state['JAK']['best_bid'][0] - 2 * self.state['EPT']['best_ask'][0] - 5 * self.state['DLO']['best_ask'][0] - 3 * self.state['MKU']['best_ask'][0]
vol = min(self.state['JAK']['best_bid'][1]//10, self.state['EPT']['best_ask'][1]//2, self.state['DLO']['best_ask'][1]//5, self.state['MKU']['best_ask'][1]//3)
vol = min(vol, 5)
if vol < 0:
print("vol is negative", vol)
# print(create_arb_JAK)
if create_arb_JAK > 5:
await self.place_swap_order('toJAK', vol)
await self.place_order('JAK', 10 * vol, xchange_client.Side.SELL)
await self.place_order('EPT', 2 * vol, xchange_client.Side.BUY)
await self.place_order('DLO', 5 * vol, xchange_client.Side.BUY)
await self.place_order('MKU', 3 * vol, xchange_client.Side.BUY)
await asyncio.sleep(0.1)
redeem_arb_JAK = -10 * self.state['JAK']['best_ask'][0] + 2 * self.state['EPT']['best_bid'][0] + 5 * self.state['DLO']['best_bid'][0] + 3 * self.state['MKU']['best_bid'][0]
vol = min(self.state['JAK']['best_ask'][1]//10, self.state['EPT']['best_bid'][1]//2, self.state['DLO']['best_bid'][1]//5, self.state['MKU']['best_bid'][1]//3)
vol = min(vol, 5)
if vol < 0:
print("vol is negative", vol)
# print(redeem_arb_JAK)
if redeem_arb_JAK > 5:
await self.place_swap_order('fromJAK', vol)
await self.place_order('JAK', 10 * vol, xchange_client.Side.BUY)
await self.place_order('EPT', 2 * vol, xchange_client.Side.SELL)
await self.place_order('DLO', 5 * vol, xchange_client.Side.SELL)
await self.place_order('MKU', 3 * vol, xchange_client.Side.SELL)
await asyncio.sleep(0.1)
print(create_arb_SCP, redeem_arb_SCP, create_arb_JAK, redeem_arb_JAK)
async def monitor_positions(self):
await asyncio.sleep(1)
print(self.positions)
await asyncio.sleep(1)
async def bot_handle_cancel_response(self, order_id: str, success: bool, error: Optional[str]) -> None:
order = self.open_orders[order_id]
print(f"{'Market' if order[2] else 'Limit'} Order ID {order_id} cancelled, {order[1]} unfilled")
async def bot_handle_order_fill(self, order_id: str, qty: int, price: int):
print("order fill", self.positions)
async def bot_handle_order_rejected(self, order_id: str, reason: str) -> None:
print("order rejected because of ", reason)
async def bot_handle_trade_msg(self, symbol: str, price: int, qty: int):
# print("something was traded")
pass
async def bot_handle_book_update(self, symbol: str) -> None:
# print("book update")
# await self.generate_orders()
pass
async def bot_handle_swap_response(self, swap: str, qty: int, success: bool):
# print("Swap response")
pass
async def trade(self):
"""This is a task that is started right before the bot connects and runs in the background."""
await asyncio.sleep(5)
print("attempting to trade")
await self.place_order("BRV", 3, xchange_client.Side.SELL, 7)
# Cancelling an order
order_to_cancel = await self.place_order("BRV",3, xchange_client.Side.BUY, 5)
await asyncio.sleep(5)
await self.cancel_order(order_to_cancel)
# Placing Swap requests
await self.place_swap_order('toJAK', 1)
await asyncio.sleep(5)
await self.place_swap_order('fromSCP', 1)
await asyncio.sleep(5)
# Placing an order that gets rejected for exceeding qty limits
await self.place_order("BRV",1000, xchange_client.Side.SELL, 7)
await asyncio.sleep(5)
# Placing a market order
market_order_id = await self.place_order("BRV",10, xchange_client.Side.SELL)
print("Market Order ID:", market_order_id)
await asyncio.sleep(5)
# Viewing Positions
print("My positions:", self.positions)
async def view_books(self):
"""Prints the books every 1 seconds."""
while True:
await asyncio.sleep(1)
for security, book in self.order_books.items():
sorted_bids = sorted((k,v) for k,v in book.bids.items() if v != 0)
sorted_asks = sorted((k,v) for k,v in book.asks.items() if v != 0)
print(f"Bids for {security}:\n{sorted_bids}")
print(f"Asks for {security}:\n{sorted_asks}")
async def clear_pos(self):
print("CLEARING POS CLEARING POS CLEARING POS CLEARING POS CLEARING POS CLEARING POS ")
print("Pos", self.positions)
for security, pos in self.positions.items():
if security == 'cash':
continue
print(security, pos)
if pos > 0:
lots = pos//40
rem = pos%40
for i in range(lots):
await self.place_order(security, 40, xchange_client.Side.SELL)
await self.place_order(security, rem, xchange_client.Side.SELL)
elif pos < 0:
lots = (-pos)//40
rem = (-pos)%40
for i in range(lots):
await self.place_order(security, 40, xchange_client.Side.BUY)
await self.place_order(security, rem, xchange_client.Side.BUY)
await asyncio.sleep(1)
print("Cleared positions", self.positions)
async def start(self):
"""
Creates tasks that can be run in the background. Then connects to the exchange
and listens for messages.
"""
# asyncio.create_task(self.view_books())
asyncio.create_task(self.arbitrage())
asyncio.create_task(self.monitor_positions())
await self.connect()
async def main():
# SERVER = 'staging.uchicagotradingcompetition.com:3333' # run on sandbox
SERVER = "dayof.uchicagotradingcompetition.com:3333"
my_client = MyXchangeClient(SERVER, "princeton","nidoqueen-chansey-9131")
await my_client.start()
return
if __name__ == "__main__":
loop = asyncio.get_event_loop()
result = loop.run_until_complete(main())