Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
symbol.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <algorithm>
7#include <array>
8#include <memory>
9#include <tuple>
10#include <unordered_map>
11#include <unordered_set>
12#include <slick/orderbook/observer.hpp>
13
14namespace slick::sim::exch {
15
16struct Symbol;
17
18struct OrderBookObserver : public orderbook::IOrderBookObserver {
20 OrderBookObserver(Symbol* symbol) : symbol_(symbol) {}
21
22 void onPriceLevelUpdate(const PriceLevelUpdate& update) override;
23
24 void onOrderUpdate(const OrderUpdate& update) override;
25};
26
35
36struct Symbol {
40 static constexpr size_t TRADE_HISTORY_CAPACITY = 64;
41
48 std::shared_ptr<OrderBook> order_book_;
49 std::atomic_uint16_t num_subscriptions_ = 0;
51 std::vector<MDLevel> md_level_update_cache_;
52 std::vector<MDOrder> md_order_update_cache_;
53 std::shared_ptr<OrderBookObserver> book_observer_;
55
61 Symbol() : book_observer_(std::make_shared<OrderBookObserver>(this)) {
62 md_level_update_cache_.reserve(256);
63 md_order_update_cache_.reserve(1024);
64 };
65 ~Symbol() = default;
66
67 Symbol(const Symbol&) = delete;
68 Symbol& operator= (const Symbol&) = delete;
69
70 Symbol(Symbol&& other) noexcept
71 : id_(std::exchange(other.id_, 0))
72 , venue_(std::exchange(other.venue_, Venue::UNKNOWN_VENUE))
73 , symbol_(std::move(other.symbol_))
74 , matching_engine_(std::exchange(other.matching_engine_, nullptr))
75 , order_book_(std::move(other.order_book_))
76 , num_subscriptions_(other.num_subscriptions_.load(std::memory_order_relaxed))
77 // Move the reserved caches too, so a moved Symbol retains their capacity.
78 // Otherwise feed processing would have to allocate it again. SymbolManager
79 // currently constructs Symbols in place, so normal use does not test this.
80 , md_level_update_cache_(std::move(other.md_level_update_cache_))
81 , md_order_update_cache_(std::move(other.md_order_update_cache_))
82 , book_observer_(std::make_shared<OrderBookObserver>(this))
83 , trade_history_(std::move(other.trade_history_))
84 , traded_qty_credit_(std::move(other.traded_qty_credit_))
85 {
86 if (order_book_) {
87 order_book_->removeObserver(other.book_observer_);
88 order_book_->addObserver(book_observer_);
89 }
90 }
92 if (this != &other) {
93 id_ = std::exchange(other.id_, 0);
94 venue_ = std::exchange(other.venue_, Venue::UNKNOWN_VENUE);
95 symbol_ = std::move(other.symbol_);
96 matching_engine_ = std::exchange(other.matching_engine_, nullptr);
97 order_book_ = std::move(other.order_book_);
98 num_subscriptions_.store(other.num_subscriptions_.load(std::memory_order_relaxed), std::memory_order_relaxed);
99 md_level_update_cache_ = std::move(other.md_level_update_cache_);
100 md_order_update_cache_ = std::move(other.md_order_update_cache_);
101 trade_history_ = std::move(other.trade_history_);
102 traded_qty_credit_ = std::move(other.traded_qty_credit_);
103 }
104 return *this;
105 }
106
116 void creditTradedQty(Side side, price_t price, qty_t phantom_consumed, time_t event_time) {
117 if (phantom_consumed <= 0 || side > Side::SELL) {
118 return;
119 }
120 auto &credits = traded_qty_credit_[static_cast<size_t>(side)][price];
121 // One credit per timestamp. A single print's sweep of one level arrives as
122 // one summary, but a batch can carry several prints stamped alike.
123 for (auto &credit : credits) {
124 if (credit.event_time == event_time) {
125 credit.qty += phantom_consumed;
126 return;
127 }
128 }
129 credits.emplace_back(phantom_consumed, event_time);
130 }
131
140 qty_t takeTradedQty(Side side, price_t price, time_t as_of) {
141 if (side > Side::SELL) {
142 return 0;
143 }
144 auto &by_price = traded_qty_credit_[static_cast<size_t>(side)];
145 auto it = by_price.find(price);
146 if (it == by_price.end()) {
147 return 0;
148 }
149
150 auto &credits = it->second;
151 qty_t unreflected = 0;
152 for (auto credit = credits.begin(); credit != credits.end();) {
153 if (as_of < credit->event_time) {
154 // The update predates this trade, so it still reports the
155 // pre-trade quantity at this price.
156 unreflected += credit->qty;
157 ++credit;
158 } else {
159 credit = credits.erase(credit);
160 }
161 }
162 if (credits.empty()) {
163 by_price.erase(it);
164 }
165 return unreflected;
166 }
167
169 void recordTrade(const MDTrade &trade) {
170 trade_history_.push(trade);
171 }
172
178
179 template<OrderBookType BookType>
181 order_book_ = std::make_shared<OrderBookImpl<BookType>>(id_, symbol_, venue_);
182 order_book_->addObserver(order_book_->shared_from_this());
183 order_book_->addObserver(book_observer_);
184 }
185
186 std::tuple<OrdRejectReason, std::vector<TradeSummaryInfo>> addOrder(Order *order, time_t request_time);
187 std::tuple<OrdRejectReason, std::vector<TradeSummaryInfo>> modifyOrder(Order* order, price_t new_price, qty_t new_qty, time_t request_time);
188 void cancelOrder(Order *order, time_t request_time);
189
190 Order* findOrderByClientOrderId(const char* client_order_id) const {
191 return order_book_->findOrderByClientOrderId(client_order_id);
192 }
193
194 Order* findOrderByOrderId(const char* order_id) const {
195 return order_book_->findOrderByOrderId(order_id);
196 }
197
198 Order* findOrder(uint64_t order_id) const {
199 return order_book_->findOrder(order_id);
200 }
201
202 void onPriceLevelUpdate(const PriceLevelUpdate& update);
203
205 void onOrderUpdate(const OrderUpdate& update);
206
207private:
210 std::array<std::unordered_map<price_t, std::vector<TradedCredit>>, 2> traded_qty_credit_;
211};
212
213
214} // namespace exchange
Helpers for a venue whose one config entry describes several independent exchange instances,...
Definition exchange.hpp:30
slick::orderbook::PriceLevelUpdate PriceLevelUpdate
uint_fast64_t time_t
Definition types.hpp:23
@ UNKNOWN_VENUE
Definition types.hpp:156
constexpr symid_t INVALID_SYMBOL_ID
Definition types.hpp:27
slick::orderbook::OrderUpdate OrderUpdate
SelfMatchPreventionMode
Definition order.hpp:233
uint16_t symid_t
Definition types.hpp:24
utils::fixed_string< sizeof(MarketDataUpdate::symbol)> symbol_name_t
An instrument name at its wire width.
int_fast64_t qty_t
Definition types.hpp:22
int_fast64_t price_t
Definition types.hpp:21
void onOrderUpdate(const OrderUpdate &update) override
Definition symbol.cpp:11
void onPriceLevelUpdate(const PriceLevelUpdate &update) override
Definition symbol.cpp:7
void creditTradedQty(Side side, price_t price, qty_t phantom_consumed, time_t event_time)
Records that a trade print consumed phantom_consumed of the phantom liquidity resting at (side,...
Definition symbol.hpp:116
utils::RingBuffer< MDTrade > trade_history_
Definition symbol.hpp:54
SelfMatchPreventionMode smp_mode_
Definition symbol.hpp:50
symbol_name_t symbol_
Stored at wire width so publishing can copy the whole field without reading past the end of a shorter...
Definition symbol.hpp:46
void cancelOrder(Order *order, time_t request_time)
Definition symbol.cpp:89
std::tuple< OrdRejectReason, std::vector< TradeSummaryInfo > > addOrder(Order *order, time_t request_time)
Definition symbol.cpp:15
Order * findOrderByOrderId(const char *order_id) const
Definition symbol.hpp:194
engine::MatchingEngine * matching_engine_
Definition symbol.hpp:47
static constexpr size_t TRADE_HISTORY_CAPACITY
Recent trades published on this instrument's tape, newest at the back.
Definition symbol.hpp:40
std::vector< MDLevel > md_level_update_cache_
Definition symbol.hpp:51
void onOrderUpdate(const OrderUpdate &update)
Called when an individual order is updated (L3 event).
Definition symbol.cpp:112
Symbol()
Both caches are filled by book observers and drained by the exchange thread, which only ever clear()s...
Definition symbol.hpp:61
Order * findOrderByClientOrderId(const char *client_order_id) const
Definition symbol.hpp:190
Symbol(const Symbol &)=delete
std::vector< MDOrder > md_order_update_cache_
Definition symbol.hpp:52
Symbol(Symbol &&other) noexcept
Definition symbol.hpp:70
void recordTrade(const MDTrade &trade)
Appends a trade to the tape history that serves the subscribe snapshot.
Definition symbol.hpp:169
std::shared_ptr< OrderBook > order_book_
Definition symbol.hpp:48
qty_t takeTradedQty(Side side, price_t price, time_t as_of)
Returns the quantity traded at (side, price) that a level update stamped as_of does not yet reflect,...
Definition symbol.hpp:140
void onPriceLevelUpdate(const PriceLevelUpdate &update)
Definition symbol.cpp:98
std::atomic_uint16_t num_subscriptions_
Definition symbol.hpp:49
void clearTradeHistory()
Drops the tape history, so a venue that is about to resend its own trade snapshot does not end up rep...
Definition symbol.hpp:175
std::tuple< OrdRejectReason, std::vector< TradeSummaryInfo > > modifyOrder(Order *order, price_t new_price, qty_t new_qty, time_t request_time)
Definition symbol.cpp:58
std::shared_ptr< OrderBookObserver > book_observer_
Definition symbol.hpp:53
Symbol & operator=(const Symbol &)=delete
Order * findOrder(uint64_t order_id) const
Definition symbol.hpp:198
Quantity one trade print removed from phantom liquidity at one price, kept until a level update stamp...
Definition symbol.hpp:31