150 lines
4.8 KiB
C++
150 lines
4.8 KiB
C++
#ifdef SPARK_ENABLE_CCAPI
|
|
#include "BinanceDiffDepthUpdate.h"
|
|
#include "BinanceBookLevelComp.h"
|
|
#include "BinanceDepthSnapshot.h"
|
|
#include "lib/md/binance/BinanceBookLevel.h"
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
|
|
|
namespace bts::md::binance {
|
|
|
|
static void merge_side(
|
|
std::vector<BinanceBookLevel> &target,
|
|
const std::vector<BinanceBookLevel> &changes,
|
|
domain::Side side) {
|
|
BinanceBookLevelComp comp(side);
|
|
// Don't assume book level changes are sorted (they should be sorted, but
|
|
// the code is more resilient this way).
|
|
for (const auto &ch : changes) {
|
|
auto it = std::lower_bound(target.begin(), target.end(), ch.price, comp);
|
|
if (it == target.end() || it->price != ch.price) {
|
|
// Price doesn't match that of an existing level, so it cannot be a
|
|
// 'remove' op.
|
|
bts_assert(it->quantity > 0);
|
|
target.insert(it, ch);
|
|
continue;
|
|
}
|
|
// Price does match an existing level. If quantity is 0, then this is a
|
|
// 'remove' op, otherwise it's an 'update.'
|
|
if (ch.quantity == 0) {
|
|
target.erase(it);
|
|
continue;
|
|
}
|
|
it->quantity = ch.quantity;
|
|
}
|
|
}
|
|
|
|
static void apply_changes(
|
|
domain::Side side,
|
|
domain::BookSide &bs,
|
|
const std::vector<BinanceBookLevel> &changes) {
|
|
auto cursor = bs.begin();
|
|
auto it = changes.begin();
|
|
domain::BookLevelComp cmp(side);
|
|
for (; it != changes.end(); it++) {
|
|
// Create price instance from it->price to support cases where
|
|
// it->price is not of type `Price` (e.g., if it is a double). Treat
|
|
// it->quantity in the same manner.
|
|
domain::Price ch_price(it->price);
|
|
cursor = std::lower_bound(cursor, bs.end(), ch_price, cmp);
|
|
if (it->quantity == 0) {
|
|
if (cursor != bs.end() && cursor->price == ch_price) {
|
|
bs.erase(cursor);
|
|
}
|
|
continue;
|
|
}
|
|
domain::Quantity ch_quantity(it->quantity);
|
|
if (cursor->price == ch_price) {
|
|
cursor->total_quantity = ch_quantity;
|
|
cursor->direct_quantity = ch_quantity;
|
|
} else {
|
|
bs.insert(
|
|
cursor, domain::BookLevel{ch_price, ch_quantity, ch_quantity, 0});
|
|
}
|
|
}
|
|
}
|
|
|
|
static void strip_side_non_changes(
|
|
const std::vector<BinanceBookLevel> &origin,
|
|
std::vector<BinanceBookLevel> &changed_levels,
|
|
domain::Side side) {
|
|
BinanceBookLevelComp comp(side);
|
|
if (changed_levels.empty()) {
|
|
return;
|
|
}
|
|
auto it = changed_levels.begin();
|
|
for (const auto &bl : origin) {
|
|
it = std::lower_bound(it, changed_levels.end(), bl.price, comp);
|
|
if (it == changed_levels.end()) {
|
|
return;
|
|
}
|
|
it = *it == bl ? changed_levels.erase(it) : it + 1;
|
|
}
|
|
}
|
|
|
|
bool BinanceDiffDepthUpdate::has_changes() const {
|
|
return !changed_bids.empty() || !changed_asks.empty();
|
|
}
|
|
|
|
void BinanceDiffDepthUpdate::merge(const BinanceDiffDepthUpdate &update) {
|
|
update.validate();
|
|
bts_assert(update.first_update_id >= last_update_id);
|
|
merge_side(changed_bids, update.changed_bids, domain::Side::Buy);
|
|
merge_side(changed_asks, update.changed_asks, domain::Side::Sell);
|
|
}
|
|
|
|
bool BinanceDiffDepthUpdate::is_before_event(std::size_t event_id) const {
|
|
return first_update_id <= event_id;
|
|
}
|
|
|
|
bool BinanceDiffDepthUpdate::is_after_event(std::size_t event_id) const {
|
|
return last_update_id > event_id;
|
|
}
|
|
|
|
bool BinanceDiffDepthUpdate::includes_event(std::size_t event_id) const {
|
|
return is_before_event(event_id + 1) && is_after_event(event_id);
|
|
}
|
|
|
|
void BinanceDiffDepthUpdate::apply_to_book(domain::Book &book) const {
|
|
apply_changes(domain::Side::Buy, book[domain::Side::Buy], changed_bids);
|
|
apply_changes(domain::Side::Sell, book[domain::Side::Sell], changed_asks);
|
|
}
|
|
|
|
void BinanceDiffDepthUpdate::strip_non_changes(
|
|
const BinanceDepthSnapshot &origin) {
|
|
strip_side_non_changes(origin.bids, changed_bids, domain::Side::Buy);
|
|
strip_side_non_changes(origin.asks, changed_asks, domain::Side::Sell);
|
|
}
|
|
|
|
void BinanceDiffDepthUpdate::validate() const {
|
|
bts_assert(last_update_id >= first_update_id);
|
|
}
|
|
|
|
void from_json(const nlohmann::json &j, BinanceDiffDepthUpdate &upd) {
|
|
j["e"].get_to(upd.event_type);
|
|
j["E"].get_to(upd.event_time);
|
|
j["s"].get_to(upd.symbol);
|
|
j["U"].get_to(upd.first_update_id);
|
|
j["u"].get_to(upd.last_update_id);
|
|
j["b"].get_to(upd.changed_bids);
|
|
j["a"].get_to(upd.changed_asks);
|
|
}
|
|
|
|
void to_json(nlohmann::json &j, const BinanceDiffDepthUpdate &upd) {
|
|
j["e"] = upd.event_type;
|
|
j["E"] = upd.event_time;
|
|
j["s"] = upd.symbol;
|
|
j["U"] = upd.first_update_id;
|
|
j["u"] = upd.last_update_id;
|
|
j["b"] = upd.changed_bids;
|
|
j["a"] = upd.changed_asks;
|
|
}
|
|
|
|
BinanceDiffDepthUpdate &operator+=(
|
|
BinanceDiffDepthUpdate &augend, const BinanceDiffDepthUpdate &addend) {
|
|
augend.merge(addend);
|
|
return augend;
|
|
}
|
|
|
|
} // namespace bts::md::binance
|
|
#endif
|