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