72 if (!instruments_known_) {
73 pending_.emplace_back(std::string(symbol), type);
76 auto it = by_symbol_.find(symbol);
77 if (it == by_symbol_.end()) {
80 apply_subscription(states_[it->second], type);
85 std::erase_if(pending_, [&](
const auto& p) {
return p.first == symbol && p.second == type; });
86 auto it = by_symbol_.find(symbol);
87 if (it == by_symbol_.end()) {
90 auto& state = states_[it->second];
91 subscribed(state, type) =
false;
93 state.order_updates.clear();
97 [[nodiscard]]
const Instrument*
find(std::string_view symbol)
const {
98 auto it = by_symbol_.find(symbol);
99 return it == by_symbol_.end() ? nullptr : &states_.at(it->second).instrument;
127 by_symbol_.insert_or_assign(instrument.symbol, key);
128 auto& state = states_[key];
136 instruments_known_ =
true;
137 auto pending = std::move(pending_);
139 for (
const auto& [symbol, type] : pending) {
140 auto it = by_symbol_.find(symbol);
141 if (it != by_symbol_.end()) {
142 apply_subscription(states_[it->second], type);
148 auto it = states_.find(key);
149 return it == states_.end() ? nullptr : &it->second;
151 [[nodiscard]] std::unordered_map<Key, InstrumentState>&
states() noexcept {
return states_; }
152 [[nodiscard]] Sink&
sink() noexcept {
return sink_; }
153 [[nodiscard]]
const std::string&
who() const noexcept {
return who_; }
162 auto it = states_.find(key);
163 if (it == states_.end()) {
166 auto& state = it->second;
171 if (publish_order && state.orders && derived().live()) {
172 append_order(state.order_updates, state.instrument, u);
174 if (!state.touched) {
175 state.touched =
true;
176 touched_.push_back(key);
181 void trade(Key key, int64_t wire_price, int64_t lots,
Side aggressor, uint64_t event_time) {
182 auto it = states_.find(key);
183 if (it == states_.end() || !it->second.trades) {
186 const auto& instrument = it->second.instrument;
188 if (!instrument.scale.to_price(wire_price, price)) {
189 LOG_WARN(
"{}: trade price {} of {} is off its scale - dropped", who_, wire_price, instrument.symbol);
192 sink_.publish_trade(instrument, price,
lots_to_qty(lots), aggressor, event_time);
197 const bool in_sequence = derived().live();
198 for (
const Key key : touched_) {
199 auto& state = states_[key];
200 state.touched =
false;
203 if (in_sequence && state.instrument.top) {
206 if (!state.order_updates.empty()) {
207 sink_.publish_orders(state.instrument, state.order_updates,
false, state.last_time, ++order_sequence_);
208 state.order_updates.clear();
217 const auto level = to_level(state.instrument, change, ok);
219 levels_.push_back(level);
222 if (!levels_.empty()) {
223 sink_.publish_levels(state.instrument, levels_,
false, state.last_time, ++level_sequence_);
231 uint64_t event_time) {
237 sink_.publish_status(state.
instrument, status, cancel_allowed, reason, event_time);
243 for (
auto& [key, state] : states_) {
244 if (state.instrument.top) {
253 for (
auto& [key, state] : states_) {
255 state.touched =
false;
256 state.order_updates.clear();
266 for (
auto& [key, state] : states_) {
267 if (state.instrument.top) {
271 publish_snapshot(state);
274 publish_order_snapshot(state);
280 [[nodiscard]] Derived& derived() noexcept {
return static_cast<Derived&
>(*this); }
281 [[nodiscard]]
const Derived& derived() const noexcept {
return static_cast<const Derived&
>(*this); }
284 static bool& subscribed(InstrumentState& state,
SubscriptionType type)
noexcept {
296 bool& flag = subscribed(state, type);
303 if (derived().live()) {
304 if (&flag == &state.levels) {
305 publish_snapshot(state);
306 }
else if (&flag == &state.orders) {
307 publish_order_snapshot(state);
312 static PriceLevel to_level(
const Instrument& instrument,
const LevelBook::LevelChange& change,
bool& ok) {
315 ok = instrument.scale.to_price(change.price, price);
318 level.quantity =
lots_to_qty(change.qty > 0 ? change.qty : 0);
319 level.num_orders = change.qty > 0 ? change.orders : 0;
320 level.side = change.side;
325 static void append_order(std::vector<MboOrder>& out,
const Instrument& instrument,
const BookOrderChange& u) {
333 if (!instrument.scale.to_price(u.price, price)) {
343 const bool same_side = u.prev_side == u.side;
344 if (changed && !same_side) {
347 out.push_back(MboOrder{u.order_id, u.priority, price,
lots_to_qty(u.qty), u.side,
353 state.book.for_each_level([&](
const LevelBook::LevelChange& change) {
355 const auto level = to_level(state.instrument, change, ok);
357 levels_.push_back(level);
360 sink_.publish_levels(state.instrument, levels_,
true, state.last_time, ++level_sequence_);
365 derived().for_each_resting_order(state.key, [&](
const BookOrderChange& order) {
366 append_order(orders_, state.instrument, order);
369 state.order_updates.clear();
370 sink_.publish_orders(state.instrument, orders_,
true, state.last_time, ++order_sequence_);
375 SharedTopOfBook& top = *state.instrument.top;
376 if (bid != state.top_bid) {
378 top.bid.store(bid, std::memory_order_release);
380 if (ask != state.top_ask) {
382 top.ask.store(ask, std::memory_order_release);
388 const auto best = [&](
Side side) {
389 const int64_t wire = state.book.best(side);
402 bool instruments_known_ =
false;
403 std::unordered_map<Key, InstrumentState> states_;
404 std::unordered_map<std::string, Key, StringHash, std::equal_to<>> by_symbol_;
406 std::vector<std::pair<std::string, SubscriptionType>> pending_;
407 std::vector<Key> touched_;
408 std::vector<PriceLevel> levels_;
409 std::vector<MboOrder> orders_;
410 uint64_t level_sequence_ = 0;
411 uint64_t order_sequence_ = 0;