Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Orderbook.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,12 @@ class Orderbook {
? asks_.at(price)
: bids_.at(price);

// Swap-and-pop: move the last element into the cancelled slot, then
// pop the back. This is O(1) and keeps the vector contiguous.
// We must update the swapped order's stored index to reflect its new position.
if (idx != orders.size() - 1) {
orders[idx] = std::move(orders.back());
orders_.at(orders[idx]->GetOrderId()).location_ = idx;
// Erase in place to preserve FIFO time priority within the price level.
// The shifted suffix needs fresh indices in the lookup map.
orders.erase(orders.begin() + static_cast<std::ptrdiff_t>(idx));
for (std::size_t i = idx; i < orders.size(); ++i) {
orders_.at(orders[i]->GetOrderId()).location_ = i;
}
orders.pop_back();

if (side == Side::Sell) {
if (orders.empty()) asks_.erase(price);
Expand Down Expand Up @@ -500,4 +498,4 @@ class Orderbook {
void ResetMarketDataStats() { stats_.Reset(); }
bool IsInitialized() const { return isInitialized_; }
uint64_t GetLastSequenceNumber() const { return lastSequenceNumber_; }
};
};
18 changes: 17 additions & 1 deletion tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ TEST(TestTimePriority_FIFO) {
ASSERT_EQ(trades[0].GetBidTrade().orderId_, 1);
}

TEST(TestCancelPreservesFIFO) {
Orderbook orderbook;
orderbook.AddOrder(std::make_shared<Order>(OrderType::GoodTillCancel, 1, Side::Buy, 100, 10));
orderbook.AddOrder(std::make_shared<Order>(OrderType::GoodTillCancel, 2, Side::Buy, 100, 10));
orderbook.AddOrder(std::make_shared<Order>(OrderType::GoodTillCancel, 3, Side::Buy, 100, 10));

orderbook.CancelOrder(1);
auto trades = orderbook.AddOrder(
std::make_shared<Order>(OrderType::GoodTillCancel, 4, Side::Sell, 100, 10)
);

ASSERT_EQ(trades.size(), 1);
ASSERT_EQ(trades[0].GetBidTrade().orderId_, 2);
}

TEST(TestMarketOrderBuy) {
Orderbook orderbook;
orderbook.AddOrder(std::make_shared<Order>(OrderType::GoodTillCancel, 1, Side::Sell, 100, 10));
Expand Down Expand Up @@ -589,6 +604,7 @@ int main() {
RUN_TEST(TestMultipleMatchesAtSamePrice);
RUN_TEST(TestPricePriority);
RUN_TEST(TestTimePriority_FIFO);
RUN_TEST(TestCancelPreservesFIFO);
RUN_TEST(TestMarketOrderBuy);
RUN_TEST(TestMarketOrderSell);
RUN_TEST(TestMarketOrderEmptyBook);
Expand All @@ -603,7 +619,7 @@ int main() {
RUN_TEST(TestMinNotionalValidation);
RUN_TEST(TestMarketOrderValidation);

std::cout << "\nAll " << 21 << " functionality tests passed!\n";
std::cout << "\nAll " << 22 << " functionality tests passed!\n";

PrintPerformanceHeader();

Expand Down