nautilus_system/
config.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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;
23use nautilus_data::engine::config::DataEngineConfig;
24use nautilus_execution::engine::config::ExecutionEngineConfig;
25use nautilus_model::identifiers::TraderId;
26#[cfg(feature = "streaming")]
27use nautilus_persistence::config::StreamingConfig;
28use nautilus_portfolio::config::PortfolioConfig;
29use nautilus_risk::engine::config::RiskEngineConfig;
30
31/// Configuration trait for a `NautilusKernel` core system instance.
32pub trait NautilusKernelConfig: Debug {
33    /// Returns the kernel environment context.
34    fn environment(&self) -> Environment;
35    /// Returns the trader ID for the node.
36    fn trader_id(&self) -> TraderId;
37    /// Returns if trading strategy state should be loaded from the database on start.
38    fn load_state(&self) -> bool;
39    /// Returns if trading strategy state should be saved to the database on stop.
40    fn save_state(&self) -> bool;
41    /// Returns the logging configuration for the kernel.
42    fn logging(&self) -> LoggerConfig;
43    /// Returns the unique instance identifier for the kernel.
44    fn instance_id(&self) -> Option<UUID4>;
45    /// Returns the timeout for all clients to connect and initialize.
46    fn timeout_connection(&self) -> Duration;
47    /// Returns the timeout for execution state to reconcile.
48    fn timeout_reconciliation(&self) -> Duration;
49    /// Returns the timeout for portfolio to initialize margins and unrealized pnls.
50    fn timeout_portfolio(&self) -> Duration;
51    /// Returns the timeout for all engine clients to disconnect.
52    fn timeout_disconnection(&self) -> Duration;
53    /// Returns the timeout after stopping the node to await residual events before final shutdown.
54    fn delay_post_stop(&self) -> Duration;
55    /// Returns the timeout to await pending tasks cancellation during shutdown.
56    fn timeout_shutdown(&self) -> Duration;
57    /// Returns the cache configuration.
58    fn cache(&self) -> Option<CacheConfig>;
59    /// Returns the message bus configuration.
60    fn msgbus(&self) -> Option<MessageBusConfig>;
61    /// Returns the data engine configuration.
62    fn data_engine(&self) -> Option<DataEngineConfig>;
63    /// Returns the risk engine configuration.
64    fn risk_engine(&self) -> Option<RiskEngineConfig>;
65    /// Returns the execution engine configuration.
66    fn exec_engine(&self) -> Option<ExecutionEngineConfig>;
67    /// Returns the portfolio configuration.
68    fn portfolio(&self) -> Option<PortfolioConfig>;
69    /// Returns the configuration for streaming to feather files.
70    #[cfg(feature = "streaming")]
71    fn streaming(&self) -> Option<StreamingConfig>;
72}
73
74/// Basic implementation of `NautilusKernelConfig` for builder and testing.
75#[derive(Debug, Clone)]
76pub struct KernelConfig {
77    /// The kernel environment context.
78    pub environment: Environment,
79    /// The trader ID for the node (must be a name and ID tag separated by a hyphen).
80    pub trader_id: TraderId,
81    /// If trading strategy state should be loaded from the database on start.
82    pub load_state: bool,
83    /// If trading strategy state should be saved to the database on stop.
84    pub save_state: bool,
85    /// The logging configuration for the kernel.
86    pub logging: LoggerConfig,
87    /// The unique instance identifier for the kernel
88    pub instance_id: Option<UUID4>,
89    /// The timeout for all clients to connect and initialize.
90    pub timeout_connection: Duration,
91    /// The timeout for execution state to reconcile.
92    pub timeout_reconciliation: Duration,
93    /// The timeout for portfolio to initialize margins and unrealized pnls.
94    pub timeout_portfolio: Duration,
95    /// The timeout for all engine clients to disconnect.
96    pub timeout_disconnection: Duration,
97    /// The delay after stopping the node to await residual events before final shutdown.
98    pub delay_post_stop: Duration,
99    /// The delay to await pending tasks cancellation during shutdown.
100    pub timeout_shutdown: Duration,
101    /// The cache configuration.
102    pub cache: Option<CacheConfig>,
103    /// The message bus configuration.
104    pub msgbus: Option<MessageBusConfig>,
105    /// The data engine configuration.
106    pub data_engine: Option<DataEngineConfig>,
107    /// The risk engine configuration.
108    pub risk_engine: Option<RiskEngineConfig>,
109    /// The execution engine configuration.
110    pub exec_engine: Option<ExecutionEngineConfig>,
111    /// The portfolio configuration.
112    pub portfolio: Option<PortfolioConfig>,
113    /// The configuration for streaming to feather files.
114    #[cfg(feature = "streaming")]
115    pub streaming: Option<StreamingConfig>,
116}
117
118impl NautilusKernelConfig for KernelConfig {
119    fn environment(&self) -> Environment {
120        self.environment
121    }
122
123    fn trader_id(&self) -> TraderId {
124        self.trader_id
125    }
126
127    fn load_state(&self) -> bool {
128        self.load_state
129    }
130
131    fn save_state(&self) -> bool {
132        self.save_state
133    }
134
135    fn logging(&self) -> LoggerConfig {
136        self.logging.clone()
137    }
138
139    fn instance_id(&self) -> Option<UUID4> {
140        self.instance_id
141    }
142
143    fn timeout_connection(&self) -> Duration {
144        self.timeout_connection
145    }
146
147    fn timeout_reconciliation(&self) -> Duration {
148        self.timeout_reconciliation
149    }
150
151    fn timeout_portfolio(&self) -> Duration {
152        self.timeout_portfolio
153    }
154
155    fn timeout_disconnection(&self) -> Duration {
156        self.timeout_disconnection
157    }
158
159    fn delay_post_stop(&self) -> Duration {
160        self.delay_post_stop
161    }
162
163    fn timeout_shutdown(&self) -> Duration {
164        self.timeout_shutdown
165    }
166
167    fn cache(&self) -> Option<CacheConfig> {
168        self.cache.clone()
169    }
170
171    fn msgbus(&self) -> Option<MessageBusConfig> {
172        self.msgbus.clone()
173    }
174
175    fn data_engine(&self) -> Option<DataEngineConfig> {
176        self.data_engine.clone()
177    }
178
179    fn risk_engine(&self) -> Option<RiskEngineConfig> {
180        self.risk_engine.clone()
181    }
182
183    fn exec_engine(&self) -> Option<ExecutionEngineConfig> {
184        self.exec_engine.clone()
185    }
186
187    fn portfolio(&self) -> Option<PortfolioConfig> {
188        self.portfolio.clone()
189    }
190
191    #[cfg(feature = "streaming")]
192    fn streaming(&self) -> Option<StreamingConfig> {
193        self.streaming.clone()
194    }
195}
196
197impl Default for KernelConfig {
198    fn default() -> Self {
199        Self {
200            environment: Environment::Backtest,
201            trader_id: TraderId::default(),
202            load_state: false,
203            save_state: false,
204            logging: LoggerConfig::default(),
205            instance_id: None,
206            timeout_connection: Duration::from_secs(60),
207            timeout_reconciliation: Duration::from_secs(30),
208            timeout_portfolio: Duration::from_secs(10),
209            timeout_disconnection: Duration::from_secs(10),
210            delay_post_stop: Duration::from_secs(10),
211            timeout_shutdown: Duration::from_secs(5),
212            cache: None,
213            msgbus: None,
214            data_engine: None,
215            risk_engine: None,
216            exec_engine: None,
217            portfolio: None,
218            #[cfg(feature = "streaming")]
219            streaming: None,
220        }
221    }
222}