Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
tick.hpp
Go to the documentation of this file.
1#pragma once
2/**
3 * @file tick.hpp
4 * @brief OHLCVTick — atomic market data unit for the lock-free streaming pipeline.
5 *
6 * Module: include/srfm/stream/
7 * Owner: AGT-10 (Builder) — 2026-03-01
8 *
9 * Responsibility
10 * --------------
11 * Define the canonical OHLCVTick structure that flows through the streaming
12 * pipeline from TickIngester → SignalProcessor. Every field has a defined
13 * valid range; validation is performed at the ingestion boundary.
14 *
15 * Design Constraints
16 * ------------------
17 * • Trivially copyable — ring buffer moves raw bytes.
18 * • No dynamic allocation — fixed-size POD struct.
19 * • All validation is non-throwing (returns bool, never throws).
20 *
21 * NOT Responsible For
22 * -------------------
23 * • Parsing from wire format (TickIngester / PipeTickSource)
24 * • Storage or persistence (stateless type definition)
25 * • Cross-asset normalisation (CoordinateNormalizer)
26 */
27
28#include <cmath>
29#include <cstdint>
30
31namespace srfm::stream {
32
33// ── OHLCVTick ─────────────────────────────────────────────────────────────────
34
35/**
36 * @brief Single OHLCV bar tick from the market data feed.
37 *
38 * All price fields are in quote-currency units (e.g., USD).
39 * Volume is in base-currency units (e.g., contracts or shares).
40 * timestamp_ns is nanoseconds since Unix epoch (UTC).
41 *
42 * Invariants (enforced by tick_is_valid()):
43 * - open, high, low, close > 0 and finite
44 * - high >= low
45 * - high >= open, high >= close
46 * - low <= open, low <= close
47 * - volume >= 0 and finite
48 * - timestamp_ns > 0
49 */
50struct OHLCVTick {
51 double open{0.0}; ///< Opening price of the bar.
52 double high{0.0}; ///< Highest price of the bar.
53 double low{0.0}; ///< Lowest price of the bar.
54 double close{0.0}; ///< Closing price of the bar.
55 double volume{0.0}; ///< Traded volume over the bar.
56 std::int64_t timestamp_ns{0};///< Bar timestamp, nanoseconds since epoch.
57};
58
59// ── Validation ────────────────────────────────────────────────────────────────
60
61/**
62 * @brief Validate all fields of an OHLCVTick.
63 *
64 * Rejects ticks that would produce NaN or negative prices/volumes downstream.
65 * This is the single authoritative validation gate; call it exactly once at
66 * the ingestion boundary.
67 *
68 * @return true iff the tick satisfies all field invariants.
69 * @return false on any out-of-range, NaN, or Inf value.
70 *
71 * @note noexcept — safe to call in the hot path inside the ingestion thread.
72 */
73[[nodiscard]] inline bool tick_is_valid(const OHLCVTick& t) noexcept {
74 // All price fields must be finite and positive.
75 if (!std::isfinite(t.open) || t.open <= 0.0) return false;
76 if (!std::isfinite(t.high) || t.high <= 0.0) return false;
77 if (!std::isfinite(t.low) || t.low <= 0.0) return false;
78 if (!std::isfinite(t.close) || t.close <= 0.0) return false;
79 // Volume must be finite and non-negative.
80 if (!std::isfinite(t.volume) || t.volume < 0.0) return false;
81 // Timestamp must be positive.
82 if (t.timestamp_ns <= 0) return false;
83 // OHLC consistency.
84 if (t.high < t.low) return false;
85 if (t.high < t.open) return false;
86 if (t.high < t.close) return false;
87 if (t.low > t.open) return false;
88 if (t.low > t.close) return false;
89 return true;
90}
91
92/**
93 * @brief Reason codes for tick rejection (diagnostic, not returned on hot path).
94 *
95 * Used by TickIngester to categorise dropped ticks in its counters.
96 */
97enum class TickRejectReason : std::uint8_t {
98 NanOrInfOpen = 0,
99 NanOrInfHigh = 1,
100 NanOrInfLow = 2,
101 NanOrInfClose = 3,
102 NanOrInfVolume = 4,
104 NegativeVolume = 6,
107 RingFull = 9, ///< Valid tick dropped because ring buffer was full.
108};
109
110} // namespace srfm::stream
bool tick_is_valid(const OHLCVTick &t) noexcept
Validate all fields of an OHLCVTick.
Definition tick.hpp:73
TickRejectReason
Reason codes for tick rejection (diagnostic, not returned on hot path).
Definition tick.hpp:97
@ RingFull
Valid tick dropped because ring buffer was full.
Single OHLCV bar tick from the market data feed.
Definition tick.hpp:50
double low
Lowest price of the bar.
Definition tick.hpp:53
double open
Opening price of the bar.
Definition tick.hpp:51
double volume
Traded volume over the bar.
Definition tick.hpp:55
double close
Closing price of the bar.
Definition tick.hpp:54
std::int64_t timestamp_ns
Bar timestamp, nanoseconds since epoch.
Definition tick.hpp:56
double high
Highest price of the bar.
Definition tick.hpp:52