@@ -22,69 +22,64 @@ class LimitOrderBook {
22
22
LimitTree<Side::Buy> buys;
23
23
// / a mapping of order IDs to orders (for cancellation).
24
24
UIDOrderMap orders;
25
- // / the counter for assigning unique IDs to orders.
26
- UID sequence;
27
25
28
26
public:
29
27
// / Initialize a new limit order book object.
30
- LimitOrderBook () : sells(), buys(), orders(), sequence( 1 ) { }
28
+ LimitOrderBook () : sells(), buys(), orders() { }
31
29
32
30
// / Add a new sell limit order to the book.
33
31
// /
32
+ // / @param order_id the ID for the order
34
33
// / @param size the number of shares to sell
35
34
// / @param price the limit price for the order
36
35
// / @param arrival the time the order arrived at
37
36
// / @return the order ID for the order added to the book
38
37
// /
39
- UID limit_sell (Size size, Price price, Timestamp arrival) {
40
- // put the order into the sequence map
41
- orders.insert ({sequence , {sequence , Side::Sell, size, price, arrival}});
38
+ void limit_sell (UID order_id, Size size, Price price, Timestamp arrival) {
39
+ // put the order into the map
40
+ orders.insert ({order_id , {order_id , Side::Sell, size, price, arrival}});
42
41
if (buys.best != nullptr && price <= buys.best ->key ) { // crosses
43
42
// place a market order with the limit price
44
- buys.market (&orders.at (sequence), [&](UID uid) { orders.erase (uid); });
45
- if (orders.at (sequence).size == 0 ) { // order filled
46
- orders.erase (sequence);
47
- return 0 ;
48
- }
43
+ buys.market (&orders.at (order_id), [&](UID uid) { orders.erase (uid); });
44
+ if (orders.at (order_id).size == 0 ) // order filled
45
+ orders.erase (order_id);
49
46
}
50
- sells.limit (&orders.at (sequence));
51
- return sequence++;
47
+ sells.limit (&orders.at (order_id));
52
48
}
53
49
54
50
// / Add a new buy limit order to the book.
55
51
// /
52
+ // / @param order_id the ID for the order
56
53
// / @param size the number of shares to buy
57
54
// / @param price the limit price for the order
58
55
// / @param arrival the time the order arrived at
59
56
// / @return the order ID for the order added to the book
60
57
// /
61
- UID limit_buy (Size size, Price price, Timestamp arrival) {
62
- // put the order into the sequence map
63
- orders.insert ({sequence , {sequence , Side::Buy, size, price, arrival}});
58
+ void limit_buy (UID order_id, Size size, Price price, Timestamp arrival) {
59
+ // put the order into the map
60
+ orders.insert ({order_id , {order_id , Side::Buy, size, price, arrival}});
64
61
if (sells.best != nullptr && price >= sells.best ->key ) { // crosses
65
62
// place a market order with the limit price
66
- sells.market (&orders.at (sequence), [&](UID uid) { orders.erase (uid); });
67
- if (orders.at (sequence).size == 0 ) { // order filled
68
- orders.erase (sequence);
69
- return 0 ;
70
- }
63
+ sells.market (&orders.at (order_id), [&](UID uid) { orders.erase (uid); });
64
+ if (orders.at (order_id).size == 0 ) // order filled
65
+ orders.erase (order_id);
71
66
}
72
- buys.limit (&orders.at (sequence));
73
- return sequence++;
67
+ buys.limit (&orders.at (order_id));
74
68
}
75
69
76
70
// / Add a new order to the book.
77
71
// /
78
72
// / @param side whether the order is a buy (true) or sell (false)
73
+ // / @param order_id the ID for the order
79
74
// / @param size the number of shares to buy
80
75
// / @param price the limit/market price for the order
81
76
// / @param arrival the time the order arrived at
82
77
// / @return the order ID for the order added to the book
83
78
// /
84
- inline UID limit (Side side, Size size, Price price, Timestamp arrival) {
79
+ inline void limit (Side side, UID order_id , Size size, Price price, Timestamp arrival) {
85
80
switch (side) { // send the order to the appropriate side
86
- case Side::Sell: return limit_sell (size, price, arrival);
87
- case Side::Buy: return limit_buy (size, price, arrival);
81
+ case Side::Sell: return limit_sell (order_id, size, price, arrival);
82
+ case Side::Buy: return limit_buy (order_id, size, price, arrival);
88
83
}
89
84
}
90
85
@@ -117,36 +112,39 @@ class LimitOrderBook {
117
112
118
113
// / Execute a sell market order.
119
114
// /
115
+ // / @param order_id the ID for the order
120
116
// / @param size the size of the market order
121
117
// / @arrival the arrival of the market order
122
118
// /
123
- void market_sell (Size size, Timestamp arrival) {
124
- auto order = Order (sequence , Side::Sell, size, 0 , arrival);
119
+ void market_sell (UID order_id, Size size, Timestamp arrival) {
120
+ auto order = Order (order_id , Side::Sell, size, 0 , arrival);
125
121
order.execution = arrival;
126
122
buys.market (&order, [&](UID uid) { orders.erase (uid); });
127
123
}
128
124
129
125
// / Execute a buy market order.
130
126
// /
127
+ // / @param order_id the ID for the order
131
128
// / @param size the size of the market order
132
129
// / @arrival the arrival of the market order
133
130
// /
134
- void market_buy (Size size, Timestamp arrival) {
135
- auto order = Order (sequence , Side::Buy, size, 0 , arrival);
131
+ void market_buy (UID order_id, Size size, Timestamp arrival) {
132
+ auto order = Order (order_id , Side::Buy, size, 0 , arrival);
136
133
order.execution = arrival;
137
134
sells.market (&order, [&](UID uid) { orders.erase (uid); });
138
135
}
139
136
140
137
// / Execute a market order.
141
138
// /
142
139
// / @param side whether the order is a sell or buy order
140
+ // / @param order_id the ID for the order
143
141
// / @param size the size of the market order
144
142
// / @arrival the arrival of the market order
145
143
// /
146
- inline void market (Side side, Size size, Timestamp arrival) {
144
+ inline void market (Side side, UID order_id, Size size, Timestamp arrival) {
147
145
switch (side) { // send the market order to the appropriate side
148
- case Side::Sell: { market_sell (size, arrival); break ; }
149
- case Side::Buy: { market_buy (size, arrival); break ; }
146
+ case Side::Sell: { market_sell (order_id, size, arrival); break ; }
147
+ case Side::Buy: { market_buy (order_id, size, arrival); break ; }
150
148
}
151
149
}
152
150
@@ -187,20 +185,20 @@ class LimitOrderBook {
187
185
// / @param price the limit price to get the volume for
188
186
// / @return the volume for the given limit price
189
187
// /
190
- inline Size volume_sell (Price price) { return sells.volume_at (price); }
188
+ inline Volume volume_sell (Price price) { return sells.volume_at (price); }
191
189
192
190
// / Return the total volume for the sell side of the book.
193
- inline Size volume_sell () { return sells.volume ; }
191
+ inline Volume volume_sell () { return sells.volume ; }
194
192
195
193
// / Return the total volume for the buy side of the book.
196
194
// /
197
195
// / @param price the limit price to get the volume for
198
196
// / @return the volume for the given limit price
199
197
// /
200
- inline Size volume_buy (Price price) { return buys.volume_at (price); }
198
+ inline Volume volume_buy (Price price) { return buys.volume_at (price); }
201
199
202
200
// / Return the total volume for the buy side of the book.
203
- inline Size volume_buy () { return buys.volume ; }
201
+ inline Volume volume_buy () { return buys.volume ; }
204
202
205
203
// / Return the volume at the given limit price.
206
204
// /
0 commit comments