-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
188 lines (158 loc) · 7.65 KB
/
Copy pathmain.cpp
File metadata and controls
188 lines (158 loc) · 7.65 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
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
#include "include/Analytics.hpp"
#include "include/Exchange.hpp"
#include "include/OrderBook.hpp"
#include "include/ThreadedExchange.hpp"
#include <iomanip>
#include <iostream>
#include <random>
#include <thread>
static void banner(const char* title) {
std::cout << "\n" << std::string(55, '=') << '\n';
std::cout << " " << title << '\n';
std::cout << std::string(55, '=') << '\n';
}
// ─── Phase 1: Core Order Book ─────────────────────────────────────────────────
void phase1() {
banner("PHASE 1: CORE ORDER BOOK");
Analytics analytics;
OrderBook book("AAPL", [&](const Trade& t) {
std::cout << " [TRADE] buy#" << t.buy_order_id
<< " x sell#" << t.sell_order_id
<< " @ " << t.price
<< " qty=" << t.quantity << '\n';
analytics.on_trade(t);
});
// Resting limit orders (prices in cents: 15000 = $150.00)
std::cout << "\n-- Resting limit orders --\n";
book.submit_limit(1, Side::Buy, 15000, 100); // BUY 100 @ 150.00
book.submit_limit(2, Side::Buy, 14900, 200); // BUY 200 @ 149.00
book.submit_limit(3, Side::Sell, 15100, 50); // SELL 50 @ 151.00
book.submit_limit(4, Side::Sell, 15200, 75); // SELL 75 @ 152.00
book.print_book();
// Price-time priority: two buys at same price, then a crossing sell
std::cout << "-- FIFO demo: two buys @ 15100, then SELL 120 @ 15100 --\n";
book.submit_limit(5, Side::Buy, 15100, 100);
book.submit_limit(6, Side::Buy, 15100, 50);
// Expected: order 5 gets 100, order 6 gets 20
Order sell7 = book.submit_limit(7, Side::Sell, 15100, 120);
std::cout << " Sell #7 -> status=" << to_string(sell7.status)
<< " filled=" << sell7.filled << "/" << sell7.quantity << '\n';
book.print_book();
// Market order
std::cout << "-- Market SELL 30 --\n";
Order mkt = book.submit_market(8, Side::Sell, 30);
std::cout << " Market #8 -> status=" << to_string(mkt.status)
<< " filled=" << mkt.filled << '\n';
book.print_book();
// Cancel
std::cout << "-- CANCEL order #2 --\n";
std::cout << " cancel(2)=" << (book.cancel(2) ? "OK" : "FAILED") << '\n';
book.print_book();
// Modify
std::cout << "-- MODIFY order #4: price=15050, qty=60 --\n";
book.modify(4, 15050, 60);
book.print_book();
// Market data summary
std::cout << " Best bid : " << book.best_bid().value_or(0) << '\n';
std::cout << " Best ask : " << book.best_ask().value_or(0) << '\n';
std::cout << " Spread : " << book.spread().value_or(0) << '\n';
std::cout << std::fixed << std::setprecision(1);
std::cout << " Mid : " << book.mid_price().value_or(0) << '\n';
analytics.print_stats();
}
// ─── Phase 3: Exchange Simulation ────────────────────────────────────────────
void phase3() {
banner("PHASE 3: EXCHANGE SIMULATION");
Exchange exchange("AAPL");
SimStats s = exchange.run_simulation(100'000, 4);
std::cout << '\n';
std::cout << " Orders submitted : " << s.orders_submitted << '\n';
std::cout << " Filled : " << s.orders_filled << '\n';
std::cout << " Partially filled : " << s.orders_partial << '\n';
std::cout << " Resting (new) : " << s.orders_new << '\n';
std::cout << " Rejected : " << s.orders_rejected << '\n';
std::cout << " Trades executed : " << s.trades_executed << '\n';
std::cout << " Total volume : " << s.total_volume << '\n';
std::cout << std::fixed << std::setprecision(0);
std::cout << " Elapsed : " << s.elapsed_ms << " ms\n";
std::cout << " Throughput : " << s.orders_per_sec << " orders/sec\n";
std::cout << " Trade rate : " << s.trades_per_sec << " trades/sec\n";
exchange.book().print_book(5);
}
// ─── Phase 4: Multithreading ──────────────────────────────────────────────────
void phase4() {
banner("PHASE 4: MULTITHREADING");
RiskParams risk;
risk.max_net_position = 20000;
risk.max_order_size = 500;
ThreadedExchange tex("AAPL", risk);
tex.start();
constexpr int NUM_ORDERS = 100'000;
std::mt19937 rng(7);
std::uniform_int_distribution<Price> price_dist(9950, 10050);
std::uniform_int_distribution<Quantity> qty_dist(1, 100);
std::bernoulli_distribution side_dist(0.5);
std::cout << "\n Submitting " << NUM_ORDERS << " orders from producer thread...\n";
auto t0 = std::chrono::high_resolution_clock::now();
for (int i = 1; i <= NUM_ORDERS; ++i) {
Order o;
o.id = (OrderId)i;
o.side = side_dist(rng) ? Side::Buy : Side::Sell;
o.type = OrderType::Limit;
o.status = OrderStatus::New;
o.price = price_dist(rng);
o.quantity = qty_dist(rng);
o.filled = 0;
o.timestamp = std::chrono::steady_clock::now();
tex.submit(o);
}
// Wait until the matching thread drains the queue
while (tex.orders_processed() < NUM_ORDERS)
std::this_thread::sleep_for(std::chrono::milliseconds(5));
tex.stop();
auto t1 = std::chrono::high_resolution_clock::now();
double ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
auto snap = tex.market_data();
std::cout << '\n';
std::cout << " Orders submitted : " << NUM_ORDERS << '\n';
std::cout << " Orders processed : " << tex.orders_processed() << '\n';
std::cout << " Orders rejected : " << tex.orders_rejected() << '\n';
std::cout << " Risk net pos : N/A (see RiskChecker)\n";
std::cout << std::fixed << std::setprecision(0);
std::cout << " Wall-clock time : " << ms << " ms\n";
std::cout << " Throughput : " << (int)(NUM_ORDERS / (ms / 1000.0)) << " orders/sec\n";
if (snap.bid) std::cout << " Best bid : " << *snap.bid << '\n';
if (snap.ask) std::cout << " Best ask : " << *snap.ask << '\n';
if (snap.mid) std::cout << std::fixed << std::setprecision(1)
<< " Mid : " << *snap.mid << '\n';
}
// ─── Phase 5: Analytics ───────────────────────────────────────────────────────
void phase5() {
banner("PHASE 5: ANALYTICS");
Analytics analytics;
OrderBook book("AAPL", [&](const Trade& t) { analytics.on_trade(t); });
std::mt19937 rng(123);
std::uniform_int_distribution<Price> price_dist(9950, 10050);
std::uniform_int_distribution<Quantity> qty_dist(1, 200);
std::bernoulli_distribution side_dist(0.5);
for (int i = 1; i <= 1000; ++i) {
Side s = side_dist(rng) ? Side::Buy : Side::Sell;
book.submit_limit((OrderId)i, s, price_dist(rng), qty_dist(rng));
}
analytics.snapshot_depth(book, 20);
analytics.print_stats();
double imb = Analytics::order_imbalance(book, 10);
std::cout << std::fixed << std::setprecision(4);
std::cout << "\n Order imbalance : " << imb
<< " (+" << std::setprecision(1) << imb * 100 << "% bid-side)\n";
analytics.print_heatmap(10);
book.print_book(5);
}
// ─── Entry point ─────────────────────────────────────────────────────────────
int main() {
phase1();
phase3();
phase4();
phase5();
return 0;
}