-
Notifications
You must be signed in to change notification settings - Fork 6
/
competitor.cpp
829 lines (656 loc) · 25.2 KB
/
competitor.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// 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 quantity_t bid_weight = std::cbrt(offer_quote_size);
const quantity_t offer_weight = std::cbrt(bid_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 quantity_t bid_weight = std::sqrt(offer_quote_size);
const quantity_t offer_weight = std::sqrt(bid_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 quantity_t bid_weight = offer_quote_size;
const quantity_t offer_weight = bid_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 quantity_t bid_weight = offer_quote_size * offer_quote_size;
const quantity_t offer_weight = bid_quote_size * bid_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;
};
// 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;
};
// Momentum Bot
//
// - Part 1. Liquidity Taker: takes all orders that cross the weighted spread with IOC.
// - Part 2. Market Maker: frontruns the leading spread if the size is greater than 0.1
//
//
class MomentumBot : public Bot::AbstractBot {
public:
MyState state;
using Bot::AbstractBot::AbstractBot;
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;
state.log_path = "book.log";
start_time = time_ns();
}
// EDIT THIS METHOD
void on_trade_update(Common::TradeUpdate& update, Bot::Communicator& com){
if (DEBUG) {
std::cout << "Called on TRADE update: " << update.getMsg() << std::endl;
}
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;
if (INFO) {
std::cout << "WE WERE TRADED WITH!" << std::endl;
std::cout << update.getMsg() << std::endl;
}
}
}
quantity_t last_large_order_qty = 0;
price_t last_large_order_price = 0;
bool last_large_order_side = false;
// EDIT THIS METHOD
void on_order_update(Common::OrderUpdate & update, Bot::Communicator& com){
if (DEBUG) {
std::cout << "Called on ORDER update: " << update.getMsg() << std::endl;
}
///
/// Get the previous prices.
///
// a way to put in a bid of quantity 1 at the current best bid
price_t best_bid = state.get_bbo(0, true);
price_t best_offer = state.get_bbo(0, false);
price_t spread_size = state.get_spread(0);
quantity_t bid_quote_size = state.get_quote_size(0, true);
quantity_t offer_quote_size = state.get_quote_size(0, false);
price_t fair_price = weighted_price(best_bid, best_offer, bid_quote_size, offer_quote_size);
state.on_order_update(update);
// This order is our order -- do not respond.
if (state.submitted.count(update.order_id)) {
return;
}
constexpr quantity_t position_limit = 1000;
// Momentum on top of book: Taking side on large orders
// Other competitors are literally incapable of placing orders larger than 2000 -- due to their position limits.
if (update.quantity > 2000) {
// BUY side large order
if (update.buy && update.price > best_bid && state.positions[0] < position_limit) {
// Make a large IOC order to take best offers.
order_id_t order_id = place_order(com, Common::Order{
.ticker = 0,
.price = best_offer + 0.02,
.quantity = position_limit - state.positions[0],
.buy = true,
.ioc = true,
.order_id = 0,
.trader_id = trader_id
});
if (INFO) {
std::cout << "BIG BUY DETECTED!" << std::endl;
std::cout << " -- best_bid : " << best_bid << std::endl;
std::cout << " -- bid_quote_size : " << bid_quote_size << std::endl;
std::cout << " -- best_offer : " << best_offer << std::endl;
std::cout << " -- offer_quote_size : " << offer_quote_size << std::endl;
std::cout << " -- spread_size : " << spread_size << std::endl;
std::cout << " -- fair_price : " << fair_price << std::endl;
std::cout << " ----------------------------------------" << std::endl;
std::cout << " -- THEIR PRICE : " << update.price << std::endl;
std::cout << " -- THEIR QTY : " << update.quantity << std::endl;
std::cout << " -- OUR QTY : " << position_limit - state.positions[0] << std::endl;
std::cout << " -- ORDER ID : " << order_id << std::endl;
std::cout << " -- THEIR ORDER : " << update.order_id << std::endl;
std::cout << " ----------------------------------------" << std::endl;
}
}
if (!update.buy && update.price < best_offer && state.positions[0] > -position_limit) {
// Make a large IOC order to take best bids.
order_id_t order_id = place_order(com, Common::Order{
.ticker = 0,
.price = best_bid - 0.02,
.quantity = state.positions[0] + position_limit,
.buy = false,
.ioc = true,
.order_id = 0,
.trader_id = trader_id
});
if (INFO) {
std::cout << "BIG SELL DETECTED!" << std::endl;
std::cout << " -- best_bid : " << best_bid << std::endl;
std::cout << " -- bid_quote_size : " << bid_quote_size << std::endl;
std::cout << " -- best_offer : " << best_offer << std::endl;
std::cout << " -- offer_quote_size : " << offer_quote_size << std::endl;
std::cout << " -- spread_size : " << spread_size << std::endl;
std::cout << " -- fair_price : " << fair_price << std::endl;
std::cout << " ----------------------------------------" << std::endl;
std::cout << " -- THEIR PRICE : " << update.price << std::endl;
std::cout << " -- THEIR QTY : " << update.quantity << std::endl;
std::cout << " -- OUR QTY : " << position_limit + state.positions[0] << std::endl;
std::cout << " -- ORDER ID : " << order_id << std::endl;
std::cout << " -- THEIR ORDER : " << update.order_id << std::endl;
std::cout << " ----------------------------------------" << std::endl;
}
}
}
//// STRATEGY 1:
//// MARKET TAKING:
// got the true weighted price.
// price_t weighted_price = (best_bid * bid_quote_size + best_offer * offer_quote_size) / (bid_quote_size + offer_quote_size);
/*
if (spread_size < 2 && spread_size > 0.2 && update.quantity <= 100){
quantity_t order_size = update.quantity > 100 ? 100 : update.quantity;
// if the order crosses the weighted spread, send an IOC to hit it.
if (update.buy && update.price > fair_price + 0.02) {
order_id_t order_id = place_order(com, Common::Order{
.ticker = 0,
.price = update.price,
.quantity = order_size,
.buy = false,
.ioc = true,
.order_id = 0,
.trader_id = trader_id
});
if (INFO) {
std::cout << "SHIT BUY DETECTED!" << std::endl;
std::cout << " -- best_bid : " << best_bid << std::endl;
std::cout << " -- bid_quote_size : " << bid_quote_size << std::endl;
std::cout << " -- best_offer : " << best_offer << std::endl;
std::cout << " -- offer_quote_size : " << offer_quote_size << std::endl;
std::cout << " -- spread_size : " << spread_size << std::endl;
std::cout << " -- fair_price : " << fair_price << std::endl;
std::cout << " ----------------------------------------" << std::endl;
std::cout << " -- THEIR PRICE : " << update.price << std::endl;
std::cout << " -- THEIR QTY : " << update.quantity << std::endl;
std::cout << " -- OUR QTY : " << order_size << std::endl;
std::cout << " -- ORDER ID : " << order_id << std::endl;
std::cout << " -- THEIR ORDER : " << update.order_id << std::endl;
std::cout << " ----------------------------------------" << std::endl;
}
}
else if (!update.buy && update.price < fair_price - 0.02) {
order_id_t order_id = place_order(com, Common::Order{
.ticker = 0,
.price = update.price,
.quantity = order_size,
.buy = true,
.ioc = true,
.order_id = 0, // this order ID will be chosen randomly by com
.trader_id = trader_id
});
if (INFO) {
std::cout << "SHIT SELL DETECTED!" << std::endl;
std::cout << " -- best_bid : " << best_bid << std::endl;
std::cout << " -- bid_quote_size : " << bid_quote_size << std::endl;
std::cout << " -- best_offer : " << best_offer << std::endl;
std::cout << " -- offer_quote_size : " << offer_quote_size << std::endl;
std::cout << " -- spread_size : " << spread_size << std::endl;
std::cout << " -- fair_price : " << fair_price << std::endl;
std::cout << " ----------------------------------------" << std::endl;
std::cout << " -- THEIR PRICE : " << update.price << std::endl;
std::cout << " -- THEIR QTY : " << update.quantity << std::endl;
std::cout << " -- OUR QTY : " << order_size << std::endl;
std::cout << " -- ORDER ID : " << order_id << std::endl;
std::cout << " -- THEIR ORDER : " << update.order_id << std::endl;
std::cout << " ----------------------------------------" << std::endl;
}
}
}
*/
// Timer dump
int64_t now = time_ns();
if (TIME_INFO && (now - last > 10e9)) { // 10ms
state.log_book();
std::cout << now << ": " << std::endl;
std::cout << " -- best_bid : " << best_bid << std::endl;
std::cout << " -- bid_quote_size : " << bid_quote_size << std::endl;
std::cout << " -- best_offer : " << best_offer << std::endl;
std::cout << " -- offer_quote_size : " << offer_quote_size << std::endl;
std::cout << " -- spread_size : " << spread_size << std::endl;
last = now;
}
// a way to cancel all your open orders
// for (const auto& x : state.open_orders) {
// place_cancel(com, Common::Cancel{
// .ticker = 0,
// .order_id = x.first,
// .trader_id = trader_id
// });
// }
// a way to get your current position
// quantity_t position = state.positions[0];
// MARKET MAKING CODE
// if (best_bid != 0.0 && best_offer != 0.0 && spread_size >= 0.1 && std::abs(position) < 20) { // 0.0 denotes no bid
// place_order(com, Common::Order{
// .ticker = 0,
// .price = best_bid + 0.01,
// .quantity = 1,
// .buy = true,
// .ioc = false,
// .order_id = last_order_id, // this order ID will be chosen randomly by com
// .trader_id = trader_id
// });
// ++last_order_id;
// place_order(com, Common::Order{
// .ticker = 0,
// .price = best_offer - 0.01,
// .quantity = 1,
// .buy = false,
// .ioc = false,
// .order_id = last_order_id, // this order ID will be chosen randomly by com
// .trader_id = trader_id
// });
// ++last_order_id;
// if (INFO) {
// std::cout << "Placing spread " << best_bid+0.01 << ", " << best_offer-0.01 << std::endl;
// }
// }
}
// EDIT THIS METHOD
void on_cancel_update(Common::CancelUpdate & update, Bot::Communicator& com){
if (DEBUG) {
std::cout << "Called on CANCEL update: " << update.getMsg() << std::endl;
}
state.on_cancel_update(update);
}
// (maybe) EDIT THIS METHOD
void on_reject_order_update(Common::RejectOrderUpdate& update, Bot::Communicator& com) {
if (DEBUG) {
std::cout << "Called on REJECT ORDER update: " << update.getMsg() << std::endl;
}
std::cout << update.getMsg() << std::endl;
}
// (maybe) EDIT THIS METHOD
void on_reject_cancel_update(Common::RejectCancelUpdate& update, Bot::Communicator& com) {
if (DEBUG) {
std::cout << "Called on REJECT CANCEL update: " << update.getMsg() << std::endl;
}
if (update.reason != Common::INVALID_ORDER_ID) {
std::cout << update.getMsg() << std::endl;
}
}
// (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) {
if (trade_with_me_in_this_packet) {
price_t pnl = state.get_pnl();
std::cout << "got trade with me; pnl = "
<< std::setw(15) << std::left << pnl
<< " ; position = "
<< std::setw(5) << std::left << state.positions[0]
<< " ; pnl/s = "
<< std::setw(15) << std::left << (pnl/((time_ns() - start_time)/1e9))
<< " ; pnl/volume = "
<< std::setw(15) << std::left << (state.volume_traded ? pnl/state.volume_traded : 0.0)
<< std::endl;
}
}
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);
}
};
// 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_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.open("prices.csv");
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) {
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 << 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);
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);
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, 0, 0, 0);
state.on_cancel_update(update);
}
// (maybe) EDIT THIS METHOD
void on_reject_order_update(Common::RejectOrderUpdate& update, Bot::Communicator& com) {
}
// (maybe) EDIT THIS METHOD
void on_reject_cancel_update(Common::RejectCancelUpdate& update, Bot::Communicator& com) {
}
// (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;
}