ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
microstructure.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <cstddef>
12#include <cstdint>
13#include <limits>
14
15#include "itch/book/l3_book.hpp"
16
17namespace itch::analytics {
18
23[[nodiscard]] inline auto spread(const book::Bbo& bbo) -> double {
24 if (!bbo.has_bid || !bbo.has_ask) {
25 return std::numeric_limits<double>::quiet_NaN();
26 }
27 return bbo.ask_price.to_double() - bbo.bid_price.to_double();
28}
29
35[[nodiscard]] inline auto mid_price(const book::Bbo& bbo) -> double {
36 if (!bbo.has_bid || !bbo.has_ask) {
37 return std::numeric_limits<double>::quiet_NaN();
38 }
39 return (bbo.bid_price.to_double() + bbo.ask_price.to_double()) / 2.0;
40}
41
51[[nodiscard]] inline auto queue_imbalance(const book::Bbo& bbo) -> double {
52 const double bid = static_cast<double>(bbo.bid_shares);
53 const double ask = static_cast<double>(bbo.ask_shares);
54 const double total = bid + ask;
55 if (total <= 0.0) {
56 return std::numeric_limits<double>::quiet_NaN();
57 }
58 return (bid - ask) / total;
59}
60
67[[nodiscard]] inline auto depth_at_level(
68 const book::L3Book& book, book::Side side, std::size_t levels
69) -> std::uint64_t {
70 std::uint64_t total = 0;
71 for (const auto& level : book.depth(side, levels)) {
72 total += level.shares;
73 }
74 return total;
75}
76
87[[nodiscard]] inline auto order_flow_imbalance(const book::Bbo& previous, const book::Bbo& current)
88 -> double {
89 const double prev_bid_price = previous.bid_price.to_double();
90 const double curr_bid_price = current.bid_price.to_double();
91 const double prev_ask_price = previous.ask_price.to_double();
92 const double curr_ask_price = current.ask_price.to_double();
93 const double prev_bid_size = static_cast<double>(previous.bid_shares);
94 const double curr_bid_size = static_cast<double>(current.bid_shares);
95 const double prev_ask_size = static_cast<double>(previous.ask_shares);
96 const double curr_ask_size = static_cast<double>(current.ask_shares);
97
98 double bid_flow = 0.0;
99 if (curr_bid_price > prev_bid_price) {
100 bid_flow = curr_bid_size;
101 } else if (curr_bid_price == prev_bid_price) {
102 bid_flow = curr_bid_size - prev_bid_size;
103 } else {
104 bid_flow = -prev_bid_size;
105 }
106
107 double ask_flow = 0.0;
108 if (curr_ask_price > prev_ask_price) {
109 ask_flow = prev_ask_size;
110 } else if (curr_ask_price == prev_ask_price) {
111 ask_flow = curr_ask_size - prev_ask_size;
112 } else {
113 ask_flow = -curr_ask_size;
114 }
115
116 return bid_flow - ask_flow;
117}
118
119} // namespace itch::analytics
A single-symbol, order-level (L3) limit order book with allocation-light internals.
Definition l3_book.hpp:72
char side
Definition csv_sink.cpp:21
Single-symbol, order-level (L3) limit order book with allocation-light internals.
auto queue_imbalance(const book::Bbo &bbo) -> double
Top-of-book queue imbalance in [-1, 1].
auto spread(const book::Bbo &bbo) -> double
The bid-ask spread in price units, or NaN if either side is empty.
auto depth_at_level(const book::L3Book &book, book::Side side, std::size_t levels) -> std::uint64_t
Total displayed shares within the best levels price levels of a side.
auto order_flow_imbalance(const book::Bbo &previous, const book::Bbo &current) -> double
The order-flow imbalance between two consecutive BBO observations.
auto mid_price(const book::Bbo &bbo) -> double
The mid price ((bid + ask) / 2), or NaN if either side is empty.
Side
Which side of the book an order rests on.
Definition l3_book.hpp:26
Best bid and offer of a single book.
Definition l3_book.hpp:46