Files
spark/src/lib/md/CCApiMarketDepthAdaptor.cpp
T

78 lines
2.4 KiB
C++

#ifdef SPARK_ENABLE_CCAPI
#include "CCApiMarketDepthAdaptor.h"
#include "lib/domain/BUY_SELL.h"
#include "lib/domain/Book.h"
#include "lib/md/CCApiCorrelationID.h"
#include "lib/md/CCApiDepthWalker.h"
#include "lib/md/ICCApiMessageHandler.h"
#include <ccapi_cpp/ccapi_event.h>
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <ccapi_cpp/ccapi_session.h>
#include <vector>
namespace bts::md {
static bool parse(const ccapi::Message &msg, domain::Book &book) {
if (msg.getType() != ccapi::Message::Type::MARKET_DATA_EVENTS_MARKET_DEPTH) {
return false;
}
// The market data arrives in the following order:
// * bid levels (by price descending)
// * ask levels (by price ascending)
CCApiDepthWalker bid_walker{
CCAPI_BEST_BID_N_PRICE, CCAPI_BEST_BID_N_SIZE, &book[BUY]};
CCApiDepthWalker ask_walker{
CCAPI_BEST_ASK_N_PRICE, CCAPI_BEST_ASK_N_SIZE, &book[SELL]};
const auto &levels = msg.getElementList();
for (const auto &level : levels) {
// Each level has 2 entries, either:
// ["BID_PRICE", "BID_SIZE"], or
// ["ASK_PRICE", "ASK_SIZE"]
const auto &props = level.getNameValueMap();
for (const auto &[name, value] : props) {
bid_walker.parse_field(name, value) ||
ask_walker.parse_field(name, value);
}
}
// validate the book
bts_assert(bid_walker.price_idx == bid_walker.qty_idx);
bts_assert(ask_walker.price_idx == ask_walker.qty_idx);
bts_assert(book.is_sorted());
bts_assert(book[BUY].count_populated() == bid_walker.price_idx);
bts_assert(book[SELL].count_populated() == ask_walker.price_idx);
return true;
}
static domain::Book parse(const ccapi::Message &msg) {
domain::Book book;
parse(msg, book);
return book;
}
CCApiMarketDepthAdaptor::CCApiMarketDepthAdaptor(
domain::SecurityID security_id, IMarketDataListener *listener)
: security_id_(security_id), listener_(listener) {}
const domain::Book &CCApiMarketDepthAdaptor::last_book() const {
return last_book_;
}
void CCApiMarketDepthAdaptor::on_messages(
const ccapi::Event & /*event*/,
ccapi::Session * /*session*/,
const std::vector<const ccapi::Message *> &msgs,
const CCApiCorrelationID & /*corr_id*/) {
for (const auto *msg : msgs) {
auto book = parse(*msg);
book.set_security_id(security_id_);
listener_->on_book(book);
}
}
} // namespace bts::md
#endif