-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_book.h
More file actions
139 lines (119 loc) · 3.6 KB
/
Copy pathorder_book.h
File metadata and controls
139 lines (119 loc) · 3.6 KB
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
#ifndef ORDER_BOOK_H
#define ORDER_BOOK_H
#include <map>
#include <queue>
#include <cstdint>
#include <cstring>
enum Side { BUY, SELL };
struct Order {
uint64_t order_id;
char symbol[8];
Side side;
double price;
int quantity;
uint64_t timestamp;
};
struct PriceLevel {
double price;
int total_quantity;
std::queue<Order> orders;
PriceLevel() : price(0.0), total_quantity(0) {}
explicit PriceLevel(double p) : price(p), total_quantity(0) {}
};
class OrderBook {
private:
char symbol_[8];
std::map<double, PriceLevel, std::greater<double>> bids_; // descending
std::map<double, PriceLevel, std::less<double>> asks_; // ascending
public:
explicit OrderBook(const char* symbol) {
snprintf(symbol_, sizeof(symbol_), "%s", symbol);
}
void add_order(const Order& order) {
if (order.side == BUY) {
auto& level = bids_[order.price];
level.price = order.price;
level.orders.push(order);
level.total_quantity += order.quantity;
} else {
auto& level = asks_[order.price];
level.price = order.price;
level.orders.push(order);
level.total_quantity += order.quantity;
}
}
bool get_best_bid(double& price, int& quantity) {
if (bids_.empty()) return false;
auto& level = bids_.begin()->second;
price = level.price;
quantity = level.total_quantity;
return true;
}
bool get_best_ask(double& price, int& quantity) {
if (asks_.empty()) return false;
auto& level = asks_.begin()->second;
price = level.price;
quantity = level.total_quantity;
return true;
}
Order* get_top_bid() {
if (bids_.empty()) return nullptr;
auto& level = bids_.begin()->second;
if (level.orders.empty()) {
bids_.erase(bids_.begin());
return get_top_bid();
}
return &level.orders.front();
}
Order* get_top_ask() {
if (asks_.empty()) return nullptr;
auto& level = asks_.begin()->second;
if (level.orders.empty()) {
asks_.erase(asks_.begin());
return get_top_ask();
}
return &level.orders.front();
}
void remove_quantity_from_top_bid(int qty) {
if (bids_.empty()) return;
auto it = bids_.begin();
auto& level = it->second;
while (qty > 0 && !level.orders.empty()) {
auto& order = level.orders.front();
if (order.quantity <= qty) {
qty -= order.quantity;
level.total_quantity -= order.quantity;
level.orders.pop();
} else {
order.quantity -= qty;
level.total_quantity -= qty;
qty = 0;
}
}
if (level.orders.empty()) {
bids_.erase(it);
}
}
void remove_quantity_from_top_ask(int qty) {
if (asks_.empty()) return;
auto it = asks_.begin();
auto& level = it->second;
while (qty > 0 && !level.orders.empty()) {
auto& order = level.orders.front();
if (order.quantity <= qty) {
qty -= order.quantity;
level.total_quantity -= order.quantity;
level.orders.pop();
} else {
order.quantity -= qty;
level.total_quantity -= qty;
qty = 0;
}
}
if (level.orders.empty()) {
asks_.erase(it);
}
}
const char* symbol() const { return symbol_; }
};
#endif