-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (70 loc) · 1.99 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (70 loc) · 1.99 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
#include <fstream>
#include <unordered_map>
#include "order_book.h"
void
print(std::ostream& os, uint64_t time, const std::string& ticker, PQ best_ask, PQ best_bid)
{
os << time << ',' << ticker << ',';
if (best_bid.price != 0)
os << best_bid;
else
os << ',';
os << ',';
if (best_ask.price != 0)
os << best_ask;
else
os << ',';
os << '\n';
}
int
main(int argc, const char* argv[])
{
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " FILE\n";
return 0;
}
std::ifstream input(argv[1]);
assert(input.is_open());
// Order books for each symbol
std::unordered_map<std::string, OrderBook> books;
std::string line;
// Skip the header
getline(input, line);
while (getline(input, line)) {
// Skip empty strings
if (line.find_last_not_of(" \t\n\v\f\r") == std::string::npos)
continue;
// Parse event message
Event event(line);
// Get the order book for this symbol
auto& book = books[event.ticker];
switch (event.type) {
case Event::Type::Buy:
book.buy(event.order, event.price, event.shares);
break;
case Event::Type::Sell:
book.sell(event.order, event.price, event.shares);
break;
case Event::Type::Decrease:
book.decrease(event.order, event.shares);
break;
case Event::Type::Delete:
book.remove(event.order);
break;
case Event::Type::Execute:
book.execute(event.order, event.shares);
break;
case Event::Type::Fill:
book.fill(event.order);
break;
default:
// Trades and cross-trades don't affect the order book
break;
}
if (book.changed()) {
auto ask_bid = book.best_ask_bid();
print(std::cout, event.time, event.ticker, ask_bid.first, ask_bid.second);
}
}
return 0;
}