ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
order_book.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <cstdint>
15#include <functional>
16#include <list>
17#include <map>
18#include <memory>
19#include <ostream>
20#include <set>
21#include <variant>
22
23#include "itch/messages.hpp"
24
25namespace itch {
26
27struct PriceLevel;
28
35struct Order {
38 uint32_t shares;
39 uint32_t price;
41 std::string stock;
42
50 Order(uint64_t ref_num, char side, uint32_t shrs, uint32_t prc, const std::string& stk)
51 : order_reference_number {ref_num},
53 shares {shrs},
54 price {prc},
55 level {nullptr},
56 stock {stk} {}
57};
58
60using OrderIt = std::list<std::shared_ptr<Order>>::iterator;
61
67struct PriceLevel {
68 uint32_t total_shares {0};
69 std::list<std::shared_ptr<Order>> orders;
70
73 auto add_order(const std::shared_ptr<Order>& order) -> void;
74
77 auto remove_order(const OrderIt& order_it) -> void;
78};
79
91 public:
96 LimitOrderBook(const std::string& stock_symbol) : m_stock_symbol(stock_symbol) {}
103 auto process(const Message& message) -> void;
104
113 auto print(std::ostream& out, unsigned int delay_ms = 0) const -> void;
114
115 // Type aliases for the bid and ask maps (sorted containers).
116 using BidMap = std::map<uint32_t, PriceLevel, std::greater<uint32_t>>;
117 using AskMap = std::map<uint32_t, PriceLevel, std::less<uint32_t>>;
118
122 auto get_bids() const -> const BidMap& { return m_bids; }
123
127 auto get_asks() const -> const AskMap& { return m_asks; }
128
130 const std::set<char> book_messages {'A', 'F', 'E', 'C', 'X', 'D', 'U'};
131
132 private:
133 std::string m_stock_symbol;
134 BidMap m_bids;
135 AskMap m_asks;
136 std::map<uint64_t, OrderIt> m_orders;
137
138 // Message Handlers
143 auto handle_message(const AddOrderMessage& msg) -> void;
144
149 auto handle_message(const AddOrderMPIDAttributionMessage& msg) -> void;
150
155 auto handle_message(const OrderExecutedMessage& msg) -> void;
156
161 auto handle_message(const OrderExecutedWithPriceMessage& msg) -> void;
162
167 auto handle_message(const OrderCancelMessage& msg) -> void;
168
173 auto handle_message(const OrderDeleteMessage& msg) -> void;
174
180 auto handle_message(const OrderReplaceMessage& msg) -> void;
181
186 template <typename T>
187 auto handle_message(const T& /* msg */) -> void { /* No-op for irrelevant messages */ }
188
196 auto add_order(
197 uint64_t order_ref, char side, uint32_t shares, uint32_t price, const std::string& stock
198 ) -> void;
199
204 auto remove_order(uint64_t order_ref, uint32_t canceled_shares) -> void;
205};
206
207} // namespace itch
Manages the state of a limit order book for a specific financial instrument.
LimitOrderBook(const std::string &stock_symbol)
Constructs an order book scoped to a single stock symbol.
const std::set< char > book_messages
Set of message type characters that affect the book state.
auto get_bids() const -> const BidMap &
The map of active Bids.
std::map< uint32_t, PriceLevel, std::greater< uint32_t > > BidMap
auto print(std::ostream &out, unsigned int delay_ms=0) const -> void
Visualizes the current state of the order book to an output stream.
auto get_asks() const -> const AskMap &
The map of active Asks.
std::map< uint32_t, PriceLevel, std::less< uint32_t > > AskMap
auto process(const Message &message) -> void
Dispatches and processes a generic ITCH message.
std::string price
Definition csv_sink.cpp:23
char side
Definition csv_sink.cpp:21
std::string shares
Definition csv_sink.cpp:22
Defines every ITCH 5.0 message struct, the Message variant, and the stream-printing helpers used to f...
std::list< std::shared_ptr< Order > >::iterator OrderIt
Iterator type for navigating the list of orders within a price level.
std::variant< SystemEventMessage, StockDirectoryMessage, StockTradingActionMessage, RegSHOMessage, MarketParticipantPositionMessage, MWCBDeclineLevelMessage, MWCBStatusMessage, IPOQuotingPeriodUpdateMessage, LULDAuctionCollarMessage, OperationalHaltMessage, AddOrderMessage, AddOrderMPIDAttributionMessage, OrderExecutedMessage, OrderExecutedWithPriceMessage, OrderCancelMessage, OrderDeleteMessage, OrderReplaceMessage, NonCrossTradeMessage, CrossTradeMessage, BrokenTradeMessage, NOIIMessage, RetailPriceImprovementIndicatorMessage, DLCRMessage > Message
A variant able to hold any one of the ITCH 5.0 message structs.
Definition messages.hpp:483
Add Order, With MPID Attribution (F): like Add Order, but attributed to a market participant.
Definition messages.hpp:229
Add Order, No MPID Attribution (A): a new displayable order has been accepted and placed on the book.
Definition messages.hpp:211
Order Cancel (X): a partial cancellation reduced the shares of a resting order.
Definition messages.hpp:285
Order Delete (D): a resting order was cancelled in its entirety and removed from the book.
Definition messages.hpp:299
Order Executed (E): a resting order was executed in whole or in part at its display price.
Definition messages.hpp:249
Order Executed With Price (C): a resting order was executed at a price different from its display pri...
Definition messages.hpp:266
Order Replace (U): a resting order was replaced with a new order at a new reference number,...
Definition messages.hpp:314
Represents a single resting order within the Limit Order Book.
PriceLevel * level
Pointer to the price level containing this order.
char buy_sell_indicator
'B' for Buy, 'S' for Sell.
uint32_t price
Limit price of the order.
uint32_t shares
Current quantity of shares available in this order.
std::string stock
The stock symbol for this order.
Order(uint64_t ref_num, char side, uint32_t shrs, uint32_t prc, const std::string &stk)
Constructs an order with the given identity, side, quantity, and price.
uint64_t order_reference_number
Unique identifier assigned to the order by the exchange.
Represents a specific price node in the order book.
uint32_t total_shares
Aggregate volume of shares at this price.
auto add_order(const std::shared_ptr< Order > &order) -> void
Appends an order to the end of the queue (Time priority).
std::list< std::shared_ptr< Order > > orders
FIFO queue of orders.
auto remove_order(const OrderIt &order_it) -> void
Removes a specific order from the queue.