order_book 0.3.0
Exchange feed order book that prints the best bid and offer
Loading...
Searching...
No Matches
order_book.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <expected>
6#include <functional>
7#include <string>
8#include <string_view>
9
10#include <ankerl/unordered_dense.h>
11
12#include <order_book/feed.hpp>
13#include <order_book/levels.hpp>
14
15namespace order_book {
16
18struct StringHash {
19 using is_transparent = void;
20 using is_avalanching = void;
22 [[nodiscard]] std::uint64_t operator()(std::string_view text) const noexcept
23 {
24 return ankerl::unordered_dense::hash<std::string_view>{}(text);
25 }
26};
27
29enum class BookError : std::uint8_t {
30 DuplicateOrder,
31 UnknownOrder,
32};
33
42class OrderBook {
43public:
47 [[nodiscard]] std::expected<bool, BookError> apply(std::string_view order, const Event& event);
49 [[nodiscard]] Bbo bbo() const;
51 [[nodiscard]] std::size_t order_count() const { return m_orders.size(); }
53 [[nodiscard]] std::size_t level_count() const { return m_levels.size(); }
54
55private:
56 struct Order {
57 Shares remaining;
58 std::uint32_t level;
59 };
60
61 std::expected<bool, BookError> add(std::string_view order, const AddOrder& add);
62
63 BookLevels m_book;
64 LevelPool m_levels;
65 ankerl::unordered_dense::map<std::string, Order, StringHash, std::equal_to<>> m_orders;
66};
67
68} // namespace order_book
std::size_t order_count() const
Number of resting orders on both sides.
std::expected< bool, BookError > apply(std::string_view order, const Event &event)
std::size_t level_count() const
Number of price levels on both sides.
Bbo bbo() const
Current best bid and offer.
Best bid and offer; an empty side has no level.
Definition levels.hpp:21
Hash that lets string-keyed unordered containers look up a std::string_view without a copy.
std::uint64_t operator()(std::string_view text) const noexcept
Hash of the characters, equal for a std::string and a view of it.
void is_transparent
Enables heterogeneous lookup.