14 MarketHandler MarketManager::_default;
19 for (
const auto& order : _orders)
20 _order_pool.Release(order.second);
24 for (
auto order_book_ptr : _order_books)
25 if (order_book_ptr !=
nullptr)
26 _order_book_pool.Release(order_book_ptr);
30 for (
auto symbol_ptr : _symbols)
31 if (symbol_ptr !=
nullptr)
32 _symbol_pool.Release(symbol_ptr);
39 if (_symbols.size() <= symbol.
Id)
40 _symbols.resize(symbol.
Id + 1,
nullptr);
43 Symbol* symbol_ptr = _symbol_pool.Create(symbol);
46 assert((_symbols[symbol.
Id] ==
nullptr) &&
"Duplicate symbol detected!");
47 if (_symbols[symbol.
Id] !=
nullptr)
50 _symbol_pool.Release(symbol_ptr);
53 _symbols[symbol.
Id] = symbol_ptr;
63 assert(((
id < _symbols.size()) && (_symbols[
id] !=
nullptr)) &&
"Symbol not found!");
64 if ((_symbols.size() <=
id) || (_symbols[
id] ==
nullptr))
68 Symbol* symbol_ptr = _symbols[id];
74 _symbols[id] =
nullptr;
77 _symbol_pool.Release(symbol_ptr);
84 assert(((symbol.
Id < _symbols.size()) && (_symbols[symbol.
Id] !=
nullptr)) &&
"Symbol not found!");
85 if ((_symbols.size() <= symbol.
Id) || (_symbols[symbol.
Id] ==
nullptr))
89 Symbol* symbol_ptr = _symbols[symbol.
Id];
92 if (_order_books.size() <= symbol.
Id)
93 _order_books.resize(symbol.
Id + 1,
nullptr);
96 OrderBook* order_book_ptr = _order_book_pool.Create(*
this, *symbol_ptr);
99 assert((_order_books[symbol.
Id] ==
nullptr) &&
"Duplicate order book detected!");
100 if (_order_books[symbol.
Id] !=
nullptr)
103 _order_book_pool.Release(order_book_ptr);
106 _order_books[symbol.
Id] = order_book_ptr;
116 assert(((
id < _order_books.size()) && (_order_books[
id] !=
nullptr)) &&
"Order book not found!");
117 if ((_order_books.size() <=
id) || (_order_books[
id] ==
nullptr))
121 OrderBook* order_book_ptr = _order_books[id];
127 _order_books[id] =
nullptr;
130 _order_book_pool.Release(order_book_ptr);
146 return AddMarketOrder(order,
false);
148 return AddLimitOrder(order,
false);
151 return AddStopOrder(order,
false);
154 return AddStopLimitOrder(order,
false);
160 ErrorCode MarketManager::AddMarketOrder(
const Order& order,
bool recursive)
164 if (order_book_ptr ==
nullptr)
167 Order new_order(order);
173 if (_matching && !recursive)
174 MatchMarket(order_book_ptr, &new_order);
180 if (_matching && !recursive)
181 Match(order_book_ptr);
184 order_book_ptr->ResetMatchingPrice();
189 ErrorCode MarketManager::AddLimitOrder(
const Order& order,
bool recursive)
193 if (order_book_ptr ==
nullptr)
196 Order new_order(order);
202 if (_matching && !recursive)
203 MatchLimit(order_book_ptr, &new_order);
206 if ((new_order.LeavesQuantity > 0) && !new_order.IsIOC() && !new_order.IsFOK())
209 OrderNode* order_ptr = _order_pool.Create(new_order);
212 if (!_orders.insert(std::make_pair(order_ptr->Id, order_ptr)).second)
218 _order_pool.Release(order_ptr);
224 UpdateLevel(*order_book_ptr, order_book_ptr->AddOrder(order_ptr));
233 if (_matching && !recursive)
234 Match(order_book_ptr);
237 order_book_ptr->ResetMatchingPrice();
242 ErrorCode MarketManager::AddStopOrder(
const Order& order,
bool recursive)
246 if (order_book_ptr ==
nullptr)
249 Order new_order(order);
252 if (new_order.IsTrailingStop() || new_order.IsTrailingStopLimit())
253 new_order.StopPrice = order_book_ptr->CalculateTrailingStopPrice(new_order);
259 if (_matching && !recursive)
262 uint64_t stop_price = new_order.IsBuy() ? order_book_ptr->GetMarketPriceAsk() : order_book_ptr->GetMarketPriceBid();
265 bool arbitrage = new_order.IsBuy() ? (new_order.StopPrice <= stop_price) : (new_order.StopPrice >= stop_price);
271 new_order.StopPrice = 0;
278 MatchMarket(order_book_ptr, &new_order);
284 if (_matching && !recursive)
285 Match(order_book_ptr);
288 order_book_ptr->ResetMatchingPrice();
295 if (new_order.LeavesQuantity > 0)
298 OrderNode* order_ptr = _order_pool.Create(new_order);
301 if (!_orders.insert(std::make_pair(order_ptr->Id, order_ptr)).second)
307 _order_pool.Release(order_ptr);
313 if (order_ptr->IsTrailingStop() || order_ptr->IsTrailingStopLimit())
314 order_book_ptr->AddTrailingStopOrder(order_ptr);
316 order_book_ptr->AddStopOrder(order_ptr);
325 if (_matching && !recursive)
326 Match(order_book_ptr);
329 order_book_ptr->ResetMatchingPrice();
334 ErrorCode MarketManager::AddStopLimitOrder(
const Order& order,
bool recursive)
338 if (order_book_ptr ==
nullptr)
341 Order new_order(order);
344 if (new_order.IsTrailingStop() || new_order.IsTrailingStopLimit())
346 int64_t diff = new_order.Price - new_order.StopPrice;
347 new_order.StopPrice = order_book_ptr->CalculateTrailingStopPrice(new_order);
348 new_order.Price = new_order.StopPrice + diff;
355 if (_matching && !recursive)
358 uint64_t stop_price = new_order.IsBuy() ? order_book_ptr->GetMarketPriceAsk() : order_book_ptr->GetMarketPriceBid();
361 bool arbitrage = new_order.IsBuy() ? (new_order.StopPrice <= stop_price) : (new_order.StopPrice >= stop_price);
366 new_order.StopPrice = 0;
372 MatchLimit(order_book_ptr, &new_order);
375 if ((new_order.LeavesQuantity > 0) && !new_order.IsIOC() && !new_order.IsFOK())
378 OrderNode* order_ptr = _order_pool.Create(new_order);
381 if (!_orders.insert(std::make_pair(order_ptr->Id, order_ptr)).second)
387 _order_pool.Release(order_ptr);
393 UpdateLevel(*order_book_ptr, order_book_ptr->AddOrder(order_ptr));
402 if (_matching && !recursive)
403 Match(order_book_ptr);
406 order_book_ptr->ResetMatchingPrice();
413 if (new_order.LeavesQuantity > 0)
416 OrderNode* order_ptr = _order_pool.Create(new_order);
419 if (!_orders.insert(std::make_pair(order_ptr->Id, order_ptr)).second)
425 _order_pool.Release(order_ptr);
431 if (order_ptr->IsTrailingStop() || order_ptr->IsTrailingStopLimit())
432 order_book_ptr->AddTrailingStopOrder(order_ptr);
434 order_book_ptr->AddStopOrder(order_ptr);
443 if (_matching && !recursive)
444 Match(order_book_ptr);
447 order_book_ptr->ResetMatchingPrice();
460 assert((
id > 0) &&
"Order Id must be greater than zero!");
463 assert((quantity > 0) &&
"Order quantity must be greater than zero!");
468 auto order_it = _orders.find(
id);
469 assert((order_it != _orders.end()) &&
"Order not found!");
470 if (order_it == _orders.end())
476 if (order_book_ptr ==
nullptr)
498 switch (order_ptr->
Type)
501 UpdateLevel(*order_book_ptr, order_book_ptr->ReduceOrder(order_ptr, quantity, hidden, visible));
505 order_book_ptr->ReduceStopOrder(order_ptr, quantity, hidden, visible);
509 order_book_ptr->ReduceTrailingStopOrder(order_ptr, quantity, hidden, visible);
512 assert(
false &&
"Unsupported order type!");
522 switch (order_ptr->
Type)
525 UpdateLevel(*order_book_ptr, order_book_ptr->ReduceOrder(order_ptr, quantity, hidden, visible));
529 order_book_ptr->ReduceStopOrder(order_ptr, quantity, hidden, visible);
533 order_book_ptr->ReduceTrailingStopOrder(order_ptr, quantity, hidden, visible);
536 assert(
false &&
"Unsupported order type!");
541 _orders.erase(order_it);
544 _order_pool.Release(order_ptr);
548 if (_matching && !recursive)
549 Match(order_book_ptr);
552 order_book_ptr->ResetMatchingPrice();
559 return ModifyOrder(
id, new_price, new_quantity,
false,
false);
564 return ModifyOrder(
id, new_price, new_quantity,
true,
false);
570 assert((
id > 0) &&
"Order Id must be greater than zero!");
573 assert((new_quantity > 0) &&
"Order quantity must be greater than zero!");
574 if (new_quantity == 0)
578 auto order_it = _orders.find(
id);
579 assert((order_it != _orders.end()) &&
"Order not found!");
580 if (order_it == _orders.end())
586 if (order_book_ptr ==
nullptr)
590 switch (order_ptr->
Type)
593 UpdateLevel(*order_book_ptr, order_book_ptr->DeleteOrder(order_ptr));
597 order_book_ptr->DeleteStopOrder(order_ptr);
601 order_book_ptr->DeleteTrailingStopOrder(order_ptr);
604 assert(
false &&
"Unsupported order type!");
609 order_ptr->
Price = new_price;
630 if (_matching && !recursive)
631 MatchLimit(order_book_ptr, order_ptr);
637 switch (order_ptr->
Type)
640 UpdateLevel(*order_book_ptr, order_book_ptr->AddOrder(order_ptr));
644 order_book_ptr->AddStopOrder(order_ptr);
648 order_book_ptr->AddTrailingStopOrder(order_ptr);
651 assert(
false &&
"Unsupported order type!");
664 _orders.erase(order_it);
667 _order_pool.Release(order_ptr);
671 if (_matching && !recursive)
672 Match(order_book_ptr);
675 order_book_ptr->ResetMatchingPrice();
682 return ReplaceOrder(
id, new_id, new_price, new_quantity,
false);
688 assert((
id > 0) &&
"Order Id must be greater than zero!");
691 assert((new_id > 0) &&
"New order Id must be greater than zero!");
694 assert((new_quantity > 0) &&
"Order quantity must be greater than zero!");
695 if (new_quantity == 0)
699 auto order_it = _orders.find(
id);
700 assert((order_it != _orders.end()) &&
"Order not found!");
701 if (order_it == _orders.end())
704 assert(order_ptr->
IsLimit() &&
"Replace order operation is valid only for limit orders!");
710 if (order_book_ptr ==
nullptr)
714 switch (order_ptr->
Type)
717 UpdateLevel(*order_book_ptr, order_book_ptr->DeleteOrder(order_ptr));
721 order_book_ptr->DeleteStopOrder(order_ptr);
725 order_book_ptr->DeleteTrailingStopOrder(order_ptr);
728 assert(
false &&
"Unsupported order type!");
736 _orders.erase(order_it);
739 order_ptr->
Id = new_id;
740 order_ptr->
Price = new_price;
749 if (_matching && !recursive)
750 MatchLimit(order_book_ptr, order_ptr);
755 if (!_orders.insert(std::make_pair(order_ptr->
Id, order_ptr)).second)
761 _order_pool.Release(order_ptr);
767 switch (order_ptr->
Type)
770 UpdateLevel(*order_book_ptr, order_book_ptr->AddOrder(order_ptr));
774 order_book_ptr->AddStopOrder(order_ptr);
778 order_book_ptr->AddTrailingStopOrder(order_ptr);
781 assert(
false &&
"Unsupported order type!");
791 _order_pool.Release(order_ptr);
795 if (_matching && !recursive)
796 Match(order_book_ptr);
799 order_book_ptr->ResetMatchingPrice();
823 assert((
id > 0) &&
"Order Id must be greater than zero!");
828 auto order_it = _orders.find(
id);
829 assert((order_it != _orders.end()) &&
"Order not found!");
830 if (order_it == _orders.end())
836 if (order_book_ptr ==
nullptr)
840 switch (order_ptr->
Type)
843 UpdateLevel(*order_book_ptr, order_book_ptr->DeleteOrder(order_ptr));
847 order_book_ptr->DeleteStopOrder(order_ptr);
851 order_book_ptr->DeleteTrailingStopOrder(order_ptr);
854 assert(
false &&
"Unsupported order type!");
862 _orders.erase(order_it);
865 _order_pool.Release(order_ptr);
868 if (_matching && !recursive)
869 Match(order_book_ptr);
872 order_book_ptr->ResetMatchingPrice();
880 assert((
id > 0) &&
"Order Id must be greater than zero!");
883 assert((quantity > 0) &&
"Order quantity must be greater than zero!");
888 auto order_it = _orders.find(
id);
889 assert((order_it != _orders.end()) &&
"Order not found!");
890 if (order_it == _orders.end())
896 if (order_book_ptr ==
nullptr)
906 order_book_ptr->UpdateLastPrice(*order_ptr, order_ptr->
Price);
907 order_book_ptr->UpdateMatchingPrice(*order_ptr, order_ptr->
Price);
922 switch (order_ptr->
Type)
925 UpdateLevel(*order_book_ptr, order_book_ptr->ReduceOrder(order_ptr, quantity, hidden, visible));
929 order_book_ptr->ReduceStopOrder(order_ptr, quantity, hidden, visible);
933 order_book_ptr->ReduceTrailingStopOrder(order_ptr, quantity, hidden, visible);
936 assert(
false &&
"Unsupported order type!");
952 _orders.erase(order_it);
955 _order_pool.Release(order_ptr);
960 Match(order_book_ptr);
963 order_book_ptr->ResetMatchingPrice();
971 assert((
id > 0) &&
"Order Id must be greater than zero!");
974 assert((quantity > 0) &&
"Order quantity must be greater than zero!");
979 auto order_it = _orders.find(
id);
980 assert((order_it != _orders.end()) &&
"Order not found!");
981 if (order_it == _orders.end())
987 if (order_book_ptr ==
nullptr)
997 order_book_ptr->UpdateLastPrice(*order_ptr, price);
998 order_book_ptr->UpdateMatchingPrice(*order_ptr, price);
1013 switch (order_ptr->
Type)
1016 UpdateLevel(*order_book_ptr, order_book_ptr->ReduceOrder(order_ptr, quantity, hidden, visible));
1020 order_book_ptr->ReduceStopOrder(order_ptr, quantity, hidden, visible);
1024 order_book_ptr->ReduceTrailingStopOrder(order_ptr, quantity, hidden, visible);
1027 assert(
false &&
"Unsupported order type!");
1043 _orders.erase(order_it);
1046 _order_pool.Release(order_ptr);
1051 Match(order_book_ptr);
1054 order_book_ptr->ResetMatchingPrice();
1061 for (
auto order_book_ptr : _order_books)
1062 if (order_book_ptr !=
nullptr)
1063 Match(order_book_ptr);
1072 while ((order_book_ptr->_best_bid !=
nullptr) &&
1073 (order_book_ptr->_best_ask !=
nullptr) &&
1074 (order_book_ptr->_best_bid->
Price >= order_book_ptr->_best_ask->
Price))
1077 LevelNode* bid_level_ptr = order_book_ptr->_best_bid;
1078 LevelNode* ask_level_ptr = order_book_ptr->_best_ask;
1085 while ((bid_order_ptr !=
nullptr) && (ask_order_ptr !=
nullptr))
1088 OrderNode* next_bid_order_ptr = bid_order_ptr->next;
1089 OrderNode* next_ask_order_ptr = ask_order_ptr->next;
1092 if (bid_order_ptr->
IsAON() || ask_order_ptr->
IsAON())
1095 uint64_t chain = CalculateMatchingChain(order_book_ptr, bid_level_ptr, ask_level_ptr);
1102 if (bid_order_ptr->
IsAON())
1104 uint64_t price = bid_order_ptr->
Price;
1105 ExecuteMatchingChain(order_book_ptr, bid_level_ptr, price, chain);
1106 ExecuteMatchingChain(order_book_ptr, ask_level_ptr, price, chain);
1110 uint64_t price = ask_order_ptr->
Price;
1111 ExecuteMatchingChain(order_book_ptr, ask_level_ptr, price, chain);
1112 ExecuteMatchingChain(order_book_ptr, bid_level_ptr, price, chain);
1119 OrderNode* executing_order_ptr = bid_order_ptr;
1120 OrderNode* reducing_order_ptr = ask_order_ptr;
1121 if (executing_order_ptr->LeavesQuantity > reducing_order_ptr->LeavesQuantity)
1122 std::swap(executing_order_ptr, reducing_order_ptr);
1125 uint64_t quantity = executing_order_ptr->LeavesQuantity;
1128 uint64_t price = executing_order_ptr->Price;
1131 _market_handler.
onExecuteOrder(*executing_order_ptr, price, quantity);
1134 order_book_ptr->UpdateLastPrice(*executing_order_ptr, price);
1135 order_book_ptr->UpdateMatchingPrice(*executing_order_ptr, price);
1138 executing_order_ptr->ExecutedQuantity += quantity;
1144 _market_handler.
onExecuteOrder(*reducing_order_ptr, price, quantity);
1147 order_book_ptr->UpdateLastPrice(*reducing_order_ptr, price);
1148 order_book_ptr->UpdateMatchingPrice(*reducing_order_ptr, price);
1151 reducing_order_ptr->ExecutedQuantity += quantity;
1154 ReduceOrder(reducing_order_ptr->Id, quantity,
true);
1157 bid_order_ptr = next_bid_order_ptr;
1158 ask_order_ptr = next_ask_order_ptr;
1162 ActivateStopOrders(order_book_ptr, (LevelNode*)order_book_ptr->
best_buy_stop(), order_book_ptr->GetMarketPriceAsk());
1163 ActivateStopOrders(order_book_ptr, (LevelNode*)order_book_ptr->
best_sell_stop(), order_book_ptr->GetMarketPriceBid());
1167 if (!ActivateStopOrders(order_book_ptr))
1172 void MarketManager::MatchMarket(OrderBook* order_book_ptr, Order* order_ptr)
1175 if (order_ptr->IsBuy())
1178 if (order_book_ptr->best_ask() ==
nullptr)
1181 order_ptr->Price = order_book_ptr->best_ask()->Price;
1182 if (order_ptr->Price > (std::numeric_limits<uint64_t>::max() - order_ptr->Slippage))
1183 order_ptr->Price = std::numeric_limits<uint64_t>::max();
1185 order_ptr->Price += order_ptr->Slippage;
1190 if (order_book_ptr->best_bid() ==
nullptr)
1193 order_ptr->Price = order_book_ptr->best_bid()->Price;
1194 if (order_ptr->Price < (std::numeric_limits<uint64_t>::min() + order_ptr->Slippage))
1195 order_ptr->Price = std::numeric_limits<uint64_t>::min();
1197 order_ptr->Price -= order_ptr->Slippage;
1201 MatchOrder(order_book_ptr, order_ptr);
1204 void MarketManager::MatchLimit(OrderBook* order_book_ptr, Order* order_ptr)
1207 MatchOrder(order_book_ptr, order_ptr);
1210 void MarketManager::MatchOrder(OrderBook* order_book_ptr, Order* order_ptr)
1213 LevelNode* level_ptr;
1214 while ((level_ptr = order_ptr->IsBuy() ? order_book_ptr->_best_ask : order_book_ptr->_best_bid) !=
nullptr)
1217 bool arbitrage = order_ptr->IsBuy() ? (order_ptr->Price >= level_ptr->Price) : (order_ptr->Price <= level_ptr->Price);
1222 if (order_ptr->IsFOK() || order_ptr->IsAON())
1225 uint64_t chain = CalculateMatchingChain(order_book_ptr, level_ptr, order_ptr->Price, order_ptr->LeavesQuantity);
1232 ExecuteMatchingChain(order_book_ptr, level_ptr, order_ptr->Price, chain);
1235 _market_handler.
onExecuteOrder(*order_ptr, order_ptr->Price, order_ptr->LeavesQuantity);
1238 order_book_ptr->UpdateLastPrice(*order_ptr, order_ptr->Price);
1239 order_book_ptr->UpdateMatchingPrice(*order_ptr, order_ptr->Price);
1242 order_ptr->ExecutedQuantity += order_ptr->LeavesQuantity;
1245 order_ptr->LeavesQuantity = 0;
1251 OrderNode* executing_order_ptr = level_ptr->OrderList.front();
1254 while (executing_order_ptr !=
nullptr)
1257 OrderNode* next_executing_order_ptr = executing_order_ptr->next;
1260 uint64_t quantity = std::min(executing_order_ptr->LeavesQuantity, order_ptr->LeavesQuantity);
1263 if (executing_order_ptr->IsAON() && (executing_order_ptr->LeavesQuantity > order_ptr->LeavesQuantity))
1267 uint64_t price = executing_order_ptr->Price;
1270 _market_handler.
onExecuteOrder(*executing_order_ptr, price, quantity);
1273 order_book_ptr->UpdateLastPrice(*executing_order_ptr, price);
1274 order_book_ptr->UpdateMatchingPrice(*executing_order_ptr, price);
1277 executing_order_ptr->ExecutedQuantity += quantity;
1280 ReduceOrder(executing_order_ptr->Id, quantity,
true);
1286 order_book_ptr->UpdateLastPrice(*order_ptr, price);
1287 order_book_ptr->UpdateMatchingPrice(*order_ptr, price);
1290 order_ptr->ExecutedQuantity += quantity;
1293 order_ptr->LeavesQuantity -= quantity;
1294 if (order_ptr->LeavesQuantity == 0)
1298 executing_order_ptr = next_executing_order_ptr;
1303 bool MarketManager::ActivateStopOrders(OrderBook* order_book_ptr)
1305 bool result =
false;
1313 if (ActivateStopOrders(order_book_ptr, (LevelNode*)order_book_ptr->best_buy_stop(), order_book_ptr->GetMarketPriceAsk()) ||
1314 ActivateStopOrders(order_book_ptr, (LevelNode*)order_book_ptr->best_trailing_buy_stop(), order_book_ptr->GetMarketPriceAsk()))
1321 RecalculateTrailingStopPrice(order_book_ptr, order_book_ptr->_best_ask);
1324 if (ActivateStopOrders(order_book_ptr, (LevelNode*)order_book_ptr->best_sell_stop(), order_book_ptr->GetMarketPriceBid()) ||
1325 ActivateStopOrders(order_book_ptr, (LevelNode*)order_book_ptr->best_trailing_sell_stop(), order_book_ptr->GetMarketPriceBid()))
1332 RecalculateTrailingStopPrice(order_book_ptr, order_book_ptr->_best_bid);
1338 bool MarketManager::ActivateStopOrders(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t stop_price)
1340 bool result =
false;
1342 if (level_ptr !=
nullptr)
1345 bool arbitrage = level_ptr->IsBid() ? (stop_price <= level_ptr->Price) : (stop_price >= level_ptr->Price);
1350 OrderNode* activating_order_ptr = level_ptr->OrderList.front();
1353 while (activating_order_ptr !=
nullptr)
1356 OrderNode* next_activating_order_ptr = activating_order_ptr->next;
1359 switch (activating_order_ptr->Type)
1363 result = ActivateStopOrder(order_book_ptr, activating_order_ptr);
1367 result = ActivateStopLimitOrder(order_book_ptr, activating_order_ptr);
1370 assert(
false &&
"Unsupported order type!");
1376 activating_order_ptr = next_activating_order_ptr;
1383 bool MarketManager::ActivateStopOrder(OrderBook* order_book_ptr, OrderNode* order_ptr)
1386 if (order_ptr->IsTrailingStop() || order_ptr->IsTrailingStopLimit())
1387 order_book_ptr->DeleteTrailingStopOrder(order_ptr);
1389 order_book_ptr->DeleteStopOrder(order_ptr);
1393 order_ptr->Price = 0;
1394 order_ptr->StopPrice = 0;
1401 MatchMarket(order_book_ptr, order_ptr);
1407 _orders.erase(_orders.find(order_ptr->Id));
1410 _order_pool.Release(order_ptr);
1415 bool MarketManager::ActivateStopLimitOrder(OrderBook* order_book_ptr, OrderNode* order_ptr)
1418 if (order_ptr->IsTrailingStop() || order_ptr->IsTrailingStopLimit())
1419 order_book_ptr->DeleteTrailingStopOrder(order_ptr);
1421 order_book_ptr->DeleteStopOrder(order_ptr);
1425 order_ptr->StopPrice = 0;
1431 MatchLimit(order_book_ptr, order_ptr);
1434 if ((order_ptr->LeavesQuantity > 0) && !order_ptr->IsIOC() && !order_ptr->IsFOK())
1437 UpdateLevel(*order_book_ptr, order_book_ptr->AddOrder(order_ptr));
1445 _orders.erase(_orders.find(order_ptr->Id));
1448 _order_pool.Release(order_ptr);
1454 uint64_t MarketManager::CalculateMatchingChain(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t price, uint64_t volume)
1456 OrderNode* order_ptr = level_ptr->OrderList.front();
1457 uint64_t available = 0;
1460 while (level_ptr !=
nullptr)
1463 bool arbitrage = level_ptr->IsBid() ? (price <= level_ptr->Price) : (price >= level_ptr->Price);
1468 while (order_ptr !=
nullptr)
1470 uint64_t need = volume - available;
1471 uint64_t quantity = order_ptr->IsAON() ? order_ptr->LeavesQuantity : std::min(order_ptr->LeavesQuantity, need);
1472 available += quantity;
1475 if (volume == available)
1479 if (volume < available)
1483 order_ptr = order_ptr->next;
1487 if (order_ptr ==
nullptr)
1489 level_ptr = order_book_ptr->GetNextLevel(level_ptr);
1490 if (level_ptr !=
nullptr)
1491 order_ptr = level_ptr->OrderList.front();
1499 uint64_t MarketManager::CalculateMatchingChain(OrderBook* order_book_ptr, LevelNode* bid_level_ptr, LevelNode* ask_level_ptr)
1501 LevelNode* longest_level_ptr = bid_level_ptr;
1502 LevelNode* shortest_level_ptr = ask_level_ptr;
1503 OrderNode* longest_order_ptr = bid_level_ptr->OrderList.front();
1504 OrderNode* shortest_order_ptr = ask_level_ptr->OrderList.front();
1505 uint64_t required = longest_order_ptr->LeavesQuantity;
1506 uint64_t available = 0;
1509 if (longest_order_ptr->IsAON() && shortest_order_ptr->IsAON())
1512 if (shortest_order_ptr->LeavesQuantity > longest_order_ptr->LeavesQuantity)
1514 required = shortest_order_ptr->LeavesQuantity;
1516 std::swap(longest_level_ptr, shortest_level_ptr);
1517 std::swap(longest_order_ptr, shortest_order_ptr);
1520 else if (shortest_order_ptr->IsAON())
1522 required = shortest_order_ptr->LeavesQuantity;
1524 std::swap(longest_level_ptr, shortest_level_ptr);
1525 std::swap(longest_order_ptr, shortest_order_ptr);
1529 while ((longest_level_ptr !=
nullptr) && (shortest_level_ptr !=
nullptr))
1532 while ((longest_order_ptr !=
nullptr) && (shortest_order_ptr !=
nullptr))
1534 uint64_t need = required - available;
1535 uint64_t quantity = shortest_order_ptr->IsAON() ? shortest_order_ptr->LeavesQuantity : std::min(shortest_order_ptr->LeavesQuantity, need);
1536 available += quantity;
1539 if (required == available)
1543 if (required < available)
1545 OrderNode* next = longest_order_ptr->next;
1546 longest_order_ptr = shortest_order_ptr;
1547 shortest_order_ptr = next;
1548 std::swap(required, available);
1553 shortest_order_ptr = shortest_order_ptr->next;
1557 if (longest_order_ptr ==
nullptr)
1559 longest_level_ptr = order_book_ptr->GetNextLevel(longest_level_ptr);
1560 if (longest_level_ptr !=
nullptr)
1561 longest_order_ptr = longest_level_ptr->OrderList.front();
1565 if (shortest_order_ptr ==
nullptr)
1567 shortest_level_ptr = order_book_ptr->GetNextLevel(shortest_level_ptr);
1568 if (shortest_level_ptr !=
nullptr)
1569 shortest_order_ptr = shortest_level_ptr->OrderList.front();
1577 void MarketManager::ExecuteMatchingChain(OrderBook* order_book_ptr, LevelNode* level_ptr, uint64_t price, uint64_t volume)
1580 while ((volume > 0) && (level_ptr !=
nullptr))
1583 LevelNode* next_level_ptr = order_book_ptr->GetNextLevel(level_ptr);
1586 OrderNode* executing_order_ptr = level_ptr->OrderList.front();
1589 while ((volume > 0) && (executing_order_ptr !=
nullptr))
1592 OrderNode* next_executing_order_ptr = executing_order_ptr->next;
1597 if (executing_order_ptr->IsAON())
1600 quantity = executing_order_ptr->LeavesQuantity;
1603 _market_handler.
onExecuteOrder(*executing_order_ptr, price, quantity);
1606 order_book_ptr->UpdateLastPrice(*executing_order_ptr, price);
1607 order_book_ptr->UpdateMatchingPrice(*executing_order_ptr, price);
1610 executing_order_ptr->ExecutedQuantity += quantity;
1618 quantity = std::min(executing_order_ptr->LeavesQuantity, volume);
1621 _market_handler.
onExecuteOrder(*executing_order_ptr, price, quantity);
1624 order_book_ptr->UpdateLastPrice(*executing_order_ptr, price);
1625 order_book_ptr->UpdateMatchingPrice(*executing_order_ptr, price);
1628 executing_order_ptr->ExecutedQuantity += quantity;
1631 ReduceOrder(executing_order_ptr->Id, quantity,
true);
1638 executing_order_ptr = next_executing_order_ptr;
1642 level_ptr = next_level_ptr;
1646 void MarketManager::RecalculateTrailingStopPrice(OrderBook* order_book_ptr, LevelNode* level_ptr)
1648 if (level_ptr ==
nullptr)
1651 uint64_t new_trailing_price;
1656 uint64_t old_trailing_price = order_book_ptr->_trailing_ask_price;
1657 new_trailing_price = order_book_ptr->GetMarketTrailingStopPriceAsk();
1658 order_book_ptr->_trailing_ask_price = new_trailing_price;
1659 if (new_trailing_price >= old_trailing_price)
1664 uint64_t old_trailing_price = order_book_ptr->_trailing_bid_price;
1665 new_trailing_price = order_book_ptr->GetMarketTrailingStopPriceBid();
1666 order_book_ptr->_trailing_bid_price = new_trailing_price;
1667 if (new_trailing_price <= old_trailing_price)
1672 LevelNode* previous =
nullptr;
1673 LevelNode* current = (level_ptr->Type ==
LevelType::ASK) ? order_book_ptr->_best_trailing_buy_stop : order_book_ptr->_best_trailing_sell_stop;
1674 while (current !=
nullptr)
1676 bool recalculated =
false;
1679 OrderNode* order_ptr = current->OrderList.front();
1681 while (order_ptr !=
nullptr)
1684 OrderNode* next_order_ptr = order_ptr->next;
1686 uint64_t old_stop_price = order_ptr->StopPrice;
1687 uint64_t new_stop_price = order_book_ptr->CalculateTrailingStopPrice(*order_ptr);
1690 if (new_stop_price != old_stop_price)
1693 order_book_ptr->DeleteTrailingStopOrder(order_ptr);
1696 switch (order_ptr->Type)
1699 order_ptr->StopPrice = new_stop_price;
1703 int64_t diff = order_ptr->Price - order_ptr->StopPrice;
1704 order_ptr->StopPrice = new_stop_price;
1705 order_ptr->Price = order_ptr->StopPrice + diff;
1709 assert(
false &&
"Unsupported order type!");
1718 order_book_ptr->AddTrailingStopOrder(order_ptr);
1720 recalculated =
true;
1724 order_ptr = next_order_ptr;
1730 current = (previous !=
nullptr) ? previous : ((level_ptr->Type ==
LevelType::ASK) ? order_book_ptr->_best_trailing_buy_stop : order_book_ptr->_best_trailing_sell_stop);
1736 current = order_book_ptr->GetNextTrailingStopLevel(current);
1741 void MarketManager::UpdateLevel(
const OrderBook& order_book,
const LevelUpdate& update)
const
1743 switch (update.Type)
1746 _market_handler.
onAddLevel(order_book, update.Update, update.Top);
1749 _market_handler.
onUpdateLevel(order_book, update.Update, update.Top);
1752 _market_handler.
onDeleteLevel(order_book, update.Update, update.Top);
virtual void onUpdateOrderBook(const OrderBook &order_book, bool top)
virtual void onAddOrder(const Order &order)
virtual void onDeleteSymbol(const Symbol &symbol)
virtual void onUpdateOrder(const Order &order)
virtual void onUpdateLevel(const OrderBook &order_book, const Level &level, bool top)
virtual void onDeleteLevel(const OrderBook &order_book, const Level &level, bool top)
virtual void onDeleteOrderBook(const OrderBook &order_book)
virtual void onDeleteOrder(const Order &order)
virtual void onAddOrderBook(const OrderBook &order_book)
virtual void onAddSymbol(const Symbol &symbol)
virtual void onAddLevel(const OrderBook &order_book, const Level &level, bool top)
virtual void onExecuteOrder(const Order &order, uint64_t price, uint64_t quantity)
ErrorCode ReduceOrder(uint64_t id, uint64_t quantity)
Reduce the order by the given quantity.
const OrderBook * GetOrderBook(uint32_t id) const noexcept
Get the order book for the given symbol Id.
ErrorCode MitigateOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity)
Mitigate the order.
ErrorCode AddOrderBook(const Symbol &symbol)
Add a new order book.
ErrorCode ExecuteOrder(uint64_t id, uint64_t quantity)
Execute the order.
ErrorCode DeleteOrder(uint64_t id)
Delete the order.
ErrorCode AddSymbol(const Symbol &symbol)
Add a new symbol.
void Match()
Match crossed orders in all order books.
ErrorCode ModifyOrder(uint64_t id, uint64_t new_price, uint64_t new_quantity)
Modify the order.
ErrorCode AddOrder(const Order &order)
Add a new order.
ErrorCode ReplaceOrder(uint64_t id, uint64_t new_id, uint64_t new_price, uint64_t new_quantity)
Replace the order with a similar order but different Id, price and quantity.
ErrorCode DeleteSymbol(uint32_t id)
Delete the symbol.
ErrorCode DeleteOrderBook(uint32_t id)
Delete the order book.
const LevelNode * best_buy_stop() const noexcept
Get the order book best buy stop order price level.
const LevelNode * best_sell_stop() const noexcept
Get the order book best sell stop order price level.
Market manager definition.
@ IOC
Immediate-Or-Cancel.
C++ Trader project definitions.
uint64_t Price
Level price.
CppCommon::List< OrderNode > OrderList
Price level orders.
uint64_t VisibleQuantity() const noexcept
Order visible quantity.
ErrorCode Validate() const noexcept
Validate order parameters.
bool IsAON() const noexcept
Is the 'All-Or-None' order?
uint64_t Price
Order price.
uint64_t ExecutedQuantity
Order executed quantity.
uint64_t LeavesQuantity
Order leaves quantity.
bool IsLimit() const noexcept
Is the limit order?
OrderType Type
Order type.
uint64_t HiddenQuantity() const noexcept
Order hidden quantity.
uint32_t SymbolId
Symbol Id.
uint64_t Quantity
Order quantity.