-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderbook.h
More file actions
139 lines (118 loc) · 3.65 KB
/
orderbook.h
File metadata and controls
139 lines (118 loc) · 3.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
#pragma once
#include <map>
#include <unordered_map>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <chrono>
// MBO Record structure
struct MBORecord {
std::string ts_recv;
std::string ts_event;
uint32_t rtype;
uint16_t publisher_id;
uint32_t instrument_id;
char action;
char side;
double price;
uint32_t size;
uint32_t flags;
uint64_t order_id;
uint32_t priority;
uint32_t channel_id;
uint64_t ts_in_delta;
std::string symbol;
MBORecord() : rtype(0), publisher_id(0), instrument_id(0), action(0), side(0),
price(0.0), size(0), flags(0), order_id(0), priority(0),
channel_id(0), ts_in_delta(0) {}
};
// MBP Record structure for output
struct MBPRecord {
std::string ts_recv;
std::string ts_event;
uint32_t rtype;
uint16_t publisher_id;
uint32_t instrument_id;
char action;
char side;
double price;
uint32_t size;
uint32_t flags;
uint64_t order_id;
uint32_t priority;
uint32_t channel_id;
uint64_t ts_in_delta;
std::string symbol;
MBPRecord() : rtype(0), publisher_id(0), instrument_id(0), action(0), side(0),
price(0.0), size(0), flags(0), order_id(0), priority(0),
channel_id(0), ts_in_delta(0) {}
};
// Order structure for tracking
struct Order {
std::string order_id;
char side;
double price;
uint32_t size;
uint32_t priority;
Order() : side(0), price(0.0), size(0), priority(0) {}
Order(const std::string& id, char s, double p, uint32_t sz, uint32_t pr)
: order_id(id), side(s), price(p), size(sz), priority(pr) {}
};
// Price level structure
struct PriceLevel {
double price;
uint32_t total_size;
uint32_t order_count;
PriceLevel() : price(0.0), total_size(0), order_count(0) {}
PriceLevel(double p, uint32_t size) : price(p), total_size(size), order_count(1) {}
};
// Trade sequence buffer for T->F->C handling
struct TradeSequence {
MBORecord trade_record;
MBORecord fill_record;
MBORecord cancel_record;
bool has_trade = false;
bool has_fill = false;
bool has_cancel = false;
void reset() {
has_trade = has_fill = has_cancel = false;
}
bool is_complete() const {
return has_trade && has_fill && has_cancel;
}
};
class OrderBook {
private:
// Bid side: higher prices first (std::greater)
std::map<double, PriceLevel, std::greater<double>> bids;
// Ask side: lower prices first (std::less - default)
std::map<double, PriceLevel, std::less<double>> asks;
// Order tracking
std::unordered_map<std::string, Order> orders;
// Trade sequence buffer
TradeSequence trade_buffer;
// Helper methods
void addOrder(const MBORecord& record);
void cancelOrder(const MBORecord& record);
void modifyOrder(const MBORecord& record);
void processTrade(const MBORecord& record);
void processTradeSequence();
// Price level management
void updatePriceLevel(char side, double price, int32_t size_delta);
void removePriceLevel(char side, double price);
public:
OrderBook();
~OrderBook();
// Main processing function
void processAction(const MBORecord& record, std::vector<MBPRecord>& output);
// Generate MBP-10 snapshot
std::vector<MBPRecord> generateMBP10() const;
// Debug functions
void printOrderBook() const;
size_t getOrderCount() const { return orders.size(); }
size_t getBidLevels() const { return bids.size(); }
size_t getAskLevels() const { return asks.size(); }
};