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