Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
level_book.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <cstdint>
5#include <limits>
6#include <unordered_map>
7#include <utility>
8#include <vector>
9
10namespace slick::trader {
11
12// One instrument's price levels, aggregated from order-by-order updates in the
13// exchange's own wire units, remembering which levels changed since the last flush.
14// Converting to price_t only when a level is published keeps the per-update cost to
15// two hash lookups.
16//
17// Each update subtracts what the order was (`prev_*`) and adds what it is, so the view
18// needs no order map of its own. A level stays in the map with quantity 0 until it is
19// flushed, so its removal is reported once and then forgotten.
20//
21// The best price of each side is kept as the levels change. Only a best level
22// emptying costs a look over the side's levels, and only when the best price is next
23// asked for.
24class LevelBook {
25public:
26 // What best() returns for a side with no orders.
27 static constexpr int64_t NO_PRICE = std::numeric_limits<int64_t>::max();
28
29 struct Level {
30 int64_t qty = 0;
31 uint32_t orders = 0;
32 bool dirty = false;
33 };
34
35 // A level as reported: its price and what now rests there (0 = gone).
36 struct LevelChange {
38 int64_t price;
39 int64_t qty;
40 uint32_t orders;
41 };
42
43 // An order was `prev_qty` at `prev_price` on `prev_side` and is now `qty` at `price`
44 // on `side`. A quantity of 0 means "not on the book".
45 void apply(Side prev_side, int64_t prev_price, int64_t prev_qty, Side side, int64_t price, int64_t qty) {
46 if (prev_qty > 0) {
47 adjust(prev_side, prev_price, -prev_qty, -1);
48 }
49 if (qty > 0) {
50 adjust(side, price, qty, +1);
51 }
52 }
53
54 [[nodiscard]] bool dirty() const noexcept { return !dirty_.empty(); }
55
56 // The side's best price (the highest bid, the lowest offer), or NO_PRICE.
57 [[nodiscard]] int64_t best(Side side) noexcept {
58 Best& best = side == Side::BUY ? best_bid_ : best_ask_;
59 if (best.stale) {
60 best.stale = false;
61 best.price = NO_PRICE;
62 for (const auto& [price, level] : side == Side::BUY ? bids_ : asks_) {
63 if (level.qty > 0 && (best.price == NO_PRICE || better(side, price, best.price))) {
64 best.price = price;
65 }
66 }
67 }
68 return best.price;
69 }
70
71 // Calls `f(LevelChange)` for each level changed since the last flush, then forgets
72 // the levels that emptied.
73 template <typename F>
74 void flush(F&& f) {
75 for (const auto& [side, price] : dirty_) {
76 auto& levels = side == Side::BUY ? bids_ : asks_;
77 auto it = levels.find(price);
78 if (it == levels.end()) {
79 continue;
80 }
81 f(LevelChange{side, price, it->second.qty, it->second.orders});
82 if (it->second.qty <= 0) {
83 levels.erase(it);
84 } else {
85 it->second.dirty = false;
86 }
87 }
88 dirty_.clear();
89 }
90
91 // Calls `f(LevelChange)` for every non-empty level: a snapshot.
92 template <typename F>
93 void for_each_level(F&& f) const {
94 for (const auto& [price, level] : bids_) {
95 if (level.qty > 0) {
96 f(LevelChange{Side::BUY, price, level.qty, level.orders});
97 }
98 }
99 for (const auto& [price, level] : asks_) {
100 if (level.qty > 0) {
101 f(LevelChange{Side::SELL, price, level.qty, level.orders});
102 }
103 }
104 }
105
106 [[nodiscard]] size_t level_count() const noexcept { return bids_.size() + asks_.size(); }
107
108private:
109 struct Best {
110 int64_t price = NO_PRICE;
111 bool stale = false; // the best level emptied: the side has to be looked over
112 };
113
114 static bool better(Side side, int64_t price, int64_t than) noexcept {
115 return side == Side::BUY ? price > than : price < than;
116 }
117
118 void adjust(Side side, int64_t price, int64_t qty_delta, int32_t order_delta) {
119 auto& level = (side == Side::BUY ? bids_ : asks_)[price];
120 level.qty += qty_delta;
121 level.orders = static_cast<uint32_t>(static_cast<int64_t>(level.orders) + order_delta);
122 if (!level.dirty) {
123 level.dirty = true;
124 dirty_.emplace_back(side, price);
125 }
126 Best& best = side == Side::BUY ? best_bid_ : best_ask_;
127 if (level.qty <= 0) {
128 best.stale |= price == best.price;
129 } else if (!best.stale && (best.price == NO_PRICE || better(side, price, best.price))) {
130 best.price = price;
131 }
132 }
133
134 std::unordered_map<int64_t, Level> bids_;
135 std::unordered_map<int64_t, Level> asks_;
136 std::vector<std::pair<Side, int64_t>> dirty_;
137 Best best_bid_;
138 Best best_ask_;
139};
140
141} // namespace slick::trader
void apply(Side prev_side, int64_t prev_price, int64_t prev_qty, Side side, int64_t price, int64_t qty)
size_t level_count() const noexcept
void for_each_level(F &&f) const
static constexpr int64_t NO_PRICE
int64_t best(Side side) noexcept
bool dirty() const noexcept