ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
bars.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include <algorithm>
13#include <cstdint>
14#include <functional>
15#include <utility>
16
17#include "itch/price.hpp"
18#include "itch/tape.hpp"
19
20namespace itch::analytics {
21
23struct Bar {
24 std::uint64_t bucket {0};
25 std::uint64_t start_timestamp {0};
26 std::uint64_t end_timestamp {0};
31 std::uint64_t volume {0};
32 std::uint64_t trade_count {0};
33};
34
36using BarCallback = std::function<void(const Bar&)>;
37
39struct TimeClock {
40 std::uint64_t interval_ns {1};
41
46 [[nodiscard]] auto bucket(const Trade& trade, std::uint64_t, std::uint64_t) const noexcept
47 -> std::uint64_t {
48 return trade.timestamp / interval_ns;
49 }
50};
51
53struct TickClock {
54 std::uint64_t ticks_per_bar {1};
55
60 [[nodiscard]] auto bucket(const Trade&, std::uint64_t, std::uint64_t tick_index) const noexcept
61 -> std::uint64_t {
62 return tick_index / ticks_per_bar;
63 }
64};
65
68 std::uint64_t volume_per_bar {1};
69
74 [[nodiscard]] auto bucket(const Trade&, std::uint64_t cumulative_volume, std::uint64_t)
75 const noexcept -> std::uint64_t {
76 return cumulative_volume / volume_per_bar;
77 }
78};
79
86template <typename Clock>
88 public:
94 BarBuilder(Clock clock, BarCallback callback)
95 : m_clock {std::move(clock)}, m_callback {std::move(callback)} {}
96
100 auto add(const Trade& trade) -> void {
101 const std::uint64_t bucket = m_clock.bucket(trade, m_cumulative_volume, m_tick_index);
102 if (m_has_bar && bucket != m_bar.bucket) {
103 emit();
104 }
105 if (!m_has_bar) {
106 m_bar = Bar {};
107 m_bar.bucket = bucket;
108 m_bar.start_timestamp = trade.timestamp;
109 m_bar.open = trade.price;
110 m_bar.high = trade.price;
111 m_bar.low = trade.price;
112 m_has_bar = true;
113 }
114 m_bar.high = std::max(m_bar.high, trade.price);
115 m_bar.low = std::min(m_bar.low, trade.price);
116 m_bar.close = trade.price;
117 m_bar.end_timestamp = trade.timestamp;
118 m_bar.volume += trade.shares;
119 ++m_bar.trade_count;
120
121 m_cumulative_volume += trade.shares;
122 ++m_tick_index;
123 }
124
126 auto flush() -> void {
127 if (m_has_bar) {
128 emit();
129 }
130 }
131
132 private:
134 auto emit() -> void {
135 if (m_callback) {
136 m_callback(m_bar);
137 }
138 m_has_bar = false;
139 }
140
141 Clock m_clock;
142 BarCallback m_callback;
143 Bar m_bar {};
144 bool m_has_bar {false};
145 std::uint64_t m_cumulative_volume {0};
146 std::uint64_t m_tick_index {0};
147};
148
149} // namespace itch::analytics
Builds OHLCV bars from a trade stream under a configurable clock.
Definition bars.hpp:87
auto add(const Trade &trade) -> void
Adds one trade, emitting the previous bar when the bucket changes.
Definition bars.hpp:100
auto flush() -> void
Emits the current partial bar, if any, and clears it.
Definition bars.hpp:126
BarBuilder(Clock clock, BarCallback callback)
Constructs a builder with the given clock policy and completion callback.
Definition bars.hpp:94
std::function< void(const Bar &)> BarCallback
Callback invoked with each completed bar.
Definition bars.hpp:36
Strongly typed, fixed-point price representation for ITCH price fields.
A single execution extracted from the feed (the "trade tape").
Definition tape.hpp:28
One OHLCV bar aggregated from the trade tape.
Definition bars.hpp:23
StandardPrice close
Last trade price.
Definition bars.hpp:30
std::uint64_t trade_count
Number of trades in the bar.
Definition bars.hpp:32
StandardPrice open
First trade price.
Definition bars.hpp:27
std::uint64_t start_timestamp
Timestamp of the first trade in the bar.
Definition bars.hpp:25
std::uint64_t bucket
Clock bucket id this bar covers.
Definition bars.hpp:24
StandardPrice low
Lowest trade price.
Definition bars.hpp:29
std::uint64_t end_timestamp
Timestamp of the last trade in the bar.
Definition bars.hpp:26
std::uint64_t volume
Total shares traded in the bar.
Definition bars.hpp:31
StandardPrice high
Highest trade price.
Definition bars.hpp:28
A clock that buckets a fixed number of trades into each bar.
Definition bars.hpp:53
auto bucket(const Trade &, std::uint64_t, std::uint64_t tick_index) const noexcept -> std::uint64_t
Computes the bucket id for the current trade from its tick position.
Definition bars.hpp:60
std::uint64_t ticks_per_bar
Definition bars.hpp:54
A clock that buckets trades by wall-clock time (nanoseconds).
Definition bars.hpp:39
std::uint64_t interval_ns
Definition bars.hpp:40
auto bucket(const Trade &trade, std::uint64_t, std::uint64_t) const noexcept -> std::uint64_t
Computes the bucket id for trade from its wall-clock timestamp.
Definition bars.hpp:46
A clock that buckets a fixed amount of traded volume into each bar.
Definition bars.hpp:67
std::uint64_t volume_per_bar
Definition bars.hpp:68
auto bucket(const Trade &, std::uint64_t cumulative_volume, std::uint64_t) const noexcept -> std::uint64_t
Computes the bucket id for the current trade from cumulative volume.
Definition bars.hpp:74
A normalized trade record and callback type for consuming the ITCH trade tape.