Skip to content
Merged
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
66 changes: 36 additions & 30 deletions include/orderbook/pricelevelstack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace sadhbhcraft::orderbook
QuantityType quantity_filled = 0;

auto it = m_orders.begin();
auto end = m_orders.end();
const auto end = m_orders.end();

for (; it != end; ++it)
{
Expand Down Expand Up @@ -191,44 +191,50 @@ namespace sadhbhcraft::orderbook
PriceLevelCompare<MySide> price_compare;

auto it = m_levels.begin();
const auto end = m_levels.end();

if (price_compare(*it, order))
// Loop will skip empty side, and call to price_compare() in first
// iteration will effectively skip all matching if order doesn't
// cross the book. Loop ends when either aggressor's order quantity
// is fulfilled, or price limit has been reached.
for (; it != end; ++it)
{
for (; it != m_levels.end(); ++it)
if (quantity_of(order) == quantity_filled)
{
// Order was fully filled. All aggressor's order quantity
// has been matched
break;
}
else if (price_compare(order, *it))
{
if (quantity_of(order) == quantity_filled)
{
break; //< Order was fully filled
}
else if (price_compare(order, *it))
{
break; //< Order was partially filled
}
// Order was partially filled. Price limit reached or order
// didn't cross the book
break;
}

auto res = it->match_order(
order,
quantity_of(order) - quantity_filled,
std::forward<ExecutionPolicy>(execution_policy));
auto res = it->match_order(
order,
quantity_of(order) - quantity_filled,
std::forward<ExecutionPolicy>(execution_policy));

while (res)
{
auto executed = res();
co_yield executed;
quantity_filled += executed.quantity;
}

if (!it->empty())
{
// Level wasn't fully filled
break;
}
while (res)
{
auto executed = res();
co_yield executed;
quantity_filled += executed.quantity;
}

// Remove all levels that were fully filled, but keep the one
// that still has quantity left
m_levels.erase(m_levels.begin(), it);
if (!it->empty())
{
// Level wasn't fully filled
break;
}
}

// Remove all levels that were fully filled, but keep the one
// that still has quantity left
m_levels.erase(m_levels.begin(), it);

co_return;
}

Expand Down
Loading