Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
shared_top_of_book.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <atomic>
5#include <cstddef>
6#include <deque>
7#include <limits>
8
9namespace slick::trader {
10
11// One instrument's best bid and offer, as a broker's market data worker last built
12// them, for its order worker to read. A post-only order is checked against it when
13// the exchange has no post-only instruction of its own.
14//
15// Each side is one atomic: the market data worker stores, the order worker loads,
16// neither waits. An order looks at the one side it could cross, so the two need not be
17// consistent with each other. Its own cache line, so the market data worker writing
18// one instrument does not take the line the order worker is reading another from.
19struct alignas(64) SharedTopOfBook {
20 // The book is not in sequence: being built, or rebuilt after a gap.
21 static constexpr price_t UNKNOWN = std::numeric_limits<price_t>::min();
22 // The side has no orders.
23 static constexpr price_t EMPTY = NULL_PRICE;
24
25 std::atomic<price_t> bid{UNKNOWN};
26 std::atomic<price_t> ask{UNKNOWN};
27};
28
29// Every instrument's SharedTopOfBook. An entry never moves once allocated, so a pointer
30// handed to another thread stays good for as long as the store lives. allocate() is for
31// one thread alone: whichever learns the instruments.
33public:
34 SharedTopOfBook* allocate() { return &tops_.emplace_back(); }
35 [[nodiscard]] size_t size() const noexcept { return tops_.size(); }
36
37private:
38 std::deque<SharedTopOfBook> tops_;
39};
40
41} // namespace slick::trader
constexpr price_t NULL_PRICE
Definition types.hpp:19
int64_t price_t
Definition types.hpp:16
static constexpr price_t UNKNOWN