-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallAuction.cpp
58 lines (45 loc) · 1.42 KB
/
CallAuction.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
struct Order {
bool isBuy;
double price;
int quantity;
};
class CallAuction {
private:
std::vector<Order> orders;
public:
void addOrder (bool isBuy, double price, int quantity) {
Order order = {isBuy, price, quantity};
orders.push_back(order);
}
void clearAuction () {
std::vector<Order> buyOrders;
std::vector<Order> sellOrders;
for (const auto& order : orders) {
if (order.isBuy) { buyOrders.push_back(order); }
else { sellOrders.push_back(order); }
}
}
std::sort(buyOrders.begin(), buyOrders.end(), [](const Order& a, const Order& b) { return a.price > b.price; });
std::sort(sellOrders.begin(), sellOrders.end(), [](const Order& a, const Order& b) { return a.price < b.price; });
int i = 0, j = 0;
double clearingPrice = 0;
int clearingQuantity = 0;
while (i<buyOrders.size() && j<sellOrders.size()) {
if (buyOrders[i].price >= sellOrders[j].price) {
int tradeQuantity = std::min(buyOrders[i].quantity, sellOrders[j].quantity);
clearingPrice = sellOrders[j].price;
clearingQuantity += tradeQuantity;
buyOrders[i].quantity -= tradeQuantity;
sellOrders[j].quantity -= tradeQuantity;
if (buyOrders[i].quantity == 0) { i++; }
if (sellOrders[j].quantity == 0) {j++; }
} else {
break;
}
}
std::cout << "Clear Price: " << clearingPrice << std::endl;
std::cout << "Clear Quantity: " << clearingQuantity << std::endl;
}