-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderCache.cpp
More file actions
147 lines (134 loc) · 3.55 KB
/
OrderCache.cpp
File metadata and controls
147 lines (134 loc) · 3.55 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
// Todo: your implementation of the OrderCache...
#include "OrderCache.h"
#include <algorithm>
#include <iostream>
#include <deque>
void OrderCache::addOrder(Order order)
{
const auto& id = order.orderId();
const auto& user = order.user();
const auto& secId = order.securityId();
const auto isBuy = order.isBuy();
m_ordersById[id] = std::make_shared<Order>(std::move(order));
m_ordersByUser[user][id] = m_ordersById[id];
if(isBuy)
{
m_ordersBySecurity_BuySell[secId].first[id] = m_ordersById[id];
}
else
{
m_ordersBySecurity_BuySell[secId].second[id] = m_ordersById[id];
}
}
void OrderCache::cancelOrder(const std::string& orderId)
{
DeleteOrder(orderId);
}
void OrderCache::cancelOrdersForUser(const std::string& user)
{
std::deque<std::string> toRemove;
for(auto& order:m_ordersByUser[user])
{
toRemove.push_back(order.first);
}
for(auto& order:toRemove)
{
DeleteOrder(order);
}
}
void OrderCache::cancelOrdersForSecIdWithMinimumQty(const std::string& securityId, unsigned int minQty)
{
auto it = m_ordersBySecurity_BuySell.find(securityId);
if (it == m_ordersBySecurity_BuySell.end())
{
return;
}
std::deque<std::string> toRemove;
for(const auto&[secId, orders]:it->second.first)
{
if (orders->qty() >= minQty)
{
toRemove.push_back(orders->orderId());
}
}
for(const auto&[secId, orders]:it->second.second)
{
if (orders->qty() >= minQty)
{
toRemove.push_back(orders->orderId());
}
}
for(const auto& id: toRemove)
{
DeleteOrder(id);
}
}
unsigned int OrderCache::getMatchingSizeForSecurity(const std::string& securityId)
{
unsigned int totalMatchQty = 0;
auto it = m_ordersBySecurity_BuySell.find(securityId);
if (it == m_ordersBySecurity_BuySell.end())
{
return totalMatchQty;
}
const auto& [buyOrders, sellOrders] = it->second;
std::unordered_map<std::string, unsigned int> aggregatedBuy;
std::unordered_map<std::string, unsigned int> aggregatedSell;
aggregatedBuy.reserve(buyOrders.size());
aggregatedSell.reserve(sellOrders.size());
// Aggregate quantities by company
for (const auto& [id, order] : buyOrders)
{
aggregatedBuy[order->company()] += order->qty();
}
for (const auto& [id, order] : sellOrders)
{
aggregatedSell[order->company()] += order->qty();
}
// Match buy and sell orders between different companies
for (auto& [buyCompany, buyQty] : aggregatedBuy)
{
for (auto& [sellCompany, sellQty] : aggregatedSell)
{
if (buyCompany != sellCompany)
{
auto matchQty = std::min(buyQty, sellQty);
totalMatchQty += matchQty;
buyQty -= matchQty;
sellQty -= matchQty;
}
}
}
return totalMatchQty;
}
std::vector<Order> OrderCache::getAllOrders() const
{
auto result = std::vector<Order>();
result.reserve(m_ordersById.size());
for (const auto& [orderId, order] : m_ordersById)
{
result.emplace_back(*order);
}
return result;
}
void OrderCache::DeleteOrder(const std::string& id)
{
auto it = m_ordersById.find(id);
if (it == m_ordersById.end())
{
return;
}
const Order& order = *(it->second.get());
const std::string& secId = order.securityId();
const std::string& user = order.user();
m_ordersByUser[user].erase(id);
if(order.isBuy())
{
m_ordersBySecurity_BuySell[secId].first.erase(id);
}
else
{
m_ordersBySecurity_BuySell[secId].second.erase(id);
}
m_ordersById.erase(id);
}