Also moved a few logging deps from lib_spark -> system: * bts_chrono.h * bts_no_copy.h * defer.h * dispatcher.h/cpp * file_utils.h/cpp * type_name.h
77 lines
2.8 KiB
C++
77 lines
2.8 KiB
C++
#include "MarketDataAuxiliaryServiceServer.h"
|
|
#include "VolumeAtPriceState.h"
|
|
#include "logging/logging.h"
|
|
#include "spark/api/public/ServiceContext.h"
|
|
#include "spark/api/services/MarketDataRelay.client.pb.h"
|
|
#include "spark/api/services/MarketDataRelay.pb.h"
|
|
#include "spark/config/config.h"
|
|
#include "spark/config/protos/toggles.pb.h"
|
|
#include "spark/utils/Dispatcher.h"
|
|
#include "spark/utils/PBUtils.h"
|
|
#include "spark/utils/bts_chrono.h"
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
namespace auxiliary {
|
|
|
|
using namespace bts::co2;
|
|
using namespace bts::api::md::auxiliary;
|
|
|
|
struct MarketDataAuxiliaryServiceServer::PImpl {
|
|
bool vap_enabled;
|
|
bts::api::ServiceContext &context;
|
|
MarketDataRelay relay_service;
|
|
MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher publisher;
|
|
std::unique_ptr<utils::DispatcherTimer> trade_timer;
|
|
int64_t poll_period_ms;
|
|
VolumeAtPriceState state;
|
|
uint16_t log_counter, log_trunc;
|
|
PImpl(
|
|
bts::api::ServiceContext &context,
|
|
const MarketDataAuxiliaryServiceBase::Volume_at_priceStatefulPublisher
|
|
&publisher)
|
|
: vap_enabled(config::get<config::toggles::VolumeAtPrice>().enabled()),
|
|
context(context), relay_service(context.locate<MarketDataRelay>()),
|
|
publisher(publisher), trade_timer(), poll_period_ms(100), state(),
|
|
log_counter(0), log_trunc(300) {}
|
|
};
|
|
|
|
MarketDataAuxiliaryServiceServer::MarketDataAuxiliaryServiceServer(
|
|
bts::api::ServiceContext &context)
|
|
: MarketDataAuxiliaryServiceBase(context),
|
|
me(new PImpl(context, make_volume_at_price_stateful_publisher())) {
|
|
if (me->vap_enabled)
|
|
me->context.post([this]() { _poll_md(); }, me->poll_period_ms);
|
|
}
|
|
|
|
void MarketDataAuxiliaryServiceServer::_poll_md() {
|
|
me->relay_service.requestmd(
|
|
me->state.md_req(), [this](const bts::api::Result<MDReply> &md_reply) {
|
|
if (md_reply)
|
|
_scan_trades(*md_reply.value());
|
|
me->context.post([this]() { _poll_md(); }, me->poll_period_ms);
|
|
});
|
|
}
|
|
|
|
void MarketDataAuxiliaryServiceServer::_scan_trades(const MDReply &md_reply) {
|
|
uint32_t n_trades = 0, total_unique_levels = 0;
|
|
bts::TimeStamp t0 = bts::Timer::now();
|
|
for (const auto &cr : md_reply.channels())
|
|
me->state.on_new_trades(cr, me->publisher, n_trades, total_unique_levels);
|
|
bts::TimeStamp t1 = bts::Timer::now();
|
|
double usecs = static_cast<double>(us_diff(t1, t0));
|
|
if (me->log_counter++ >= me->log_trunc) {
|
|
LOG(OrderMan, Diag,
|
|
"MarketDataAuxiliaryServiceServer::_scan_trades "
|
|
"trades:{} tot_unique:{} usecs:{}",
|
|
n_trades, total_unique_levels, usecs);
|
|
me->log_counter = 0;
|
|
}
|
|
me->publisher.flush();
|
|
}
|
|
|
|
MarketDataAuxiliaryServiceServer::~MarketDataAuxiliaryServiceServer() {}
|
|
} // namespace auxiliary
|
|
} // namespace md
|
|
} // namespace bts
|