-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.cpp
More file actions
139 lines (128 loc) · 4.77 KB
/
Copy pathcli.cpp
File metadata and controls
139 lines (128 loc) · 4.77 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
#include "include/OrderBook.hpp"
#include "include/Analytics.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
static void help() {
std::cout << R"(
Commands:
---------
BUY <qty> @ <price> limit buy
SELL <qty> @ <price> limit sell
MBUY <qty> market buy
MSELL <qty> market sell
CANCEL <id> cancel order
MODIFY <id> <price> <qty> modify order
BOOK show order book
STATS show analytics
HELP show this menu
QUIT exit
Example:
BUY 100 @ 15000
SELL 50 @ 15100
CANCEL 1
MODIFY 2 15050 80
)";
}
int main() {
Analytics analytics;
OrderBook book("CLI", [&](const Trade& t) {
std::cout << " [TRADE] #" << t.buy_order_id
<< " buy x #" << t.sell_order_id
<< " sell @ " << t.price
<< " qty=" << t.quantity << "\n";
analytics.on_trade(t);
});
OrderId next_id = 1;
std::cout << "Order Book CLI (type HELP for commands)\n";
std::cout << std::string(44, '-') << "\n";
std::string line;
while (true) {
std::cout << "\norderbook> ";
if (!std::getline(std::cin, line)) break;
// trim leading spaces
std::size_t start = line.find_first_not_of(" \t\r");
if (start == std::string::npos) { book.print_book(); continue; }
line = line.substr(start);
if (line.empty()) { book.print_book(); continue; }
std::istringstream ss(line);
std::string cmd;
ss >> cmd;
// uppercase
for (auto& c : cmd) c = (char)toupper(c);
if (cmd == "QUIT" || cmd == "EXIT" || cmd == "Q") {
std::cout << " Bye!\n";
break;
}
else if (cmd == "HELP" || cmd == "H") {
help();
}
else if (cmd == "BOOK" || cmd == "B") {
book.print_book();
}
else if (cmd == "STATS") {
analytics.snapshot_depth(book);
analytics.print_stats();
double imb = Analytics::order_imbalance(book);
std::cout << std::fixed << std::setprecision(4);
std::cout << " Imbalance : " << imb
<< " (" << (imb > 0 ? "+" : "") << imb * 100 << "% bid)\n";
analytics.print_heatmap(8);
}
else if (cmd == "BUY" || cmd == "SELL") {
// BUY <qty> @ <price>
Quantity qty; char at; Price price;
if (!(ss >> qty >> at >> price) || at != '@') {
std::cout << " Usage: " << cmd << " <qty> @ <price>\n";
continue;
}
Side side = (cmd == "BUY") ? Side::Buy : Side::Sell;
Order o = book.submit_limit(next_id, side, price, qty);
std::cout << " [#" << next_id << "] " << to_string(o.status)
<< " " << cmd
<< " qty=" << o.quantity
<< " price=" << price
<< " filled=" << o.filled << "\n";
++next_id;
}
else if (cmd == "MBUY" || cmd == "MSELL") {
// MBUY <qty>
Quantity qty;
if (!(ss >> qty)) {
std::cout << " Usage: " << cmd << " <qty>\n";
continue;
}
Side side = (cmd == "MBUY") ? Side::Buy : Side::Sell;
Order o = book.submit_market(next_id, side, qty);
std::cout << " [#" << next_id << "] MARKET " << (side == Side::Buy ? "BUY" : "SELL")
<< " qty=" << o.quantity
<< " filled=" << o.filled
<< " status=" << to_string(o.status) << "\n";
++next_id;
}
else if (cmd == "CANCEL" || cmd == "C") {
OrderId id;
if (!(ss >> id)) { std::cout << " Usage: CANCEL <id>\n"; continue; }
bool ok = book.cancel(id);
std::cout << " [#" << id << "] " << (ok ? "CANCELLED" : "NOT FOUND") << "\n";
}
else if (cmd == "MODIFY" || cmd == "M") {
// MODIFY <id> <price> <qty>
OrderId id; Price price; Quantity qty;
if (!(ss >> id >> price >> qty)) {
std::cout << " Usage: MODIFY <id> <new_price> <new_qty>\n";
continue;
}
bool ok = book.modify(id, price, qty);
std::cout << " [#" << id << "] "
<< (ok ? "MODIFIED -> price=" + std::to_string(price)
+ " qty=" + std::to_string(qty)
: "NOT FOUND") << "\n";
}
else {
std::cout << " Unknown command: " << cmd << " (type HELP)\n";
}
}
return 0;
}