kraken: engine: DRY in top-of-book handling

This commit is contained in:
Bruno BELANYI 2022-03-12 12:18:53 +01:00
parent fae4a9d5c7
commit 8534a74c87

View file

@ -41,46 +41,42 @@ struct CallbackOnTopOfBookChange {
} }
private: private:
using info_type = std::optional<std::pair<Price, Quantity>>;
struct TopInfo { struct TopInfo {
std::optional<std::pair<Price, Quantity>> asks; info_type asks;
std::optional<std::pair<Price, Quantity>> bids; info_type bids;
}; };
TopInfo calculate_top() const { TopInfo calculate_top() const {
auto info = TopInfo{}; auto info = TopInfo{};
if (auto const bid_it = engine_.bids_.find(symbol_); auto const make_top_for = [](auto const& order_map) -> info_type {
bid_it != engine_.bids_.end()) { if (order_map.size() > 0) {
auto const& [_, bid_map] = *bid_it; auto const price = order_map.begin()->first;
if (bid_map.size() > 0) {
auto const price = bid_map.begin()->first;
int quantity = 0; int quantity = 0;
auto const [begin, end] = bid_map.equal_range(price); auto const [begin, end] = order_map.equal_range(price);
for (auto it = begin; it != end; ++it) { for (auto it = begin; it != end; ++it) {
quantity += int(it->second.quantity); quantity += int(it->second.quantity);
} }
info.bids = {price, Quantity(quantity)}; return std::make_pair(price, Quantity(quantity));
} }
return std::nullopt;
};
if (auto const bid_it = engine_.bids_.find(symbol_);
bid_it != engine_.bids_.end()) {
auto const& [_, bid_map] = *bid_it;
info.bids = make_top_for(bid_map);
} }
if (auto const ask_it = engine_.asks_.find(symbol_); if (auto const ask_it = engine_.asks_.find(symbol_);
ask_it != engine_.asks_.end()) { ask_it != engine_.asks_.end()) {
auto const& [_, ask_map] = *ask_it; auto const& [_, ask_map] = *ask_it;
info.asks = make_top_for(ask_map);
if (ask_map.size() > 0) {
auto const price = ask_map.begin()->first;
int quantity = 0;
auto const [begin, end] = ask_map.equal_range(price);
for (auto it = begin; it != end; ++it) {
quantity += int(it->second.quantity);
}
info.asks = {price, Quantity(quantity)};
}
} }
return info; return info;