-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (158 loc) · 4.74 KB
/
main.cpp
File metadata and controls
170 lines (158 loc) · 4.74 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
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
#include <iostream>
#include <algorithm>
#include <vector>
// at each price level, how many orders are there
class price_level
{
public:
float price;
double size;
};
// limit order book
class book
{
private:
std::vector<price_level> bids;
std::vector<price_level> asks;
public:
// best bid and ask
double get_best_bid()
{
return 0;
}
double get_best_ask()
{
return 0;
}
// add price level according to the order
void AddPriceLevel(price_level& pl, bool is_bid)
{
if (is_bid)
{
bids.push_back(pl); // add price level to bids
std::sort(bids.begin(), bids.end(), [](auto const& l, auto const& r)
{
return l.price > r.price;
}); // sort bids in descending order
}
else
{
asks.push_back(pl); // add price level to asks
std::sort(asks.begin(), asks.end(), [](auto const& l, auto const& r)
{
return l.price < r.price;
}); // sort asks in ascending order
}
}
// delete price level if the price is the same
void DeletePriceLevel(price_level& pl, bool is_bid)
{
if (is_bid)
{
/*
- `std::remove_if` rearranges the elements in the range `[bid.begin(), bids.end())` such that the elements satisfying the condition are moved to the end of the range.
- a lambda function is used to determine the condition for the item to be moved.
- `std::remove_if` returns an iterator pointing to the new logical end of the sequence, where the elements that do not satisfy the condition now reside.
- The erase function is then called to remove elements from the container. It takes two arguments: the start iterator of the range to remove (returned by std::remove_if), and the real end iterator of the container.
- So, the second bids.end() is simply providing the real end iterator of the bids container.
*/
bids.erase(std::remove_if(bids.begin(), bids.end(), [&](auto const& l)
{
return l.price == pl.price;
}), bids.end()); // remove price level from bids
}
else
{
asks.erase(std::remove_if(asks.begin(), asks.end(), [&](auto const& l)
{
return l.price == pl.price;
}), asks.end()); // remove price level from asks
}
}
// method to update price level
void UpdatePriceLevel(price_level& pl, bool is_bid)
{
// find price level
std::vector<price_level>::iterator it; // what is iterator
if (is_bid)
{
it = std::find_if(bids.begin(), bids.end(), [&](auto const& l)
{
return l.price == pl.price;
}); // find price level in bids
}
else
{
it = std::find_if(asks.begin(), asks.end(), [&](auto const& l)
{
return l.price == pl.price;
}); // find price level in asks
}
if (it != bids.end())
{
it->price = pl.price; // update price
it->size = pl.size; // update size
}
}
};
class aggregated_book
{
private:
std::vector<book> all_books;
public:
void AddPriceLevel(int venue_id, price_level& pl, bool is_bid)
{
all_books[venue_id].AddPriceLevel(pl, is_bid);
}
void DeletePriceLevel(int venue_id, price_level& pl, bool is_bid)
{
all_books[venue_id].DeletePriceLevel(pl, is_bid);
}
void UpdatePriceLevel(int venue_id, price_level& pl, bool is_bid)
{
all_books[venue_id].UpdatePriceLevel(pl, is_bid);
}
double get_best_bid()
{
double best_bid;
for (book& b : all_books)
{
if (best_bid == 0)
best_bid = b.get_best_bid();
else
best_bid = std::max(best_bid, b.get_best_bid());
}
return best_bid;
}
double get_best_ask()
{
double best_ask;
for (book& b: all_books)
{
if (best_ask == 0)
best_ask = b.get_best_ask();
else
best_ask = std::min(best_ask, b.get_best_ask());
}
return best_ask;
}
};
int main()
{
price_level pl;
pl.price = 100;
pl.size = 1000;
book b;
b.AddPriceLevel(pl, true);
b.AddPriceLevel(pl, false);
b.DeletePriceLevel(pl, true);
b.UpdatePriceLevel(pl, true);
aggregated_book ab;
ab.AddPriceLevel(0, pl, true);
ab.AddPriceLevel(0, pl, false);
ab.DeletePriceLevel(0, pl, true);
ab.UpdatePriceLevel(0, pl, true);
ab.get_best_bid();
ab.get_best_ask();
return 0;
}