-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
368 lines (311 loc) · 13.6 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import logging
from math import sqrt
import statistics
import time
from optibook import ORDER_TYPE_LIMIT, SIDE_ASK, SIDE_BID
from optibook.exchange_responses import InsertOrderResponse
from optibook.synchronous_client import Exchange
from collections import defaultdict
from functools import reduce
import colors
from helper import color_pm_float, color_pm_int
LEVEL = 0.1
OWN_WEIGHT = 0.03
OTHER_INSIDE_WEIGHT = 0.001
OTHER_OUTSIDE_WEIGHT = 0.0003
root = logging.getLogger()
if root.handlers:
for handler in root.handlers:
root.removeHandler(handler)
logging.basicConfig(
datefmt="%H:%M:%S",
format=f"{colors.GREY}%(asctime)s.%(msecs)03d{colors.END} %(message)s",
level=logging.DEBUG,
)
class TradingAlgorithm:
def __init__(self, exchange):
self.exchange = exchange
self.prev_pnl = self.pnl = self.exchange.get_pnl()
self.instruments = ["SMALL_CHIPS_NEW_COUNTRY", "SMALL_CHIPS"]
self.is_trading = defaultdict(bool)
self.own_trades = defaultdict(list)
self.market_trades = defaultdict(list)
self.deltas = 0
self.n = 0
self.sample_mean = 0
self.sample_stdev = 0
self.square_sum = 0
self.sum = 0
while not (tradable_instruments := self.exchange.get_instruments()):
time.sleep(0.2)
for instrument_id in self.instruments:
if instrument_id not in tradable_instruments:
raise Exception(
f"'{instrument_id}' does not exist. Options: {tradable_instruments}"
)
self.book = defaultdict(list)
for instrument_id in self.instruments:
self.book[instrument_id] = self.exchange.get_last_price_book(instrument_id)
self.theo, self.margin = self.compute_market_book()
self.volume_curve = [2, 8, 16, 32]
def place_order(self, instrument_id, price, volume, side):
bid_response: InsertOrderResponse = self.exchange.insert_order(
instrument_id,
price=price,
side=(SIDE_BID if side == "bid" else SIDE_ASK),
volume=volume,
order_type=ORDER_TYPE_LIMIT, # add fill-kill
)
if not bid_response.success:
logging.info(
f"{colors.RED} Unable to insert order: {bid_response.success}{colors.END}"
)
def place_quotes_levels(self, instrument_id, initial_level, side):
for steps, level_volume in enumerate(self.volume_curve):
self.place_order(
instrument_id,
initial_level + LEVEL * steps * (1 if side == "ask" else -1),
level_volume,
side,
)
def compute_market_book(self):
while (
not self.book["SMALL_CHIPS"]
or not self.book["SMALL_CHIPS"].asks
or not self.book["SMALL_CHIPS"].bids
):
logging.info(f"{colors.YELLOW2}Waiting for SMALL_CHIPS book.{colors.END}")
time.sleep(1)
self.book["SMALL_CHIPS"] = self.exchange.get_last_price_book("SMALL_CHIPS")
logging.info(
f"{colors.YELLOW2}Computing initial market from SMALL_CHIPS book.{colors.END}"
)
book = self.book["SMALL_CHIPS"]
ask = book.asks[0]
bid = book.bids[0]
theo = (ask.price * ask.volume + bid.price * bid.volume) / (
bid.volume + ask.volume
)
margin = (ask.price - bid.price) / 2
logging.info(
f"{colors.YELLOW2}Initial conditions set: {colors.END}{colors.VIOLET2}theo: {theo:.2f}, margin: {margin:.2f}{colors.END}"
)
return theo, margin
def compute_market_ema(self):
alpha = 0.03
def ema_aggregate(acc, el):
discount = (1 - alpha) ** el.volume
return discount * acc + (1 - discount) * el.price
ticks = self.exchange.get_trade_tick_history(
"SMALL_CHIPS"
) + self.exchange.get_trade_tick_history("SMALL_CHIPS_NEW_COUNTRY")
ticks.sort(key=lambda x: x.timestamp)
prices = list(map(lambda x: x.price, ticks))
mean, stdev = statistics.mean(prices), statistics.stdev(prices)
exemplars = list(filter(lambda x: abs(mean - x.price) <= 2 * stdev, ticks))
theo = reduce(ema_aggregate, exemplars, exemplars[0].price)
# TODO: tweak multiplier
margin = stdev
return theo, margin
def print_status(self):
own_trades = (
self.own_trades["SMALL_CHIPS"] + self.own_trades["SMALL_CHIPS_NEW_COUNTRY"]
)
market_trades = (
self.market_trades["SMALL_CHIPS"]
+ self.market_trades["SMALL_CHIPS_NEW_COUNTRY"]
)
for instrument_id in self.instruments:
if self.book[instrument_id].asks and self.book[instrument_id].bids:
logging.info(
f"{instrument_id}: bid={self.book[instrument_id].bids[0].price:.2f}, ask={self.book[instrument_id].asks[0].price:.2f}, spread={(self.book[instrument_id].asks[0].price - self.book[instrument_id].bids[0].price):.2f}"
)
else:
logging.info(
f"{instrument_id}: {self.book[instrument_id].asks} {self.book[instrument_id].bids}"
)
logging.info(f"{self.book[instrument_id]}")
logging.info(
f"{colors.WHITE2}PNL: {self.prev_pnl:.2f} -> {self.pnl:.2f}{colors.END}, (Δ: {color_pm_float(self.pnl - self.prev_pnl)}) \
{colors.WHITE2}{len(own_trades)}/{len(market_trades)}{colors.END} trades"
)
logging.info(
f"SMALL: {color_pm_int(self.positions['SMALL_CHIPS'])} {color_pm_int(self.positions['SMALL_CHIPS_NEW_COUNTRY'])}, δ: {color_pm_int(self.deltas)}" # \
# TECH: {color_pm_int(positions['TECH_INC'])} {color_pm_int(positions['TECH_INC_NEW_COUNTRY'])}, δ: {color_pm_int(positions['TECH_INC'] + positions['TECH_INC_NEW_COUNTRY'])}"
)
logging.info(own_trades)
def update_market_state(self):
self.positions = self.exchange.get_positions()
self.prev_pnl, self.pnl = self.pnl, self.exchange.get_pnl()
tradable_instruments = self.exchange.get_instruments()
for instrument_id in self.instruments:
self.book[instrument_id] = self.exchange.get_last_price_book(instrument_id)
self.is_trading[instrument_id] = (
instrument_id in tradable_instruments
and not tradable_instruments[instrument_id].paused
)
self.own_trades[instrument_id] = self.exchange.poll_new_trades(
instrument_id
)
self.market_trades[instrument_id] = self.exchange.poll_new_trade_ticks(
instrument_id
)
def update_internal_state(self):
own_trades = (
self.own_trades["SMALL_CHIPS"] + self.own_trades["SMALL_CHIPS_NEW_COUNTRY"]
)
own_ids = set(map(lambda x: x.trade_id, own_trades))
for trade in own_trades:
left, right = self.theo - self.margin, self.theo + self.margin
if left < trade.price:
update = OWN_WEIGHT * (right - trade.price) * trade.volume
self.theo -= update / 2
else:
update = OWN_WEIGHT * (trade.price - left) * trade.volume
self.theo += update / 2
self.margin += update / 2
print(f"Inside, ours: {update / 2}, {update / 2}")
market_trades = (
self.market_trades["SMALL_CHIPS"]
+ self.market_trades["SMALL_CHIPS_NEW_COUNTRY"]
)
market_trades = filter(lambda x: x.trade_id not in own_ids, market_trades)
for trade in market_trades:
left, right = self.theo - self.margin, self.theo + self.margin
if left > trade.price or right < trade.price:
update = (
OTHER_OUTSIDE_WEIGHT
* (2 * trade.price - left - right)
* trade.volume
)
self.theo += update
print(f"Outside, other: {update / 2}, {0}")
else:
d1 = OTHER_INSIDE_WEIGHT * (trade.price - left) * trade.volume
d2 = OTHER_INSIDE_WEIGHT * (trade.price - right) * trade.volume
self.margin -= (d1 - d2) / 2
self.theo += (d1 + d2) / 2
print(-(d1 - d2) / 2, (d1 + d2) / 2)
print(f"Inside, other: {-(d1 - d2) / 2}, {(d1 + d2) / 2}")
self.margin = max(self.margin, 0.05)
self.deltas = (
self.positions["SMALL_CHIPS"] + self.positions["SMALL_CHIPS_NEW_COUNTRY"]
)
prices = map(lambda x: x.price, self.market_trades["SMALL_CHIPS"])
for price in prices:
self.sample_mean = (
((self.n - 1) * self.sample_mean + price) / self.n
if self.n > 0
else price
)
self.n += 1
self.square_sum += price * price
self.sum += price
self.sample_stdev = (
1 / self.n * sqrt(self.n * self.square_sum - self.sum**2)
)
def propagate_trade(self, trade, theo_update_rate, margin_update_rate):
update = trade.volume * (trade.price - self.theo)
self.theo += theo_update_rate * update
self.margin += margin_update_rate * abs(update) # TODO: fix
def send_orders(self):
# this will be removed eventually
for instrument_id in self.instruments:
self.exchange.delete_orders(instrument_id)
if self.is_trading["SMALL_CHIPS_NEW_COUNTRY"]:
bids = self.book["SMALL_CHIPS_NEW_COUNTRY"].bids
asks = self.book["SMALL_CHIPS_NEW_COUNTRY"].asks
edge_bid, edge_ask = (
bids[0].price if bids else 0,
asks[0].price if asks else 1e7,
)
start_bid = min(edge_bid + 0.1, round(self.theo - self.margin - 0.05, 1))
start_ask = max(edge_ask - 0.1, round(self.theo + self.margin + 0.05, 1))
logging.info(
f"{colors.VIOLET2}{self.theo:.2f}±{self.margin:.2f}: {start_bid:.2f} @ {start_ask:.2f}{colors.END}"
)
self.place_quotes_levels("SMALL_CHIPS_NEW_COUNTRY", start_bid, "bid")
self.place_quotes_levels("SMALL_CHIPS_NEW_COUNTRY", start_ask, "ask")
if self.is_trading["SMALL_CHIPS"] and self.n >= 20 and self.sample_stdev != 0:
bids = self.book["SMALL_CHIPS"].bids
asks = self.book["SMALL_CHIPS"].asks
edge_bid, edge_ask = (
bids[0].price if bids else 0,
asks[0].price if asks else 1e7,
)
if (
-(edge_ask - self.sample_mean) / self.sample_stdev > 2
and self.deltas < 0
):
logging.info(
f"{colors.GREEN}Lifting {-self.deltas} @ {(self.sample_mean - 2 * self.sample_stdev):.2f} {colors.END}"
)
self.place_order(
"SMALL_CHIPS",
self.sample_mean - 2 * self.sample_stdev,
-self.deltas,
"bid",
)
elif (
-(edge_ask - self.sample_mean) / self.sample_stdev > 1.5
and self.deltas < 0
):
logging.info(
f"{colors.GREEN}Lifting {-min(10,self.deltas)} @ {(self.sample_mean - 1.5 * self.sample_stdev):.2f} {colors.END}"
)
self.place_order(
"SMALL_CHIPS",
self.sample_mean - 1.5 * self.sample_stdev,
min(10, -self.deltas),
"bid",
)
elif (
self.sample_mean - edge_bid
) / self.sample_stdev > 2 and self.deltas > 0:
logging.info(
f"{colors.GREEN}Hitting {self.deltas} @ {(self.sample_mean + 2 * self.sample_stdev):.2f} {colors.END}"
)
self.place_order(
"SMALL_CHIPS",
self.sample_mean + 2 * self.sample_stdev,
self.deltas,
"sell",
)
elif (
self.sample_mean - edge_bid
) / self.sample_stdev > 1.5 and self.deltas > 0:
logging.info(
f"{colors.GREEN}Hitting {min(10,self.deltas)} @ {(self.sample_mean + 1.5 * self.sample_stdev):.2f} {colors.END}"
)
self.place_order(
"SMALL_CHIPS",
self.sample_mean + 2 * self.sample_stdev,
min(10, self.deltas),
"sell",
)
logging.info(
f"{colors.BEIGE2}SMALL CHIP z-edges are {((edge_ask - self.sample_mean)/self.sample_stdev):.2f} {((edge_bid - self.sample_mean)/self.sample_stdev):.2f}{colors.END} {colors.GREY}(μ={self.sample_mean:.2f},σ={self.sample_stdev:.2f}){colors.END}"
)
def run(self):
if not self.exchange.is_connected():
logging.error(f"{colors.RED}Exchange not connected.{colors.END}")
return
self.update_market_state()
self.update_internal_state()
self.print_status()
self.send_orders()
HOST = "hackzurich-1.optibook.net"
USERNAME = "team-006"
PASSWORD = "gb7zflq0u6"
def main():
exchange = Exchange(
host=HOST, username=USERNAME, password=PASSWORD, info_port=7001, exec_port=8001
)
exchange.connect()
bot = TradingAlgorithm(exchange)
sleep_duration_sec = 0.3
while True:
bot.run()
time.sleep(sleep_duration_sec)
if __name__ == "__main__":
main()