-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderbook.h
157 lines (146 loc) · 5.57 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
#ifndef ORDER_BOOK_HEADER
#define ORDER_BOOK_HEADER
#include<bits/stdc++.h>
#include"snapshotRefresh.h"
struct OrderBookEntry{
int64_t sending_time;
int order_count;
int quantity;
double price;
int price_level;
int entry_type;
OrderBookEntry() = default;
OrderBookEntry(SnapShotRefreshGroup ssrg, int64_t sending_time){
this->sending_time = sending_time;
this->order_count = ssrg.num_orders;
this->quantity = ssrg.entry_sz;
this->price = ssrg.entry_px;
this->price_level = ssrg.price_level;
}
bool updateEntry(SnapShotRefreshGroup ssrg, int64_t sending_time){
if (this->sending_time > sending_time){
return false;
}
this->sending_time = sending_time;
this->order_count = ssrg.num_orders;
this->quantity = ssrg.entry_sz;
this->price = (double)ssrg.entry_px;
this->price_level = ssrg.price_level;
return 1;
}
};
struct OrderBook{
int32_t security_id;
std::map<int, OrderBookEntry>bids;
std::map<int, OrderBookEntry>asks;
OrderBook() = default;
OrderBook(int32_t sec_id){
this->security_id = sec_id;
}
bool addEntry(SnapShotRefreshGroup ssrg, int64_t sending_time){
int level = ssrg.price_level;
if (ssrg.price_level == 127 && ssrg.entry_type != 6){
std::cout << ssrg.price_level << std::endl;
exit(0);
}
if (ssrg.entry_type == 0){
// std::cout << "adding\n";
if(bids.find(level) == bids.end()){
bids[level] = OrderBookEntry(ssrg, sending_time);
return true;
}
else return bids[level].updateEntry(ssrg, sending_time);
}
else if(ssrg.entry_type == 1){
if(asks.find(level) == asks.end()){
asks[level] = OrderBookEntry(ssrg, sending_time);
return true;
}
else return asks[level].updateEntry(ssrg, sending_time);
}
else if(ssrg.entry_type == 6){
return false; //settlement not important?
}
else {
printf("What type of entry type is this\n");
std::cout << ssrg.entry_type << std::endl;
return false;
}
}
void printBook()
{
std::cout << "Security ID: " << this->security_id << "\n\n";
if(bids.size() == 0){
std::cout << "No Buy Orders Recorded\n";
}
else{
std::cout << std::left << std::setw(12) << "Bid"
<< std::left << std::setw(12) << "Order Count"
<< std::left << std::setw(12) << "Quantity"
<< std::left << std::setw(12) << "Price" << std::endl;
for(auto bidIt = bids.begin(); bidIt != bids.end(); bidIt++)
{
std::cout << std::left << std::setw(12) << bidIt->second.price_level
<< std::left << std::setw(12) << bidIt->second.order_count
<< std::left << std::setw(12) << bidIt->second.quantity
<< std::left << std::setw(20) << std::fixed << std::setprecision(2) << bidIt->second.price * 1e-9;
std::cout << std::endl;
}
std::cout << std::endl;
}
if(asks.size() == 0){
std::cout << "No Ask Orders Recorded\n";
}
else {
std::cout << std::left << std::setw(12) << "Ask"
<< std::left << std::setw(12) << "Quantity"
<< std::left << std::setw(12) << "Order Count"
<< std::left << std::setw(12) << "Price" << std::endl;
for(auto askIt = asks.begin(); askIt != asks.end(); askIt++){
std::cout << std::left << std::setw(12) << askIt->second.price_level
<< std::left << std::setw(12) << askIt->second.order_count
<< std::left << std::setw(12) << askIt->second.quantity
<< std::left << std::setw(20) << std::fixed << std::setprecision(2) << askIt->second.price * 1e-9
<< std::endl;
}
std::cout << std::endl ;
}
}
};
struct OrderBookCollection{
std::map<int32_t, OrderBook>collection;
void updateOrderBook(SnapShotFullRefresh snapshot, int64_t send_time){
for (int i = 0; i < snapshot.ssrgs.group_cnt; i++){
SnapShotRefreshGroup ssrg = snapshot.ssrgs.groups[i];
int sec_id = snapshot.security_id;
if(collection.find(sec_id) == collection.end()){
collection[sec_id] = OrderBook(sec_id);
}
collection[sec_id].addEntry(ssrg, send_time);
}
}
void printOrderBook(int32_t security_id){
if(security_id == 0){
std::vector<int32_t> all_security_ids;
for(auto it : this->collection){
all_security_ids.push_back(it.first);
}
security_id = all_security_ids[getRandomNumber(all_security_ids.size())];
}
auto orderBookIt = collection.find(security_id);
if (orderBookIt == collection.end()){
std::cout << "Sorry. Could not find orderbook with this Security ID" << std::endl;
return;
}
orderBookIt->second.printBook();
}
void printSecurityIDs(){
std::cout << "Total OrderBooks : " << collection.size() << std::endl;
std:: cout << "Security IDs: ";
for(auto it:collection){
std::cout << it.first << " ";
}
std::cout << std::endl;
}
};
#endif