-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderBook.h
193 lines (170 loc) · 6.89 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
#pragma once
#include "Order.h"
#include "DoublyLinkedList.h"
#include <unordered_map>
#include <tuple>
#include <tuple>
#include <queue>
#include <vector>
#include <algorithm>
// PriceSidePair is a tuple of price and side
using PriceSidePair = std::tuple<double, Side::Enum>;
// ComparePrice is used for bestAsk
struct ComparePrice {
bool operator()(const std::tuple<double, int>& lhs, const std::tuple<double, int>& rhs) const {
return std::get<0>(lhs) > std::get<0>(rhs);
}
};
// ComparePrice2 is used for bestBid
struct ComparePrice2 {
bool operator()(const std::tuple<double, int>& lhs, const std::tuple<double, int>& rhs) const {
return std::get<0>(lhs) < std::get<0>(rhs);
}
};
// Hash function for std::tuple so that it can be used as a key in std::unordered_map
struct MyHash {
std::size_t operator()(const std::tuple<double, Side::Enum>& k) const {
// Compute individual hash values for first and second argument (use std::hash<T>{}(item))
// and combine them using XOR and bit shifting:
return std::hash<double>{}(std::get<0>(k)) ^ (std::hash<int>{}(std::get<1>(k)) << 1);
}
};
class OrderBook
{
public:
OrderBook()
{
quantityMap[std::make_tuple(100.0, Side::Buy)] = 10;
}
int GetVolumeAtPrice(double price, Side::Enum side)
{
if(quantityMap.count(std::make_tuple(price, side)) > 0)
return quantityMap[std::make_tuple(price, side)];
return 0;
}
void addOrderToBook(Order order)
{
if(queueMap.count(std::make_tuple(order.getPrice(), order.getSide())) <= 0)
{
Node* newNode = new Node();
newNode->data = order.getOrderId();
queueMap[std::make_tuple(order.getPrice(), order.getSide())] = *newNode;
quantityMap[std::make_tuple(order.getPrice(), order.getSide())] = order.getQuantity();
}
else
{
Node* head = &queueMap.at(std::make_tuple(order.getPrice(), order.getSide()));
insertEnd(&head, order.getOrderId());
quantityMap[std::make_tuple(order.getPrice(), order.getSide())] += order.getQuantity();
}
}
void addOrderToBook(Order order, std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice2> book)
{
if(queueMap.count(std::make_tuple(order.getPrice(), order.getSide())) <= 0)
{
Node* newNode = new Node();
newNode->data = order.getOrderId();
queueMap[std::make_tuple(order.getPrice(), order.getSide())] = *newNode;
quantityMap[std::make_tuple(order.getPrice(), order.getSide())] = order.getQuantity();
}
else
{
Node* head = &queueMap.at(std::make_tuple(order.getPrice(), order.getSide()));
insertEnd(&head, order.getOrderId());
quantityMap[std::make_tuple(order.getPrice(), order.getSide())] += order.getQuantity();
}
}
void CancelOrder(int orderId)
{
if(orderMap.count(orderId) > 0){
Order order = orderMap[orderId];
Node* head = &queueMap.at(std::make_tuple(order.getPrice(), order.getSide()));
deleteNode(&head, head);
auto sameBook = bestBid;
if(head == NULL){
if(order.getSide() == Side::Buy)
{
auto sameBook = bestBid;
}
else
{
auto sameBook = bestAsk;
}
sameBook.pop();
}
quantityMap[std::make_tuple(order.getPrice(), order.getSide())] -= order.getQuantity();
orderMap.erase(orderId);
}
}
void PlaceOrder(Order order)
{
orderMap[order.getOrderId()] = order;
auto oppositeBook = bestAsk;
auto sameBook = bestBid;
if (order.getSide() == Side::Buy)
{
std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice> oppositeBook = bestAsk;
}
else
{
std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice2> oppositeBook = bestBid;
}
if (order.getSide() == Side::Buy)
{
std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice2> sameBook = bestBid;
}
else
{
std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice> sameBook = bestAsk;
}
while(order.getQuantity()>0 && oppositeBook.size()>0 && order.getPrice() >= std::get<0>(oppositeBook.top()))
{
auto otherOrder = oppositeBook.top();
double tradePrice = std::get<0>(otherOrder);
int tradeQuantity = std::min(orderMap.at(std::get<1>(otherOrder)).getQuantity(), order.getQuantity());
Side::Enum oppositeSide = orderMap.at(std::get<1>(otherOrder)).getSide();
orderMap.at(std::get<1>(otherOrder)).Fill(tradeQuantity);
order.Fill(tradeQuantity);
std::cout<<"Trade by "<<orderMap.at(std::get<1>(otherOrder)).getClient()<<"taken by "<<order.getClient()<<", "<< tradeQuantity<< " shares @ "<<tradePrice<<std::endl;
if(orderMap.at(std::get<1>(otherOrder)).isFilled())
{
CancelOrder(std::get<1>(otherOrder));
}
if(order.getQuantity() > 0){
addOrderToBook(order);
}
// if(order.getPrice() >= tradePrice)
// {
// int oppositeQuantity = quantityMap[std::make_tuple(tradePrice, oppositeSide)];
// if(order.getQuantity() >= oppositeQuantity)
// {
// order.Fill(oppositeQuantity);
// quantityMap[std::make_tuple(oppositePrice, oppositeSide)] = 0;
// oppositeBook.pop();
// }
// else
// {
// quantityMap[std::make_tuple(oppositePrice, oppositeSide)] -= order.getQuantity();
// order.Fill(order.getQuantity());
// }
// }
// else
// {
// break;
// }
}
}
void print()
{
for(auto it = quantityMap.begin(); it != quantityMap.end(); it++)
{
std::cout<<std::get<0>(it->first)<<" "<<std::get<1>(it->first)<<" "<<it->second<<std::endl;
}
}
private:
std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice> bestAsk;
std::priority_queue<std::tuple<double, int>, std::vector<std::tuple<double, int>>, ComparePrice2> bestBid;
std::unordered_map<int, Order> orderMap;
std::unordered_map<std::tuple<double, Side::Enum>, int, MyHash> quantityMap;
std::unordered_map<std::tuple<double, Side::Enum>, Node, MyHash> queueMap;
};