Skip to main content

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, bon::Builder)]
73pub struct KernelConfig {
74    /// The kernel environment context.
75    #[builder(default = Environment::Backtest)]
76    pub environment: Environment,
77    /// The trader ID for the node (must be a name and ID tag separated by a hyphen).
78    #[builder(default)]
79    pub trader_id: TraderId,
80    /// If trading strategy state should be loaded from the database on start.
81    #[builder(default)]
82    pub load_state: bool,
83    /// If trading strategy state should be saved to the database on stop.
84    #[builder(default)]
85    pub save_state: bool,
86    /// The logging configuration for the kernel.
87    #[builder(default)]
88    pub logging: LoggerConfig,
89    /// The unique instance identifier for the kernel
90    pub instance_id: Option<UUID4>,
91    /// The timeout for all clients to connect and initialize.
92    #[builder(default = Duration::from_secs(120))]
93    pub timeout_connection: Duration,
94    /// The timeout for execution state to reconcile.
95    #[builder(default = Duration::from_secs(30))]
96    pub timeout_reconciliation: Duration,
97    /// The timeout for portfolio to initialize margins and unrealized pnls.
98    #[builder(default = Duration::from_secs(10))]
99    pub timeout_portfolio: Duration,
100    /// The timeout for all engine clients to disconnect.
101    #[builder(default = Duration::from_secs(10))]
102    pub timeout_disconnection: Duration,
103    /// The delay after stopping the node to await residual events before final shutdown.
104    #[builder(default = Duration::from_secs(10))]
105    pub delay_post_stop: Duration,
106    /// The delay to await pending tasks cancellation during shutdown.
107    #[builder(default = Duration::from_secs(5))]
108    pub timeout_shutdown: Duration,
109    /// The cache configuration.
110    pub cache: Option<CacheConfig>,
111    /// The message bus configuration.
112    pub msgbus: Option<MessageBusConfig>,
113    /// The data engine configuration.
114    pub data_engine: Option<DataEngineConfig>,
115    /// The risk engine configuration.
116    pub risk_engine: Option<RiskEngineConfig>,
117    /// The execution engine configuration.
118    pub exec_engine: Option<ExecutionEngineConfig>,
119    /// The portfolio configuration.
120    pub portfolio: Option<PortfolioConfig>,
121    /// The configuration for streaming to feather files.
122    pub streaming: Option<StreamingConfig>,
123}
124
125impl NautilusKernelConfig for KernelConfig {
126    fn environment(&self) -> Environment {
127        self.environment
128    }
129
130    fn trader_id(&self) -> TraderId {
131        self.trader_id
132    }
133
134    fn load_state(&self) -> bool {
135        self.load_state
136    }
137
138    fn save_state(&self) -> bool {
139        self.save_state
140    }
141
142    fn logging(&self) -> LoggerConfig {
143        self.logging.clone()
144    }
145
146    fn instance_id(&self) -> Option<UUID4> {
147        self.instance_id
148    }
149
150    fn timeout_connection(&self) -> Duration {
151        self.timeout_connection
152    }
153
154    fn timeout_reconciliation(&self) -> Duration {
155        self.timeout_reconciliation
156    }
157
158    fn timeout_portfolio(&self) -> Duration {
159        self.timeout_portfolio
160    }
161
162    fn timeout_disconnection(&self) -> Duration {
163        self.timeout_disconnection
164    }
165
166    fn delay_post_stop(&self) -> Duration {
167        self.delay_post_stop
168    }
169
170    fn timeout_shutdown(&self) -> Duration {
171        self.timeout_shutdown
172    }
173
174    fn cache(&self) -> Option<CacheConfig> {
175        self.cache.clone()
176    }
177
178    fn msgbus(&self) -> Option<MessageBusConfig> {
179        self.msgbus.clone()
180    }
181
182    fn data_engine(&self) -> Option<DataEngineConfig> {
183        self.data_engine.clone()
184    }
185
186    fn risk_engine(&self) -> Option<RiskEngineConfig> {
187        self.risk_engine.clone()
188    }
189
190    fn exec_engine(&self) -> Option<ExecutionEngineConfig> {
191        self.exec_engine.clone()
192    }
193
194    fn portfolio(&self) -> Option<PortfolioConfig> {
195        self.portfolio
196    }
197
198    fn streaming(&self) -> Option<StreamingConfig> {
199        self.streaming.clone()
200    }
201}
202
203impl Default for KernelConfig {
204    fn default() -> Self {
205        Self::builder().build()
206    }
207}
208
209/// Configuration for file rotation in streaming output.
210#[derive(Debug, Clone)]
211pub enum RotationConfig {
212    /// Rotate based on file size.
213    Size {
214        /// Maximum buffer size in bytes before rotation.
215        max_size: u64,
216    },
217    /// Rotate based on a time interval.
218    Interval {
219        /// Interval in nanoseconds.
220        interval_ns: u64,
221    },
222    /// Rotate based on scheduled dates.
223    ScheduledDates {
224        /// Interval in nanoseconds.
225        interval_ns: u64,
226        /// Start of the scheduled rotation period.
227        schedule_ns: UnixNanos,
228    },
229    /// No automatic rotation.
230    NoRotation,
231}
232
233/// Configuration for streaming live or backtest runs to the catalog in feather format.
234#[derive(Debug, Clone, bon::Builder)]
235pub struct StreamingConfig {
236    /// The path to the data catalog.
237    pub catalog_path: String,
238    /// The `fsspec` filesystem protocol for the catalog.
239    pub fs_protocol: String,
240    /// The flush interval (milliseconds) for writing chunks.
241    pub flush_interval_ms: u64,
242    /// If any existing feather files should be replaced.
243    pub replace_existing: bool,
244    /// Rotation configuration.
245    pub rotation_config: RotationConfig,
246}
247
248impl StreamingConfig {
249    /// Creates a new [`StreamingConfig`] instance.
250    #[must_use]
251    pub const fn new(
252        catalog_path: String,
253        fs_protocol: String,
254        flush_interval_ms: u64,
255        replace_existing: bool,
256        rotation_config: RotationConfig,
257    ) -> Self {
258        Self {
259            catalog_path,
260            fs_protocol,
261            flush_interval_ms,
262            replace_existing,
263            rotation_config,
264        }
265    }
266}