-
Notifications
You must be signed in to change notification settings - Fork 6
/
logbot.cpp
642 lines (482 loc) · 15.9 KB
/
logbot.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// TODO: move ClientState and its OrderBook code into this file
#include "kirin.hpp"
#include <cassert>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <chrono>
#include <set>
#include <unordered_map>
#include <unordered_set>
#define DEBUG 0
#define INFO 1
#define TIME_INFO 1
int64_t time_ns() {
using namespace std::chrono;
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
}
// PRICING TYPE 1
price_t cube_root_price(price_t best_bid, price_t best_offer, quantity_t bid_quote_size, quantity_t offer_quote_size) const {
const bid_weight = std::cbrt(bid_quote_size);
const offer_weight = std::cbrt(offer_quote_size);
return (bid_weight * best_bid + offer_weight * best_offer) / (bid_weight + offer_weight);
}
// PRICING TYPE 2
price_t square_root_price(price_t best_bid, price_t best_offer, quantity_t bid_quote_size, quantity_t offer_quote_size) const {
const bid_weight = std::sqrt(bid_quote_size);
const offer_weight = std::sqrt(offer_quote_size);
return (bid_weight * best_bid + offer_weight * best_offer) / (bid_weight + offer_weight);
}
// PRICING TYPE 3
price_t weighted_price(price_t best_bid, price_t best_offer, quantity_t bid_quote_size, quantity_t offer_quote_size) const {
const bid_weight = bid_quote_size;
const offer_weight = offer_quote_size;
return (bid_weight * best_bid + offer_weight * best_offer) / (bid_weight + offer_weight);
}
// PRICING TYPE 4
price_t square_price(price_t best_bid, price_t best_offer, quantity_t bid_quote_size, quantity_t offer_quote_size) const {
const bid_weight = bid_quote_size * bid_quote_size;
const offer_weight = offer_quote_size * offer_quote_size;
return (bid_weight * best_bid + offer_weight * best_offer) / (bid_weight + offer_weight);
}
// Limit Orders
//
struct LimitOrder {
price_t price;
mutable quantity_t quantity; // mutable so set doesn't complain
order_id_t order_id;
long long time;
trader_id_t trader_id;
bool buy;
bool operator <(const LimitOrder& other) const {
// < means more aggressive
if (buy) {
return price > other.price || (price == other.price && time < other.time);
} else {
return price < other.price || (price == other.price && time < other.time);
}
}
bool trades_with(const LimitOrder& other) const {
return ((buy && !other.buy && price >= other.price) ||
(!buy && other.buy && price <= other.price));
}
};
// MyBook
//
struct MyBook {
public:
MyBook() {}
price_t get_bbo(bool buy) const {
const std::set<LimitOrder>& side = sides[buy];
if (side.empty()) {
return 0.0;
}
return side.begin()->price;
}
price_t get_mid_price(price_t default_to) const {
price_t best_bid = get_bbo(true);
price_t best_offer = get_bbo(false);
if (best_bid == 0.0 || best_offer == 0.0) {
return default_to;
}
return 0.5 * (best_bid + best_offer);
}
void insert(Common::Order order_to_insert) {
LimitOrder order_left = {
.price = order_to_insert.price,
.quantity = order_to_insert.quantity,
.order_id = order_to_insert.order_id,
.time = std::chrono::steady_clock::now().time_since_epoch().count(),
.trader_id = order_to_insert.trader_id,
.buy = order_to_insert.buy
};
auto& side = sides[(size_t)order_left.buy];
auto it_new = side.insert(order_left);
assert(it_new.second);
order_map[order_left.order_id] = it_new.first;
}
void cancel(trader_id_t trader_id, order_id_t order_id) {
auto it = order_map[order_id];
order_map.erase(order_id);
auto& side = sides[(size_t)it->buy];
side.erase(it);
}
quantity_t decrease_qty(order_id_t order_id, quantity_t decrease_by) {
if (!order_map.count(order_id)) {
return -1;
}
std::set<LimitOrder>::iterator it = order_map[order_id];
if (decrease_by >= it->quantity) {
order_map.erase(order_id);
std::set<LimitOrder>& side = sides[(size_t)it->buy];
side.erase(it);
return 0;
} else {
it->quantity -= decrease_by;
return it->quantity;
}
}
void print_book(std::string fp, const std::unordered_map<order_id_t, Common::Order>& mine={}) {
if (fp == "") {
return;
}
std::ofstream fout(fp, std::ios::app);
fout << time_ns() << std::endl;
fout << "offers\n";
for (auto rit = sides[0].rbegin(); rit != sides[0].rend(); rit++) {
auto x = *rit;
fout << x.price << ' ' << x.quantity;
if (mine.count(x.order_id)) {
fout << " (mine)";
}
fout << '\n';
}
fout << "\nbids\n";
for (auto& x : sides[1]) {
fout << x.price << ' ' << x.quantity;
if (mine.count(x.order_id)) {
fout << " (mine)";
}
fout << '\n';
}
fout << "EOF" << std::endl;
fout.close();
}
/// Really fucking slow. Need to improve this.
quantity_t quote_size(bool buy) const {
price_t p = get_bbo(buy);
if (p == 0.0) {
return 0;
}
quantity_t ans = 0;
for (auto& x : sides[buy]) {
if (x.price != p) {
break;
}
ans += x.quantity;
}
return ans;
}
price_t spread() const {
price_t best_bid = get_bbo(true);
price_t best_offer = get_bbo(false);
if (best_bid == 0.0 || best_offer == 0.0) {
return 0.0;
}
return best_offer - best_bid;
}
private:
std::set<LimitOrder> sides[2];
std::unordered_map<order_id_t, std::set<LimitOrder>::iterator> order_map;
};
/// BETTERBOOK
///
struct BookV2 {
public:
BookV2() {}
price_t get_bbo(bool buy) const {
const std::set<LimitOrder>& side = sides[buy];
if (side.empty()) {
return 0.0;
}
return side.begin()->price;
}
price_t get_mid_price(price_t default_to) const {
price_t best_bid = get_bbo(true);
price_t best_offer = get_bbo(false);
if (best_bid == 0.0 || best_offer == 0.0) {
return default_to;
}
return 0.5 * (best_bid + best_offer);
}
void insert(Common::Order order_to_insert) {
LimitOrder order_left = {
.price = order_to_insert.price,
.quantity = order_to_insert.quantity,
.order_id = order_to_insert.order_id,
.time = std::chrono::steady_clock::now().time_since_epoch().count(),
.trader_id = order_to_insert.trader_id,
.buy = order_to_insert.buy
};
auto& side = sides[(size_t)order_left.buy];
auto it_new = side.insert(order_left);
assert(it_new.second);
order_map[order_left.order_id] = it_new.first;
}
void cancel(trader_id_t trader_id, order_id_t order_id) {
auto it = order_map[order_id];
order_map.erase(order_id);
auto& side = sides[(size_t)it->buy];
side.erase(it);
}
quantity_t decrease_qty(order_id_t order_id, quantity_t decrease_by) {
if (!order_map.count(order_id)) {
return -1;
}
std::set<LimitOrder>::iterator it = order_map[order_id];
if (decrease_by >= it->quantity) {
order_map.erase(order_id);
std::set<LimitOrder>& side = sides[(size_t)it->buy];
side.erase(it);
return 0;
} else {
it->quantity -= decrease_by;
return it->quantity;
}
}
void print_book(std::string fp, const std::unordered_map<order_id_t, Common::Order>& mine={}) {
if (fp == "") {
return;
}
std::ofstream fout(fp, std::ios::app);
fout << time_ns() << std::endl;
fout << "offers\n";
for (auto rit = sides[0].rbegin(); rit != sides[0].rend(); rit++) {
auto x = *rit;
fout << x.price << ' ' << x.quantity;
if (mine.count(x.order_id)) {
fout << " (mine)";
}
fout << '\n';
}
fout << "\nbids\n";
for (auto& x : sides[1]) {
fout << x.price << ' ' << x.quantity;
if (mine.count(x.order_id)) {
fout << " (mine)";
}
fout << '\n';
}
fout << "EOF" << std::endl;
fout.close();
}
/// Really fucking slow. Need to improve this.
quantity_t quote_size(bool buy) const {
price_t p = get_bbo(buy);
if (p == 0.0) {
return 0;
}
quantity_t ans = 0;
for (auto& x : sides[buy]) {
if (x.price != p) {
break;
}
ans += x.quantity;
}
return ans;
}
price_t spread() const {
price_t best_bid = get_bbo(true);
price_t best_offer = get_bbo(false);
if (best_bid == 0.0 || best_offer == 0.0) {
return 0.0;
}
return best_offer - best_bid;
}
private:
std::set<LimitOrder> sides[2];
std::unordered_map<order_id_t, std::set<LimitOrder>::iterator> order_map;
};
// My State
//
//
//
//
//
//
//
//
struct MyState {
MyState(trader_id_t trader_id) :
trader_id(trader_id), books(), submitted(), open_orders(),
cash(), positions(), volume_traded(), last_trade_price(100.0),
log_path("") {}
MyState() : MyState(0) {}
void on_trade_update(const Common::TradeUpdate& update) {
last_trade_price = update.price;
books[update.ticker].decrease_qty(update.resting_order_id, update.quantity);
if (submitted.count(update.resting_order_id)) {
if (!submitted.count(update.aggressing_order_id)) {
volume_traded += update.quantity;
// not a self-trade
update_position(update.ticker, update.price,
update.buy ? -update.quantity : update.quantity); // opposite, since resting
}
open_orders[update.resting_order_id].quantity -= update.quantity;
if (open_orders[update.resting_order_id].quantity <= 0) {
open_orders.erase(update.resting_order_id);
}
} else if (submitted.count(update.aggressing_order_id)) {
volume_traded += update.quantity;
update_position(update.ticker, update.price,
update.buy ? update.quantity : -update.quantity);
}
}
void update_position(ticker_t ticker, price_t price, quantity_t delta_quantity) {
cash -= price * delta_quantity;
positions[ticker] += delta_quantity;
}
void on_order_update(const Common::OrderUpdate& update) {
const Common::Order order{
.ticker = update.ticker,
.price = update.price,
.quantity = update.quantity,
.buy = update.buy,
.ioc = false,
.order_id = update.order_id,
.trader_id = trader_id
};
books[update.ticker].insert(order);
if (submitted.count(update.order_id)) {
open_orders[update.order_id] = order;
}
}
void on_cancel_update(const Common::CancelUpdate& update) {
books[update.ticker].cancel(trader_id, update.order_id);
if (open_orders.count(update.order_id)) {
open_orders.erase(update.order_id);
}
submitted.erase(update.order_id);
}
void on_place_order(const Common::Order& order) {
submitted.insert(order.order_id);
}
std::unordered_map<price_t, std::vector<Common::Order>> levels() const {
std::unordered_map<price_t, std::vector<Common::Order>> levels;
for (const auto& p : open_orders) {
const Common::Order& order = p.second;
levels[order.price].push_back(order);
}
return levels;
}
price_t get_pnl() const {
price_t pnl = cash;
for (int i = 0; i < MAX_NUM_TICKERS; i++) {
pnl += positions[i] * books[i].get_mid_price(last_trade_price);
}
return pnl;
}
price_t get_bbo(ticker_t ticker, bool buy) const {
return books[ticker].get_bbo(buy);
}
price_t get_mid_price(ticker_t ticker, bool buy) const {
return books[ticker].get_mid_price(buy);
}
price_t get_quote_size(ticker_t ticker, bool buy) const {
return books[ticker].quote_size(buy);
}
price_t get_spread(ticker_t ticker) const {
return books[ticker].spread();
}
void log_book() {
books[0].print_book(log_path, open_orders);
}
trader_id_t trader_id;
MyBook books[MAX_NUM_TICKERS];
std::unordered_set<order_id_t> submitted;
std::unordered_map<order_id_t, Common::Order> open_orders;
price_t cash;
quantity_t positions[MAX_NUM_TICKERS];
quantity_t volume_traded;
price_t last_trade_price;
std::string log_path;
};
// LogBot
//
// - Does nothing except log the top of book stats for each tick.
//
//
class LogBot: public Bot::AbstractBot {
public:
MyState state;
using Bot::AbstractBot::AbstractBot;
std::ofstream("prices.csv") prices_file;
int64_t last = 0, start_time;
uint64_t last_order_id = 0;
bool trade_with_me_in_this_packet = false;
// (maybe) EDIT THIS METHOD
void init(Bot::Communicator& com) {
state.trader_id = trader_id;
start_time = time_ns();
prices_file << "time,best_bid,best_offer,bid_quote_size,offer_quote_size,cbrt_price,sqrt_price,weighted_price,square_price,midpoint_price,update_type,order_id,side,update_price,quantity,trader_id" << std::endl;
}
void write_to_csv(std::string type, order_id_t order_id, bool buy, price_t price, quantity_t qty, trader_id_t trader_id) {
int64_t curr = time_ns() - start_time;
price_t best_bid = state.get_bbo(0, true);
price_t best_offer = state.get_bbo(0, false);
quantity_t bid_quote_size = state.get_quote_size(0, true);
quantity_t offer_quote_size = state.get_quote_size(0, false);
prices_file << curr << ",";
prices_file << best_bid << ",";
prices_file << best_offer << ",";
prices_file << bid_quote_size << ",";
prices_file << offer_quote_size << ",";
prices_file << cube_root_price(best_bid, best_offer, bid_quote_size, offer_quote_size) << ",";
prices_file << square_root_price(best_bid, best_offer, bid_quote_size, offer_quote_size) << ",";
prices_file << weighted_price(best_bid, best_offer, bid_quote_size, offer_quote_size) << ",";
prices_file << square_price(best_bid, best_offer, bid_quote_size, offer_quote_size) << ",";
prices_file << (best_bid + best_offer) / 2 << ",";
prices_file << type << ",";
prices_file << order_id << ",";
prices_file << buy ? 1 : 0 << ",";
prices_file << price << ",";
prices_file << qty << ",";
prices_file << trader_id << std::endl;
}
// EDIT THIS METHOD
void on_trade_update(Common::TradeUpdate& update, Bot::Communicator& com){
write_to_csv("TRADE", update.aggressing_order_id, update.buy, update.price, update.quantity, update.trader_id);
state.on_trade_update(update);
if (state.submitted.count(update.resting_order_id) ||
state.submitted.count(update.aggressing_order_id)) {
trade_with_me_in_this_packet = true;
}
}
// EDIT THIS METHOD
void on_order_update(Common::OrderUpdate & update, Bot::Communicator& com){
write_to_csv("ORDER", update.order_id, update.buy, update.price, update.quantity, update.trader_id);
state.on_order_update(update);
// This order is our order -- do not respond.
if (state.submitted.count(update.order_id)) {
return;
}
}
// EDIT THIS METHOD
void on_cancel_update(Common::CancelUpdate & update, Bot::Communicator& com){
write_to_csv("CANCEL", update.order_id, update.buy, update.price, update.quantity, update.trader_id);
state.on_cancel_update(update);
}
// (maybe) EDIT THIS METHOD
void on_reject_order_update(Common::RejectOrderUpdate& update, Bot::Communicator& com) {
write_to_csv("REJECT ORDER", update.order_id, update.buy, update.price, update.quantity, update.trader_id);
}
// (maybe) EDIT THIS METHOD
void on_reject_cancel_update(Common::RejectCancelUpdate& update, Bot::Communicator& com) {
write_to_csv("REJECT CANCEL", update.order_id, update.buy, update.price, update.quantity, update.trader_id);
}
// (maybe) EDIT THIS METHOD
void on_packet_start(Bot::Communicator& com) {
trade_with_me_in_this_packet = false;
}
// (maybe) EDIT THIS METHOD
void on_packet_end(Bot::Communicator& com) {
}
order_id_t place_order(Bot::Communicator& com, const Common::Order& order) {
Common::Order copy = order;
copy.order_id = com.place_order(order);
state.on_place_order(copy);
return copy.order_id;
}
void place_cancel(Bot::Communicator& com, const Common::Cancel& cancel) {
com.place_cancel(cancel);
}
};
int main() {
MomentumBot* m = new MomentumBot(1001);
assert(m != NULL);
Manager::Manager manager;
manager.register_bot(m);
manager.run();
return 0;
}