nautilus_system/
config.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{fmt::Debug, time::Duration};
17
18use nautilus_common::{
19    cache::CacheConfig, enums::Environment, logging::logger::LoggerConfig,
20    msgbus::database::MessageBusConfig,
21};
22use nautilus_core::{UUID4, UnixNanos};
23use nautilus_data::engine::config::DataEngineConfig;
24use nautilus_execution::engine::config::ExecutionEngineConfig;
25use nautilus_model::identifiers::TraderId;
26use nautilus_portfolio::config::PortfolioConfig;
27use nautilus_risk::engine::config::RiskEngineConfig;
28
29/// Configuration trait for a `NautilusKernel` core system instance.
30pub trait NautilusKernelConfig: Debug {
31    /// Returns the kernel environment context.
32    fn environment(&self) -> Environment;
33    /// Returns the trader ID for the node.
34    fn trader_id(&self) -> TraderId;
35    /// Returns if trading strategy state should be loaded from the database on start.
36    fn load_state(&self) -> bool;
37    /// Returns if trading strategy state should be saved to the database on stop.
38    fn save_state(&self) -> bool;
39    /// Returns the logging configuration for the kernel.
40    fn logging(&self) -> LoggerConfig;
41    /// Returns the unique instance identifier for the kernel.
42    fn instance_id(&self) -> Option<UUID4>;
43    /// Returns the timeout for all clients to connect and initialize.
44    fn timeout_connection(&self) -> Duration;
45    /// Returns the timeout for execution state to reconcile.
46    fn timeout_reconciliation(&self) -> Duration;
47    /// Returns the timeout for portfolio to initialize margins and unrealized pnls.
48    fn timeout_portfolio(&self) -> Duration;
49    /// Returns the timeout for all engine clients to disconnect.
50    fn timeout_disconnection(&self) -> Duration;
51    /// Returns the timeout after stopping the node to await residual events before final shutdown.
52    fn delay_post_stop(&self) -> Duration;
53    /// Returns the timeout to await pending tasks cancellation during shutdown.
54    fn timeout_shutdown(&self) -> Duration;
55    /// Returns the cache configuration.
56    fn cache(&self) -> Option<CacheConfig>;
57    /// Returns the message bus configuration.
58    fn msgbus(&self) -> Option<MessageBusConfig>;
59    /// Returns the data engine configuration.
60    fn data_engine(&self) -> Option<DataEngineConfig>;
61    /// Returns the risk engine configuration.
62    fn risk_engine(&self) -> Option<RiskEngineConfig>;
63    /// Returns the execution engine configuration.
64    fn exec_engine(&self) -> Option<ExecutionEngineConfig>;
65    /// Returns the portfolio configuration.
66    fn portfolio(&self) -> Option<PortfolioConfig>;
67    /// Returns the configuration for streaming to feather files.
68    fn streaming(&self) -> Option<StreamingConfig>;
69}
70
71/// Basic implementation of `NautilusKernelConfig` for builder and testing.
72#[derive(Debug, Clone)]
73pub struct KernelConfig {
74    /// The kernel environment context.
75    pub environment: Environment,
76    /// The trader ID for the node (must be a name and ID tag separated by a hyphen).
77    pub trader_id: TraderId,
78    /// If trading strategy state should be loaded from the database on start.
79    pub load_state: bool,
80    /// If trading strategy state should be saved to the database on stop.
81    pub save_state: bool,
82    /// The logging configuration for the kernel.
83    pub logging: LoggerConfig,
84    /// The unique instance identifier for the kernel
85    pub instance_id: Option<UUID4>,
86    /// The timeout for all clients to connect and initialize.
87    pub timeout_connection: Duration,
88    /// The timeout for execution state to reconcile.
89    pub timeout_reconciliation: Duration,
90    /// The timeout for portfolio to initialize margins and unrealized pnls.
91    pub timeout_portfolio: Duration,
92    /// The timeout for all engine clients to disconnect.
93    pub timeout_disconnection: Duration,
94    /// The delay after stopping the node to await residual events before final shutdown.
95    pub delay_post_stop: Duration,
96    /// The delay to await pending tasks cancellation during shutdown.
97    pub timeout_shutdown: Duration,
98    /// The cache configuration.
99    pub cache: Option<CacheConfig>,
100    /// The message bus configuration.
101    pub msgbus: Option<MessageBusConfig>,
102    /// The data engine configuration.
103    pub data_engine: Option<DataEngineConfig>,
104    /// The risk engine configuration.
105    pub risk_engine: Option<RiskEngineConfig>,
106    /// The execution engine configuration.
107    pub exec_engine: Option<ExecutionEngineConfig>,
108    /// The portfolio configuration.
109    pub portfolio: Option<PortfolioConfig>,
110    /// The configuration for streaming to feather files.
111    pub streaming: Option<StreamingConfig>,
112}
113
114impl NautilusKernelConfig for KernelConfig {
115    fn environment(&self) -> Environment {
116        self.environment
117    }
118
119    fn trader_id(&self) -> TraderId {
120        self.trader_id
121    }
122
123    fn load_state(&self) -> bool {
124        self.load_state
125    }
126
127    fn save_state(&self) -> bool {
128        self.save_state
129    }
130
131    fn logging(&self) -> LoggerConfig {
132        self.logging.clone()
133    }
134
135    fn instance_id(&self) -> Option<UUID4> {
136        self.instance_id
137    }
138
139    fn timeout_connection(&self) -> Duration {
140        self.timeout_connection
141    }
142
143    fn timeout_reconciliation(&self) -> Duration {
144        self.timeout_reconciliation
145    }
146
147    fn timeout_portfolio(&self) -> Duration {
148        self.timeout_portfolio
149    }
150
151    fn timeout_disconnection(&self) -> Duration {
152        self.timeout_disconnection
153    }
154
155    fn delay_post_stop(&self) -> Duration {
156        self.delay_post_stop
157    }
158
159    fn timeout_shutdown(&self) -> Duration {
160        self.timeout_shutdown
161    }
162
163    fn cache(&self) -> Option<CacheConfig> {
164        self.cache.clone()
165    }
166
167    fn msgbus(&self) -> Option<MessageBusConfig> {
168        self.msgbus.clone()
169    }
170
171    fn data_engine(&self) -> Option<DataEngineConfig> {
172        self.data_engine.clone()
173    }
174
175    fn risk_engine(&self) -> Option<RiskEngineConfig> {
176        self.risk_engine.clone()
177    }
178
179    fn exec_engine(&self) -> Option<ExecutionEngineConfig> {
180        self.exec_engine.clone()
181    }
182
183    fn portfolio(&self) -> Option<PortfolioConfig> {
184        self.portfolio.clone()
185    }
186
187    fn streaming(&self) -> Option<StreamingConfig> {
188        self.streaming.clone()
189    }
190}
191
192impl Default for KernelConfig {
193    fn default() -> Self {
194        Self {
195            environment: Environment::Backtest,
196            trader_id: TraderId::default(),
197            load_state: false,
198            save_state: false,
199            logging: LoggerConfig::default(),
200            instance_id: None,
201            timeout_connection: Duration::from_secs(60),
202            timeout_reconciliation: Duration::from_secs(30),
203            timeout_portfolio: Duration::from_secs(10),
204            timeout_disconnection: Duration::from_secs(10),
205            delay_post_stop: Duration::from_secs(10),
206            timeout_shutdown: Duration::from_secs(5),
207            cache: None,
208            msgbus: None,
209            data_engine: None,
210            risk_engine: None,
211            exec_engine: None,
212            portfolio: None,
213            streaming: None,
214        }
215    }
216}
217
218/// Configuration for file rotation in streaming output.
219#[derive(Debug, Clone)]
220pub enum RotationConfig {
221    /// Rotate based on file size.
222    Size {
223        /// Maximum buffer size in bytes before rotation.
224        max_size: u64,
225    },
226    /// Rotate based on a time interval.
227    Interval {
228        /// Interval in nanoseconds.
229        interval_ns: u64,
230    },
231    /// Rotate based on scheduled dates.
232    ScheduledDates {
233        /// Interval in nanoseconds.
234        interval_ns: u64,
235        /// Start of the scheduled rotation period.
236        schedule_ns: UnixNanos,
237    },
238    /// No automatic rotation.
239    NoRotation,
240}
241
242/// Configuration for streaming live or backtest runs to the catalog in feather format.
243#[derive(Debug, Clone)]
244pub struct StreamingConfig {
245    /// The path to the data catalog.
246    pub catalog_path: String,
247    /// The `fsspec` filesystem protocol for the catalog.
248    pub fs_protocol: String,
249    /// The flush interval (milliseconds) for writing chunks.
250    pub flush_interval_ms: u64,
251    /// If any existing feather files should be replaced.
252    pub replace_existing: bool,
253    /// Rotation configuration.
254    pub rotation_config: RotationConfig,
255}
256
257impl StreamingConfig {
258    /// Creates a new [`StreamingConfig`] instance.
259    #[must_use]
260    pub const fn new(
261        catalog_path: String,
262        fs_protocol: String,
263        flush_interval_ms: u64,
264        replace_existing: bool,
265        rotation_config: RotationConfig,
266    ) -> Self {
267        Self {
268            catalog_path,
269            fs_protocol,
270            flush_interval_ms,
271            replace_existing,
272            rotation_config,
273        }
274    }
275}