Special Relativity in Financial Modeling 1.0.0
Lorentz transforms, spacetime classification, and geodesic price paths for quantitative finance
Loading...
Searching...
No Matches
engine.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file include/srfm/engine.hpp
4/// @brief Core Integration Engine — AGT-06 public API.
5///
6/// # Module: Integration Engine
7///
8/// ## Responsibility
9/// Orchestrate the full SRFM signal-processing pipeline:
10/// OHLCV bar data → BetaCalculator → LorentzTransform →
11/// MarketManifold → MomentumProcessor →
12/// MetricTensor + GeodesicSolver → Backtester → BacktestComparison
13///
14/// ## Usage
15/// ```cpp
16/// Engine engine;
17/// auto bars = DataLoader::load_csv("prices.csv");
18/// if (bars) {
19/// auto result = engine.run_backtest(*bars);
20/// if (result) fmt::print("{}\n", result->to_string());
21/// }
22/// ```
23///
24/// ## Guarantees
25/// - Zero panics: all fallible paths return `std::optional` or `bool`
26/// - Thread-safe reads: `run_backtest` and `run_stream_bar` are const
27/// - No raw pointers
28
29#include "srfm/backtest.hpp"
30#include "srfm/manifold.hpp"
31#include "srfm/types.hpp"
32#include "srfm/constants.hpp"
33
34#include <optional>
35#include <span>
36#include <string>
37#include <vector>
38
39namespace srfm::core {
40
41// ─── OHLCV ────────────────────────────────────────────────────────────────────
42
43/// A single OHLCV bar of market data.
44struct OHLCV {
45 double timestamp; ///< Bar index or Unix epoch seconds
46 double open; ///< Opening price
47 double high; ///< High price
48 double low; ///< Low price
49 double close; ///< Closing price
50 double volume; ///< Traded volume
51};
52
53// ─── EngineConfig ─────────────────────────────────────────────────────────────
54
55/// Configuration parameters for the core engine.
57 /// Maximum market velocity used to normalise β (BetaCalculator denominator).
58 double max_market_velocity = 1.0;
59
60 /// Effective mass proxy for momentum corrections (m_eff in p_rel = γ m v).
61 double effective_mass = 1.0;
62
63 /// Minimum number of bars required to run a backtest.
65
66 /// Backtest configuration forwarded to the Backtester.
68
69 /// If true, emit per-bar debug output to stderr.
70 bool verbose = false;
71};
72
73// ─── PipelineBar ──────────────────────────────────────────────────────────────
74
75/// Intermediate state for one bar after full pipeline processing.
77 OHLCV raw; ///< Original bar data
78 double price_return; ///< log or simple return for this bar
79 BetaVelocity beta; ///< Normalised market velocity
80 LorentzFactor gamma; ///< Lorentz factor at this bar
81 manifold::SpacetimeEvent event; ///< Spacetime embedding
82 manifold::IntervalType interval_type; ///< Causal character vs prior bar
83};
84
85// ─── Engine ───────────────────────────────────────────────────────────────────
86
87/// Orchestrates the full relativistic signal-processing pipeline.
88class Engine {
89public:
90 /// Construct with optional configuration.
91 explicit Engine(EngineConfig config = EngineConfig{});
92
93 /// Run a complete backtest on a sequence of OHLCV bars.
94 ///
95 /// # Pipeline
96 /// 1. Compute close-to-close simple returns.
97 /// 2. Compute β for each bar via BetaCalculator (rolling window = 5 bars).
98 /// 3. Classify each bar's spacetime interval via MarketManifold.
99 /// 4. Apply relativistic momentum corrections.
100 /// 5. Feed BarData + returns into Backtester::run().
101 ///
102 /// # Returns
103 /// `nullopt` if fewer than `config.min_bars` bars provided, fewer than
104 /// MIN_RETURN_SERIES_LENGTH returns result (N bars give N - 1 returns), or
105 /// any downstream computation is numerically degenerate.
106 [[nodiscard]] std::optional<backtest::BacktestComparison>
107 run_backtest(std::span<const OHLCV> bars) const noexcept;
108
109 /// Process a single bar in streaming mode, updating internal rolling state.
110 ///
111 /// Returns the PipelineBar result for the just-processed bar, or `nullopt`
112 /// if the internal window has fewer than 2 bars (warm-up phase).
113 [[nodiscard]] std::optional<PipelineBar>
114 process_stream_bar(const OHLCV& bar) noexcept;
115
116 /// Reset streaming state (clear internal bar window).
117 void reset_stream() noexcept;
118
119 /// Return a read-only view of the current streaming bar window.
120 [[nodiscard]] std::span<const OHLCV> stream_window() const noexcept;
121
122private:
123 /// Compute simple returns from a price series.
124 /// Returns vector of length (prices.size() − 1).
125 [[nodiscard]] static std::vector<double>
126 compute_returns(std::span<const double> prices) noexcept;
127
128 /// Compute β for each bar using a rolling 5-bar window.
129 /// Falls back to 0 (Newtonian) for the first few bars.
130 [[nodiscard]] static std::vector<BetaVelocity>
131 compute_betas(std::span<const double> prices,
132 double max_velocity) noexcept;
133
134 /// Build a SpacetimeEvent from an OHLCV bar.
135 [[nodiscard]] static manifold::SpacetimeEvent
136 to_event(const OHLCV& bar, double momentum_indicator) noexcept;
137
138 EngineConfig config_;
139 std::vector<OHLCV> stream_window_; ///< Rolling bar buffer for streaming
140};
141
142} // namespace srfm::core
Relativistic Backtester — AGT-05 public API.
Orchestrates the full relativistic signal-processing pipeline.
Definition engine.hpp:88
std::optional< PipelineBar > process_stream_bar(const OHLCV &bar) noexcept
Definition engine.cpp:84
std::optional< backtest::BacktestComparison > run_backtest(std::span< const OHLCV > bars) const noexcept
Definition engine.cpp:27
std::span< const OHLCV > stream_window() const noexcept
Return a read-only view of the current streaming bar window.
Definition engine.cpp:152
void reset_stream() noexcept
Reset streaming state (clear internal bar window).
Definition engine.cpp:146
Physical and financial constants for the SRFM system.
Spacetime Market Manifold — AGT-02 public API (implemented by AGT-06).
static constexpr std::size_t MIN_RETURN_SERIES_LENGTH
Definition constants.hpp:49
IntervalType
Causal character of a spacetime interval.
Definition manifold.hpp:61
Lorentz factor γ = 1/√(1−β²). Always ≥ 1.0 for valid beta.
Definition types.hpp:33
Configuration for a backtest run.
Definition backtest.hpp:113
Configuration parameters for the core engine.
Definition engine.hpp:56
double effective_mass
Effective mass proxy for momentum corrections (m_eff in p_rel = γ m v).
Definition engine.hpp:61
double max_market_velocity
Maximum market velocity used to normalise β (BetaCalculator denominator).
Definition engine.hpp:58
bool verbose
If true, emit per-bar debug output to stderr.
Definition engine.hpp:70
backtest::BacktestConfig backtest_cfg
Backtest configuration forwarded to the Backtester.
Definition engine.hpp:67
std::size_t min_bars
Minimum number of bars required to run a backtest.
Definition engine.hpp:64
A single OHLCV bar of market data.
Definition engine.hpp:44
double open
Opening price.
Definition engine.hpp:46
double low
Low price.
Definition engine.hpp:48
double close
Closing price.
Definition engine.hpp:49
double timestamp
Bar index or Unix epoch seconds.
Definition engine.hpp:45
double high
High price.
Definition engine.hpp:47
double volume
Traded volume.
Definition engine.hpp:50
Intermediate state for one bar after full pipeline processing.
Definition engine.hpp:76
double price_return
log or simple return for this bar
Definition engine.hpp:78
BetaVelocity beta
Normalised market velocity.
Definition engine.hpp:79
manifold::IntervalType interval_type
Causal character vs prior bar.
Definition engine.hpp:82
manifold::SpacetimeEvent event
Spacetime embedding.
Definition engine.hpp:81
LorentzFactor gamma
Lorentz factor at this bar.
Definition engine.hpp:80
OHLCV raw
Original bar data.
Definition engine.hpp:77
A point in 4D spacetime (t, x, y, z).
Shared primitive types for the Special Relativity in Financial Modeling (SRFM) system.