-
Notifications
You must be signed in to change notification settings - Fork 6
/
competitor_v2.cpp
452 lines (330 loc) · 10.6 KB
/
competitor_v2.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
// 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>
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));
}
};
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);
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();
}
quantity_t quote_size(bool buy) {
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() {
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;
};
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);
books[update.ticker].print_book(log_path, open_orders);
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);
books[update.ticker].print_book(log_path, open_orders);
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);
books[update.ticker].print_book(log_path, open_orders);
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) {
return books[ticker].get_bbo(buy);
}
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;
};
class MyBot : public Bot::AbstractBot {
public:
MyState state;
using Bot::AbstractBot::AbstractBot;
static int64_t time_ns() {
using namespace std::chrono;
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
}
int64_t last = 0, start_time;
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){
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){
state.on_order_update(update);
// NOTE: the strategy here is dumb, and is just to demonstrate the API
// a way to rate limit yourself
int64_t now = time_ns();
if (now - last < 10e6) { // 10ms
return;
}
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];
// a way to put in a bid of quantity 1 at the current best bid
double best_bid = state.get_bbo(0, true);
if (best_bid != 0.0 && position < 20) { // 0.0 denotes no bid
place_order(com, Common::Order{
.ticker = 0,
.price = best_bid,
.quantity = 1,
.buy = true,
.ioc = false,
.order_id = 0, // this order ID will be chosen randomly by com
.trader_id = trader_id
});
}
}
// EDIT THIS METHOD
void on_cancel_update(Common::CancelUpdate & update, Bot::Communicator& com){
state.on_cancel_update(update);
}
// (maybe) EDIT THIS METHOD
void on_reject_order_update(Common::RejectOrderUpdate& update, Bot::Communicator& com) {
std::cout << update.getMsg() << std::endl;
}
// (maybe) EDIT THIS METHOD
void on_reject_cancel_update(Common::RejectCancelUpdate& update, Bot::Communicator& com) {
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);
}
};
int main() {
MyBot* m = new MyBot(1001);
assert(m != NULL);
Manager::Manager manager;
manager.register_bot(m);
manager.run();
return 0;
}