File size: 10,006 Bytes
f86fb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "../../include/order_book/matching_engine.hpp"
#include <algorithm>

namespace quant::order_book {

MatchingEngine::MatchingEngine(core::Symbol symbol, ExecutionCallback on_exec)
    : book_(symbol), on_exec_(std::move(on_exec)) {}

void MatchingEngine::emit_report(
    core::ClientId client_id,
    core::ClientOrderId client_order_id,
    core::OrderId engine_order_id,
    core::ExecType exec_type,
    core::Price fill_price,
    core::Quantity fill_qty,
    core::Quantity leaves_qty,
    core::RejectReason reason
) noexcept {
    if (!on_exec_) return;

    protocol::ExecutionReportPayload report;
    report.client_id = client_id;
    report.client_order_id = client_order_id;
    report.engine_order_id = engine_order_id;
    report.exec_id = next_exec_id_++;
    report.exec_type = static_cast<uint8_t>(exec_type);
    report.fill_price_raw = fill_price.raw();
    report.fill_qty = fill_qty;
    report.leaves_qty = leaves_qty;
    report.reject_reason = static_cast<uint8_t>(reason);

    on_exec_(report);
}

void MatchingEngine::process_new_order(
    core::ClientId client_id,
    core::ClientOrderId client_order_id,
    core::Side side,
    core::OrderType type,
    core::Price price,
    core::Quantity qty,
    core::TimestampNs timestamp_ns
) {
    if (qty == 0) {
        emit_report(client_id, client_order_id, 0, core::ExecType::REJECTED,
                    price, 0, 0, core::RejectReason::INVALID_QUANTITY);
        return;
    }

    if (type == core::OrderType::LIMIT && price.raw() <= 0) {
        emit_report(client_id, client_order_id, 0, core::ExecType::REJECTED,
                    price, 0, 0, core::RejectReason::INVALID_PRICE);
        return;
    }

    core::OrderId engine_order_id = next_engine_order_id_++;
    core::Quantity remaining_qty = qty;

    // Matching logic
    if (side == core::Side::BUY) {
        // Match against resting ASKS (lowest price first)
        while (remaining_qty > 0 && !book_.asks().empty()) {
            auto best_ask_it = book_.asks().begin();
            core::Price best_ask_price = best_ask_it->first;

            if (type == core::OrderType::LIMIT && price < best_ask_price) {
                break; // No more matching asks at or below limit buy price
            }

            Order* resting = best_ask_it->second.head;
            if (!resting) {
                book_.asks().erase(best_ask_it);
                continue;
            }

            core::Quantity match_qty = std::min(remaining_qty, resting->leaves_qty);
            remaining_qty -= match_qty;
            resting->leaves_qty -= match_qty;
            best_ask_it->second.total_volume -= match_qty;
            total_volume_ += match_qty;
            ++total_trades_;

            bool maker_filled = (resting->leaves_qty == 0);
            core::Quantity resting_leaves = resting->leaves_qty;
            core::OrderId resting_order_id = resting->order_id;
            core::ClientId resting_client_id = resting->client_id;
            core::ClientOrderId resting_client_order_id = resting->client_order_id;

            if (maker_filled) {
                book_.remove_order(resting);
                book_.free_order(resting);
            }

            // Emit executions for resting maker
            emit_report(
                resting_client_id,
                resting_client_order_id,
                resting_order_id,
                maker_filled ? core::ExecType::FILL : core::ExecType::PARTIAL_FILL,
                best_ask_price,
                match_qty,
                resting_leaves,
                core::RejectReason::NONE
            );

            // Emit execution for incoming taker
            emit_report(
                client_id,
                client_order_id,
                engine_order_id,
                remaining_qty == 0 ? core::ExecType::FILL : core::ExecType::PARTIAL_FILL,
                best_ask_price,
                match_qty,
                remaining_qty,
                core::RejectReason::NONE
            );
        }

        // Post-match leaves handling
        if (remaining_qty > 0) {
            if (type == core::OrderType::LIMIT) {
                Order* order = book_.allocate_order();
                if (order) {
                    order->order_id = engine_order_id;
                    order->client_id = client_id;
                    order->client_order_id = client_order_id;
                    order->symbol = book_.symbol();
                    order->price = price;
                    order->qty = qty;
                    order->leaves_qty = remaining_qty;
                    order->side = side;
                    order->order_type = type;
                    order->timestamp_ns = timestamp_ns;

                    book_.add_resting_order(order);
                    emit_report(client_id, client_order_id, engine_order_id,
                                core::ExecType::NEW, price, 0, remaining_qty, core::RejectReason::NONE);
                }
            } else if (type == core::OrderType::IOC || type == core::OrderType::MARKET) {
                emit_report(client_id, client_order_id, engine_order_id,
                            core::ExecType::CANCELED, price, 0, remaining_qty, core::RejectReason::NONE);
            }
        }
    } else if (side == core::Side::SELL) {
        // Match against resting BIDS (highest price first)
        while (remaining_qty > 0 && !book_.bids().empty()) {
            auto best_bid_it = book_.bids().begin();
            core::Price best_bid_price = best_bid_it->first;

            if (type == core::OrderType::LIMIT && price > best_bid_price) {
                break; // No more matching bids at or above limit sell price
            }

            Order* resting = best_bid_it->second.head;
            if (!resting) {
                book_.bids().erase(best_bid_it);
                continue;
            }

            core::Quantity match_qty = std::min(remaining_qty, resting->leaves_qty);
            remaining_qty -= match_qty;
            resting->leaves_qty -= match_qty;
            best_bid_it->second.total_volume -= match_qty;
            total_volume_ += match_qty;
            ++total_trades_;

            bool maker_filled = (resting->leaves_qty == 0);
            core::Quantity resting_leaves = resting->leaves_qty;
            core::OrderId resting_order_id = resting->order_id;
            core::ClientId resting_client_id = resting->client_id;
            core::ClientOrderId resting_client_order_id = resting->client_order_id;

            if (maker_filled) {
                book_.remove_order(resting);
                book_.free_order(resting);
            }

            // Emit executions for resting maker
            emit_report(
                resting_client_id,
                resting_client_order_id,
                resting_order_id,
                maker_filled ? core::ExecType::FILL : core::ExecType::PARTIAL_FILL,
                best_bid_price,
                match_qty,
                resting_leaves,
                core::RejectReason::NONE
            );

            // Emit execution for incoming taker
            emit_report(
                client_id,
                client_order_id,
                engine_order_id,
                remaining_qty == 0 ? core::ExecType::FILL : core::ExecType::PARTIAL_FILL,
                best_bid_price,
                match_qty,
                remaining_qty,
                core::RejectReason::NONE
            );
        }

        // Post-match leaves handling
        if (remaining_qty > 0) {
            if (type == core::OrderType::LIMIT) {
                Order* order = book_.allocate_order();
                if (order) {
                    order->order_id = engine_order_id;
                    order->client_id = client_id;
                    order->client_order_id = client_order_id;
                    order->symbol = book_.symbol();
                    order->price = price;
                    order->qty = qty;
                    order->leaves_qty = remaining_qty;
                    order->side = side;
                    order->order_type = type;
                    order->timestamp_ns = timestamp_ns;

                    book_.add_resting_order(order);
                    emit_report(client_id, client_order_id, engine_order_id,
                                core::ExecType::NEW, price, 0, remaining_qty, core::RejectReason::NONE);
                }
            } else if (type == core::OrderType::IOC || type == core::OrderType::MARKET) {
                emit_report(client_id, client_order_id, engine_order_id,
                            core::ExecType::CANCELED, price, 0, remaining_qty, core::RejectReason::NONE);
            }
        }
    }
}

bool MatchingEngine::cancel_order(
    core::ClientId client_id,
    core::ClientOrderId client_order_id,
    core::OrderId engine_order_id,
    core::TimestampNs timestamp_ns
) {
    (void)timestamp_ns;
    Order* order = nullptr;
    if (engine_order_id != 0) {
        order = book_.find_order(engine_order_id);
    }
    if (!order && client_order_id != 0) {
        order = book_.find_order_by_client_id(client_order_id);
    }

    if (!order) {
        emit_report(client_id, client_order_id, engine_order_id,
                    core::ExecType::REJECTED, core::Price(), 0, 0,
                    core::RejectReason::ORDER_NOT_FOUND);
        return false;
    }

    core::Quantity leaves = order->leaves_qty;
    book_.remove_order(order);
    emit_report(client_id, client_order_id, order->order_id,
                core::ExecType::CANCELED, order->price, 0, leaves,
                core::RejectReason::NONE);
    book_.free_order(order);
    return true;
}

bool MatchingEngine::check_invariants() const noexcept {
    auto bb = book_.best_bid();
    auto ba = book_.best_ask();
    if (bb && ba) {
        return *bb < *ba; // Crossed or locked book invariant check
    }
    return true;
}

} // namespace quant::order_book