nautilus_trading/algorithm/
config.rs1use std::collections::HashMap;
19
20use nautilus_core::serialization::default_true;
21use nautilus_model::identifiers::ExecAlgorithmId;
22use serde::{Deserialize, Serialize};
23
24#[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 pub exec_algorithm_id: Option<ExecAlgorithmId>,
34 #[serde(default = "default_true")]
36 #[builder(default = true)]
37 pub log_events: bool,
38 #[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#[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 pub exec_algorithm_path: String,
64 pub config_path: String,
66 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}