Files
spark/src/lib/md/binance/BinanceDepthSnapshot.cpp
T

130 lines
3.8 KiB
C++

#ifdef SPARK_ENABLE_CCAPI
#include "BinanceDepthSnapshot.h"
#include "BinanceBookLevelComp.h"
#include "BinanceDiffDepthUpdate.h"
#include "lib/domain/Price.h"
#include "lib/md/binance/BinanceBookLevel.h"
#include <algorithm>
namespace bts::md::binance {
static void apply_diff(
const std::vector<BinanceBookLevel> &changes,
std::vector<BinanceBookLevel> &book_side,
BinanceBookLevelComp comp) {
for (const auto &ch : changes) {
auto it =
std::lower_bound(book_side.begin(), book_side.end(), ch.price, comp);
if (ch.quantity == 0) {
// remove
if (it != book_side.end() && it->price == ch.price) {
book_side.erase(it);
}
continue;
}
if (it->price == ch.price) {
// update
it->quantity = ch.quantity;
continue;
}
auto new_level = book_side.emplace(it);
new_level->price = ch.price;
new_level->quantity = ch.quantity;
}
}
BinanceDepthSnapshot BinanceDepthSnapshot::apply_diff(
const BinanceDiffDepthUpdate &diff_update) const {
auto bids_out = bids;
binance::apply_diff(
diff_update.changed_bids, bids_out,
BinanceBookLevelComp(domain::Side::Buy));
auto asks_out = asks;
binance::apply_diff(
diff_update.changed_asks, asks_out,
BinanceBookLevelComp(domain::Side::Sell));
BinanceDepthSnapshot rv;
rv.last_update_id = last_update_id;
rv.bids = bids_out;
rv.asks = asks_out;
return rv;
}
BinanceDiffDepthUpdate BinanceDepthSnapshot::as_initial_update() const {
BinanceDiffDepthUpdate rv;
rv.first_update_id = this->last_update_id;
rv.last_update_id = this->last_update_id;
rv.changed_bids = bids;
rv.changed_asks = asks;
return rv;
}
void from_json(const nlohmann::json &j, BinanceDepthSnapshot &snap) {
j["lastUpdateId"].get_to(snap.last_update_id);
j["bids"].get_to(snap.bids);
j["asks"].get_to(snap.asks);
}
void to_json(nlohmann::json &j, const BinanceDepthSnapshot &snap) {
j["lastUpdateId"] = snap.last_update_id;
j["bids"] = snap.bids;
j["asks"] = snap.asks;
}
struct ChangedLevelsIter {
const std::vector<BinanceBookLevel> *minuend_levels;
const std::vector<BinanceBookLevel> *subtrahend_levels;
std::size_t i = 0;
std::size_t j = 0;
const BinanceBookLevel *next() {
if (i >= minuend_levels->size()) {
return nullptr;
}
const auto &mlevel = (*minuend_levels)[i];
if (j >= subtrahend_levels->size()) {
return &mlevel;
}
const auto &slevel = (*subtrahend_levels)[i];
if (mlevel != slevel) {
return nullptr;
}
return &mlevel;
}
bool is_done() const { return i >= minuend_levels->size(); }
};
static std::vector<BinanceBookLevel> get_changed_levels(
const std::vector<BinanceBookLevel> &old_levels,
const std::vector<BinanceBookLevel> &new_levels) {
std::vector<BinanceBookLevel> changed_levels;
ChangedLevelsIter diff_iter{&new_levels, &old_levels};
const auto *level = diff_iter.next();
while (level != nullptr) {
changed_levels.push_back(*level);
level = diff_iter.next();
}
return changed_levels;
}
BinanceDiffDepthUpdate operator-(
const BinanceDepthSnapshot &minuend,
const BinanceDepthSnapshot &subtrahend) {
BinanceDiffDepthUpdate diff;
diff.first_update_id = subtrahend.last_update_id;
diff.last_update_id = minuend.last_update_id;
diff.changed_bids = get_changed_levels(subtrahend.bids, minuend.bids);
diff.changed_asks = get_changed_levels(subtrahend.asks, minuend.asks);
return diff;
}
std::ostream &operator<<(
std::ostream &out, const BinanceDepthSnapshot &snapshot) noexcept {
out << "bids: " << fmt::format("{}", fmt::join(snapshot.bids, ", "));
return out;
}
} // namespace bts::md::binance
#endif