Skip to main content

nautilus_trading/algorithm/
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
16//! Configuration for execution algorithms.
17
18use std::collections::HashMap;
19
20use nautilus_core::serialization::default_true;
21use nautilus_model::identifiers::ExecAlgorithmId;
22use serde::{Deserialize, Serialize};
23
24/// Configuration for an execution algorithm.
25#[derive(Clone, Debug, Deserialize, Serialize, bon::Builder)]
26#[serde(deny_unknown_fields)]
27#[cfg_attr(
28    feature = "python",
29    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.trading", from_py_object)
30)]
31pub struct ExecutionAlgorithmConfig {
32    /// The unique ID for the execution algorithm.
33    pub exec_algorithm_id: Option<ExecAlgorithmId>,
34    /// If events should be logged by the algorithm.
35    #[serde(default = "default_true")]
36    #[builder(default = true)]
37    pub log_events: bool,
38    /// If commands should be logged by the algorithm.
39    #[serde(default = "default_true")]
40    #[builder(default = true)]
41    pub log_commands: bool,
42}
43
44impl Default for ExecutionAlgorithmConfig {
45    fn default() -> Self {
46        Self::builder().build()
47    }
48}
49
50/// Configuration for creating execution algorithms from importable paths.
51#[derive(Debug, Clone, Deserialize, Serialize)]
52#[serde(deny_unknown_fields)]
53#[cfg_attr(
54    feature = "python",
55    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.trading", from_py_object)
56)]
57#[cfg_attr(
58    feature = "python",
59    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.trading")
60)]
61pub struct ImportableExecAlgorithmConfig {
62    /// The fully qualified name of the execution algorithm class.
63    pub exec_algorithm_path: String,
64    /// The fully qualified name of the execution algorithm config class.
65    pub config_path: String,
66    /// The execution algorithm configuration as a dictionary.
67    pub config: HashMap<String, serde_json::Value>,
68}
69
70#[cfg(test)]
71mod tests {
72    use rstest::rstest;
73
74    use super::*;
75
76    #[rstest]
77    fn test_config_default() {
78        let config = ExecutionAlgorithmConfig::default();
79
80        assert!(config.exec_algorithm_id.is_none());
81        assert!(config.log_events);
82        assert!(config.log_commands);
83    }
84
85    #[rstest]
86    fn test_config_with_id() {
87        let exec_algorithm_id = ExecAlgorithmId::new("TWAP");
88        let config = ExecutionAlgorithmConfig {
89            exec_algorithm_id: Some(exec_algorithm_id),
90            ..Default::default()
91        };
92
93        assert_eq!(config.exec_algorithm_id, Some(exec_algorithm_id));
94    }
95
96    #[rstest]
97    fn test_config_serialization() {
98        let config = ExecutionAlgorithmConfig {
99            exec_algorithm_id: Some(ExecAlgorithmId::new("TWAP")),
100            log_events: false,
101            log_commands: true,
102        };
103
104        let json = serde_json::to_string(&config).unwrap();
105        let deserialized: ExecutionAlgorithmConfig = serde_json::from_str(&json).unwrap();
106
107        assert_eq!(config.exec_algorithm_id, deserialized.exec_algorithm_id);
108        assert_eq!(config.log_events, deserialized.log_events);
109        assert_eq!(config.log_commands, deserialized.log_commands);
110    }
111}