-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderBook.cpp
More file actions
235 lines (205 loc) · 5.89 KB
/
OrderBook.cpp
File metadata and controls
235 lines (205 loc) · 5.89 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
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
#include <iostream>
#include <map>
#include <algorithm>
#include "OrderBook.h"
#include "CSVReader.h"
using namespace std;
OrderBook::OrderBook(string filename)
{
orders = CSVReader::readCSV(filename);
}
vector<string> OrderBook::getKnownProducts()
{
vector<string> products;
map<string, bool> productMap;
for (auto const &order : orders)
{
productMap[order.getProduct()] = true;
}
// Flatten map to vector
for (auto const &product : productMap)
{
products.push_back(product.first);
}
return products;
}
vector<OrderBookEntry> OrderBook::getOrders(
OrderBookType type,
string product,
string timestamp)
{
vector<OrderBookEntry> _orders;
for (auto const &order : orders)
{
if (order.getOrderType() == type &&
order.getProduct() == product &&
order.getTimestamp() == timestamp)
{
_orders.push_back(order);
}
}
return _orders;
}
/**
* Get all orders for a given product between two timestamps
* @param type The type of order (bid or ask)
* @param product The product to get orders for
* @param startTime The start of the time period
* @param endTime The end of the time period
* @returns A vector of orders
*/
vector<OrderBookEntry> OrderBook::getOrdersForTimePeriod(
OrderBookType type,
string product,
string startTime,
string endTime)
{
vector<OrderBookEntry> _orders;
for (auto const &order : orders)
{
if (order.getOrderType() == type &&
order.getProduct() == product &&
order.getTimestamp() >= startTime &&
order.getTimestamp() <= endTime)
{
_orders.push_back(order);
}
}
return _orders;
}
string OrderBook::getEarliestTime()
{
return orders[0].getTimestamp();
}
string OrderBook::getNextTime(string timestamp)
{
string _timestamp = "";
for (auto const &order : orders)
{
if (order.getTimestamp() > timestamp)
{
_timestamp = order.getTimestamp();
break;
}
if (_timestamp == "")
{
_timestamp = getEarliestTime();
}
}
return _timestamp;
}
void OrderBook::insertOrder(OrderBookEntry &order)
{
orders.push_back(order);
sort(orders.begin(), orders.end(), OrderBookEntry::compareByTimestamp);
}
vector<OrderBookEntry> OrderBook::matchAsksToBids(string product, string timestamp)
{
cout << "Matching asks to bids for " << product << " at " << timestamp << endl;
vector<OrderBookEntry> asks = getOrders(OrderBookType::ask, product, timestamp);
vector<OrderBookEntry> bids = getOrders(OrderBookType::bid, product, timestamp);
vector<OrderBookEntry> sales;
cout << "Max ask: " << OrderBook::getHighPrice(asks) << endl;
cout << "Min ask: " << OrderBook::getLowPrice(asks) << endl;
cout << "Max bid: " << OrderBook::getHighPrice(bids) << endl;
cout << "Min bid: " << OrderBook::getLowPrice(bids) << endl;
sort(asks.begin(), asks.end(), OrderBookEntry::compareByPriceAsc);
sort(bids.begin(), bids.end(), OrderBookEntry::compareByPriceDesc);
for (auto &ask : asks)
{
for (auto &bid : bids)
{
if (bid.getPrice() >= ask.getPrice())
{
OrderBookEntry sale{
ask.getPrice(),
0,
timestamp,
product,
OrderBookType::asksale};
// Handle usernames
if (bid.getUsername() == "user")
{
sale.setUsername("user");
sale.setOrderType(OrderBookType::bidsale);
}
else if (ask.getUsername() == "user")
{
sale.setUsername("user");
sale.setOrderType(OrderBookType::asksale);
}
// Match bids and asks
if (bid.getAmount() == ask.getAmount())
{
sale.setAmount(ask.getAmount());
sales.push_back(sale);
bid.setAmount(0);
break;
}
if (bid.getAmount() > ask.getAmount())
{
sale.setAmount(ask.getAmount());
sales.push_back(sale);
bid.setAmount(bid.getAmount() - ask.getAmount());
break;
}
if (bid.getAmount() < ask.getAmount() && bid.getAmount() > 0)
{
sale.setAmount(bid.getAmount());
sales.push_back(sale);
ask.setAmount(ask.getAmount() - bid.getAmount());
bid.setAmount(0);
continue;
}
}
}
}
return sales;
}
double OrderBook::getHighPrice(vector<OrderBookEntry> &orders)
{
double highPrice = 0;
for (auto const &order : orders)
{
if (order.getPrice() > highPrice)
{
highPrice = order.getPrice();
}
}
return highPrice;
}
double OrderBook::getLowPrice(vector<OrderBookEntry> &orders)
{
double lowPrice = 0;
for (size_t i = 0; i < orders.size(); i++)
{
if (i == 0)
{
lowPrice = orders[i].getPrice();
}
else if (orders[i].getPrice() < lowPrice)
{
lowPrice = orders[i].getPrice();
}
}
return lowPrice;
}
double OrderBook::getPriceSpread(vector<OrderBookEntry> &orders)
{
double highPrice = getHighPrice(orders);
double lowPrice = getLowPrice(orders);
return highPrice - lowPrice;
}
double OrderBook::getPriceSpread(double min, double max)
{
return max - min;
}
double OrderBook::getVolume(vector<OrderBookEntry> &orders)
{
double volume = 0;
for (auto const &order : orders)
{
volume += order.getAmount();
}
return volume;
}