-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderBook.h
1327 lines (1167 loc) · 28.4 KB
/
OrderBook.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ORDERBOOK_H
#define ORDERBOOK_H
#include <cstdint>
#include <time.h>
#include <string>
#include <iostream>
using namespace std;
#define NUM_ORDERS_IN_HEAP (1 << 16)
#define NUM_ORDER_BOOK_SYMBOLS (1 << 16)
/* Represents the status of an error to be returned. */
typedef enum error_status {
ACCEPT,
INVALID_AMMENDMENT_DETAILS = 101,
INVALID_ORDER_DETAILS = 303,
ORDER_DOES_NOT_EXIST = 404,
};
/* Represents an action within a command. */
typedef enum action {
NEW,
AMEND,
CANCEL,
MATCH,
QUERY
} action_t;
/* Represents an order id. */
typedef uint64_t order_id_t;
/* Represents a symbol. */
typedef string symbol_t;
/* Represents an order type. */
typedef enum order_type {
MARKET,
LIMIT,
IOC
} order_type_t;
/* Represents the strings for each order type. */
static const char* order_type_to_str[]{
"M",
"L",
"I"
};
/* Represents the two sides of a equities trade. */
typedef enum side {
BUY,
SELL
} side_t;
/* Represents the price of an order. */
typedef float price_t;
/* Represents the quantity of an order. */
typedef uint64_t quantity_t;
/* Represents the executio status of an order. */
typedef enum execution_status_t {
NOT_EXECUTED,
PARTIALLY_EXECUTED,
EXECUTED,
CANCELLED
} execution_status_t;
/* Represents alterations in the history of an order. */
typedef struct alteration_history_t {
execution_status_t status;
time_t timestamp;
price_t price;
quantity_t quantity_remaining;
alteration_history_t* next_update;
} alteration_history_t;
/* Class representing an order, buy or sell,
which has an ID, a type, and history that
tracks change to itself. */
class Order {
private:
order_id_t order_id;
order_type_t order_type;
alteration_history_t* history;
public:
Order(
order_id_t oid,
order_type_t ot,
time_t t,
price_t p,
quantity_t q) {
order_id = oid;
order_type = ot;
-- history = new alteration_history_t{
NOT_EXECUTED,
t,
p,
q,
NULL
};
}
Order()
: Order(
(order_id_t) 0,
(order_type_t) 0,
(time_t) 0,
(price_t) 0,
(quantity_t) 0) {
};
/**
* Checks if this order was active (valid) at a given
* time_t t.
*/
bool active_at(const time_t& t) const {
bool is_active = false;
alteration_history_t* ht = get_history_entry(t);
execution_status_t status = ht->status;
if ((t == -1 || history->timestamp <= t)
&& status != EXECUTED
&& status != CANCELLED) {
is_active = true;
}
return is_active;
}
/**
* Gets the relevant alteration history entry for an order.
*/
alteration_history_t *get_history_entry(
const time_t& t = -1) const {
alteration_history_t* most_recent = history;
for (alteration_history_t* eht = history;
eht != NULL;
eht = eht->next_update) {
if (t == -1 || eht->timestamp <= t) {
most_recent = eht;
} else {
break;
}
}
return most_recent;
}
/**
* Gets the status of an order at a given time t.
*/
execution_status_t get_status(const time_t& t = -1) const {
return get_history_entry(t)->status;
}
/**
* Gets the price of an order at a given time t.
*/
price_t get_price(const time_t& t = -1) const {
return get_history_entry(t)->price;
}
/**
* Gets the quantity of an order at a given time t.
*/
quantity_t get_quantity(const time_t& t = -1) const {
return get_history_entry(t)->quantity_remaining;
}
/**
* Gets the timestamp of an order at a given time t.
*/
time_t get_timestamp(const time_t& t = -1) const{
return get_history_entry(t)->timestamp;
}
/**
* Gets the order id of an order.
*/
order_id_t get_order_id() const {
return order_id;
}
/**
* Gets the order type of an order.
*/
order_type_t get_order_type() const {
return order_type;
}
/**
* Adds a given entry to the alteration history
* of the order at time t with price p and quantity q.
*/
int add_execution_history_entry(
const execution_status_t& history,
const time_t& t,
const price_t& price,
const quantity_t& q) {
alteration_history_t* new_entry
= new alteration_history_t {
history, t, price, q, NULL
};
alteration_history_t* history_entry
= get_history_entry();
if (history_entry->timestamp > t) {
delete new_entry;
} else {
history_entry->next_update = new_entry;
}
return ACCEPT;
}
/**
* Performs a transaction on the order at time t,
* bringing the price to p and the quantity to q.
*/
void transact(
const time_t& t,
const price_t& p,
const quantity_t& q) {
execution_status_t status = (q == 0)
? EXECUTED : PARTIALLY_EXECUTED;
add_execution_history_entry(status, t, p, q);
}
/**
* Performs an ammentment to a given order setting price
* to p and quantity to q.
*/
int ammend(
const price_t& p,
const quantity_t& q) {
alteration_history_t* history_entry
= get_history_entry();
add_execution_history_entry(
history_entry->status,
history_entry->timestamp,
p,
q);
return ACCEPT;
}
};
/* Represents the type of an order heap. */
typedef enum order_heap_type_t {
BUY_HEAP,
SELL_HEAP
} order_heap_type_t;
/* Min-Max heap used to represent a collection of orders
associated with the same symbol (ticker).
Main use is to get top elements, hide matched orders, etc. */
class OrderMinMaxHeap {
private:
/* Min max heap data. */
/* Active, heapified orders are first,
then inactive orders (which do not get heapified)
inactive orders are those already matched or cancelled.
after inactive orders is unused space.
e.g.
|AAAAAAAAAAAAAAIIIIIIIIIIIIIIII-----------------|
^ ^ ^
data active_end matched_end
*/
Order data[NUM_ORDERS_IN_HEAP];
/* Represents the end of active orders. */
Order* active_end;
/* Represents the end of matched orders. */
Order* matched_end;
/* Represents the used allocations in the array. */
bool allocated[NUM_ORDERS_IN_HEAP];
/* Represents the type of the heap. */
order_heap_type_t heap_type;
/* Swaps two indices in the array for eachother. */
void swap(
const int& a,
const int& b) {
Order temp = data[a];
data[a] = data[b];
data[b] = temp;
}
/* Makes an order in the array inactive. */
void make_inactive(const int& i) {
swap(i, active_end - data - 1);
allocated[active_end - data - 1] = false;
active_end--;
push_down(0);
}
/**
* Checks the precedence (sorted order) of two orders.
* i.e. a < b.
*/
bool has_precedence(
const Order& a,
const Order& b,
const time_t& t = -1) const {
return a.get_price(t) < b.get_price(t);
bool result = false;
if (((heap_type == BUY)
&& (a.get_price(t) < b.get_price(t)))
|| ((heap_type == SELL)
&& (a.get_price(t) > b.get_price(t)))
|| ((a.get_price(t) == b.get_price(t))
&& (a.get_timestamp(t) <= b.get_timestamp(t)))) {
result = true;
}
return result;
}
/**
* Checks the precedence (sorted order) of two indices
* Corresponding the orders in the array.
* i.e. a < b.
*/
bool has_precedence(
const int& a,
const int& b,
const time_t& t = -1) const {
return has_precedence(*(data + a), *(data + b), t);
}
/**
* Checks if the given indice of the array corresponds
* to an order which has children.
*/
bool has_children(const int& a) const {
return (allocated[a << 1] || allocated[a << 1 + 1]);
}
/**
* Returns the indice of the smallest descendant
* of the order associated with the given indice a.
*/
int smallest_descendant(const int& a) const {
int min = -1;
if (allocated[a << 1]) {
min = (*(data + (a << 1))).get_price();
}
if (allocated[a << 1 + 1]) {
if (min == -1
|| (*(data + (a << 1))).get_price() < min) {
min = (*(data + (a << 1))).get_price();
}
}
return min;
}
/**
* Returns the indice of the parent of the node
* that corresponds to the order at given indice m.
*/
int get_parent(const int& m) const {
return m << 1;
}
/**
* Checks if the node at index a is the grandchild
* of the node at index b.
*/
bool is_grandchild_of(
const int& a,
const int& b) const {
bool result = false;
for (int i = a;
i > b;
i = i << 1) {
result = true;
}
return result;
}
void push_down_min(const int& m) {
/* while m has children then :
i := m
m := index of the smallest child or
grandchild of i */
for (int i = m, mi = m;
has_children(mi);
i = mi, mi = smallest_descendant(mi)) {
/* if h[m] < h[i] then: */
if (has_precedence(mi, i)) {
/* if m is a grandchild of i then: */
if (is_grandchild_of(mi, i)) {
/* swap h[m] and h[i] */
swap(mi, i);
/* if h[m] > h[parent(m)] then: */
if (has_precedence(m, get_parent(m))) {
/* swap h[m] and h[parent(m)] */
swap(m, get_parent(m));
}
}
else {
/* swap h[m] and h[i] */
swap(m, i);
}
}
else {
/* break */
break;
}
}
}
void push_down_max(const int& m) {
/* while m has children then :
i := m
m := index of the smallest child or
grandchild of i */
for (int i = m, mi = m;
has_children(mi);
i = mi, mi = smallest_descendant(mi)) {
/* if h[m] < h[i] then: */
if (!has_precedence(mi, i)) {
/* if m is a grandchild of i then: */
if (is_grandchild_of(mi, i)) {
/* swap h[m] and h[i] */
swap(mi, i);
/* if h[m] > h[parent(m)] then: */
if (!has_precedence(m, get_parent(m))) {
/* swap h[m] and h[parent(m)] */
swap(m, get_parent(m));
}
}
else {
/* swap h[m] and h[i] */
swap(m, i);
}
}
else {
/* break */
break;
}
}
}
void push_down(const int& m) {
/* if i is on a min level then: */
if (has_children(m)) {
push_down_min(m);
}
else {
push_down_max(m);
}
}
void push_up_min(const int& m) {
/* while i has a grandparent and
h[i] < h[grandparent(i)] then:*/
for (int i = m;
(i << 2) >= 0 && has_precedence(i, i << 2);
i <<= 2) {
/*swap h[i] and h[grandparent(i)] */
swap(i, i << 2);
/*i : = grandparent(i) */
}
}
void push_up_max(const int& m) {
/* while i has a grandparent and
h[i] < h[grandparent(i)] then:*/
for (int mi = m;
(mi << 2) >= 0 && !has_precedence(mi, mi << 2);
mi <<= 2) {
/*swap h[i] and h[grandparent(i)] */
swap(mi, mi << 2);
/*i : = grandparent(i) */
}
}
void push_up(const int& i) {
/* if i is not the root then: */
if (i != 0) {
/* if i is on a min level then: */
if (!has_children(i)) {
/* if h[i] > h[parent(i)] then: */
if (has_precedence(i, get_parent(i))) {
/* swap h[i] and h[parent(i)] */
swap(i, get_parent(i));
/* PUSH-UP-MAX(h, parent(i)) */
push_up_max(get_parent(i));
}
else {
/* PUSH-UP-MIN(h, i) */
push_up_min(i);
}
}
else {
/* if h[i] < h[parent(i)] then: */
if (has_precedence(i, get_parent(i))) {
/*swap h[i] and h[parent(i)] */
swap(i, get_parent(i));
/* PUSH-UP-MIN(h, parent(i)) */
push_up_min(get_parent(i));
}
else {
/* PUSH-UP-MAX(h, i) */
push_up_max(i);
}
}
}
}
/**
* Searches the heap for an order with a given id.
*/
int search(const order_id_t& id) {
int found_id = -1;
for (Order* d = data;
d < matched_end;
d++) {
if (allocated[d - data]
&& d->get_order_id() == id) {
found_id = d - data;
break;
}
}
return found_id;
}
public:
OrderMinMaxHeap(
const order_heap_type_t& type) {
for (Order* d = data;
d < data + NUM_ORDERS_IN_HEAP;
d++) {
*d = Order();
}
for (bool* b = allocated;
b < allocated + NUM_ORDERS_IN_HEAP;
b++) {
*b = false;
}
active_end = data;
matched_end = active_end;
heap_type = type;
}
/**
* Inserts an order in the heap.
*/
bool insert(const Order& datum) {
swap(active_end - data, matched_end - data);
/* Append */
*(active_end) = datum;
allocated[active_end - data] = true;
push_up(active_end - data);
active_end++;
matched_end++;
/* Re-heapify */
return true;
}
/**
* Ammends an order in the heap with given id,
* with a new given price and quantity.
*/
bool ammend(
const order_id_t& id,
const price_t& new_price,
const quantity_t& new_quantity) {
int return_status = ACCEPT;
int i = search(id);
if (i != -1) {
Order* o = data + i;
o->ammend(new_price, new_quantity);
o->add_execution_history_entry(
o->get_history_entry()->status,
o->get_timestamp(),
new_price,
new_quantity);
push_down(0);
}
else {
return_status = ORDER_DOES_NOT_EXIST;
}
return return_status;
}
/**
* Cancels an order in the heap with given id.
*/
int cancel(const order_id_t& id) {
int return_status = ACCEPT;
int i = search(id);
if (i != -1) {
Order* o = (data + i);
(data + i)->add_execution_history_entry(
CANCELLED,
o->get_timestamp(),
o->get_price(),
o->get_quantity());
make_inactive(i);
push_down(0);
}
else {
return_status = ORDER_DOES_NOT_EXIST;
}
return return_status;
}
/**
* Returns the maximum order in the heap.
* Since we sort by priority, this is the highest
* priority order.
*/
Order* get_max_order() {
Order* max;
if (!allocated[0]) {
max = NULL;
}
else if (((!allocated[1] || (has_precedence(0, 1))
&& (!allocated[2] || has_precedence(0, 2))))) {
max = data;
}
else if ((!allocated[2] || has_precedence(1, 2))) {
max = data + 1;
}
else {
max = data + 2;
}
return max;
}
/**
* Gets the maximum n orders in the heap at time t,
* and places them in the output array.
* Since we sort by priority, these are the n highest
* priority orders.
*/
int get_max_n_orders(
const time_t& t,
Order* output,
const int& n) const {
/* For each element */
Order *stack = new Order[n];
int stack_i = 0;
for (const Order* d = data;
d < matched_end;
d++) {
if (!d->active_at(t)) continue;
/* If it fits in the top n orders so far */
Order *t_stack = new Order[n];
/* Meaning we have less than n orders */
if ((stack_i < n)
/* Or this is bigger than the smallest */
|| (has_precedence(*(stack + n - 1), *d, t))) {
Order* a = stack;
Order* b = t_stack;
/* Place until insertion. */
for (; a <= stack + stack_i; ) {
if (((a == stack + stack_i) && (stack_i < n))
|| (has_precedence(*d, *a, t))) {
*b++ = *d;
stack_i++;
break;
} else {
*b++ = *a++;
}
}
/* Place after insertion till finished. */
for (; b < t_stack + stack_i; ) {
*b++ = *a++;
}
}
for (int i = 0; i < stack_i; i++) {
*(stack + i) = *(t_stack + i);
}
delete[] t_stack;
}
for (int i = 0; i < stack_i; i++) {
*(output + i) = *(stack + i);
}
delete[] stack;
return stack_i;
}
/**
* Performs a transaction at time t against a given
* order with id in this array, reducing the quantity
* remaining by the given amount */
bool transact(
const order_id_t& local_id,
const quantity_t& amount,
const time_t& t) {
int local_i = search(local_id);
Order* local = data + local_i;
int abs_diff = local->get_quantity(t) - amount;
if (abs_diff <= 0) {
local->transact(t, local->get_price(t), 0);
make_inactive(local_i);
} else {
local->transact(
t,
local->get_price(t),
local->get_quantity(t) - amount);
}
return true;
}
};
/* Class representing a symbol within an orderbook hash
table, storing the buys and sells for a symbol,
alongside the next symbol in the hash table entry. */
class OrderEntry {
private:
/* Stores the symbol associated with this entry. */
char* symbol;
/* Stores the buys for the symbol (ticker). */
OrderMinMaxHeap* buys;
/* Stores the sells for the symbol (ticker). */
OrderMinMaxHeap* sells;
/* Stores the next entry in this bucket. */
OrderEntry* next_entry;
public:
OrderEntry(char* s)
: symbol(s),
buys(new OrderMinMaxHeap(BUY_HEAP)),
sells(new OrderMinMaxHeap(SELL_HEAP)),
next_entry(NULL) {
symbol = s;
};
/**
* Gets the symbol associated with this entry.
*/
char* get_symbol() const {
return symbol;
}
/**
* Gets the next entry associated with this entry.
*/
OrderEntry* get_next_entry() const {
return next_entry;
}
/**
* Adds an order to the symbol (ticker) associated
* with this entry for a buying or selling side s.
*/
bool add(
const Order& o,
const side_t& s) {
switch (s) {
case BUY:
buys->insert(o);
break;
case SELL:
sells->insert(o);
break;
default: break;
}
return true;
}
/**
* Ammends an order associated with this entry's
* symbol (ticker) with a given order id, to give it
* a new price p and quantity q.
*/
bool ammend(
const side& s,
const order_id_t& oid,
const price_t& p,
const quantity_t& q) {
switch (s) {
case BUY: buys->ammend(oid, p, q); break;
case SELL: sells->ammend(oid, p, q); break;
default: break;
}
return true;
}
/**
* Cancels an order associated with this entry's
* symbol (ticker) with a given id for a given side.
*/
int cancel(
const order_id_t& oid,
const side& s) {
switch (s) {
case BUY: buys->cancel(oid); break;
case SELL: sells->cancel(oid); break;
default: break;
}
return ACCEPT;
}
/**
* Matches the orders associated with this entry's
* symbol (ticker) at a given time t.
*/
int match(const time_t &t) {
while (true) {
Order* best_buy = buys->get_max_order();
Order* best_sell = sells->get_max_order();
if (!best_buy || !best_sell) break;
/* Collect buy data. */
alteration_history_t *b_history =
best_buy->get_history_entry();
price_t b_price = b_history->price;
quantity_t b_quantity = b_history->quantity_remaining;
order_id_t b_oi = best_buy->get_order_id();
order_type_t b_t = best_buy->get_order_type();
/* Collect sell data. */
alteration_history_t* s_history =
best_sell->get_history_entry();
price_t s_price = s_history->price;
quantity_t s_quantity = s_history->quantity_remaining;
order_id_t s_oi = best_sell->get_order_id();
order_type_t s_t = best_sell->get_order_type();
if (b_price >= s_price) {
cout << symbol
<< "|"
<< b_oi
<< "," << order_type_to_str[b_t]
<< "," << b_quantity
<< "," << b_price
<< "|"
<< s_price
<< "," << s_quantity
<< "," << order_type_to_str[s_t]
<< "," << s_oi
<< endl;
buys->transact(b_oi, b_price, t);
sells->transact(s_oi, b_price, t);
} else {
break;
}
}
return ACCEPT;
}
/**
* Queries the symbol (ticker) associated with this entry
* at a given time t.
*/
int query(const time_t& t = -1) {
Order top_buys[5];
int num_buys = buys->get_max_n_orders(t, top_buys, 5);
Order min_sells[5];
int num_sells = sells->get_max_n_orders(t, min_sells, 5);
int num_to_display = (num_buys > num_sells)
? num_buys
: num_sells;
for (int i = 0; i < num_to_display; i++) {
cout << symbol << "|";
/* BuyState */
if (i < num_buys) {
const char* order_type_str = order_type_to_str[
top_buys[i].get_order_type()
];
cout << top_buys[i].get_order_id()
<< "," << order_type_str
<< "," << top_buys[i].get_quantity(t)
<< "," << top_buys[i].get_price(t);
}
cout << "|";
/* SellState */
if (i < num_sells) {
const char* order_type_str = order_type_to_str[
min_sells[i].get_order_type()
];
cout << min_sells[i].get_price(t)
<< "," << min_sells[i].get_quantity(t)
<< "," << order_type_str
<< "," << min_sells[i].get_order_id();
}
cout << endl;
}
return ACCEPT;
}
};
/**
* Represents the format of a command.
* This corresponds to the alternative of the command.
*/
typedef enum command_format_t {
F_NEW,
F_AMMEND,
F_MATCH_TIMESTAMP,
F_MATCH_TIMESTAMP_SYMBOL,
F_QUERY,
F_QUERY_SYMBOL,
F_QUERY_TIMESTAMP,
F_QUERY_TIMESTAMP_SYMBOL,
F_QUERY_SYMBOL_TIMESTAMP
} command_format_t;
/* Class representing a command for an equity order matcher. */
class Command {
public:
command_format_t format;
action_t order_action;
order_id_t order_id;
time_t timestamp;
symbol_t symbol;
side_t side;
order_type_t order_type;
price_t price;
quantity_t quantity;
Command(
action_t ao,
order_id_t oi,
time_t t,
symbol_t s,
side_t si,
order_type_t ot,
price_t p,
quantity_t q)
: order_action(ao),
order_id(oi),
timestamp(t),
symbol(s),
side(si),
order_type(ot),
price(p),
quantity(q) {};
/**
* Prints the contents of a command.
* Used for debugging.
*/
void print() {
cout << "order_action=" << order_action << ", "
<< "order_id=" << order_id << ", "
<< "timestamp=" << timestamp << ", "
<< "symbol=" << symbol << ", "
<< "side=" << side << ", "
<< "order_type=" << order_type << ", "
<< "price=" << price << ", "
<< "quantity=" << quantity
<< endl;
}
};
/**
* Represents a list of symbols (tickers)