nautilus_system/
config.rs

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