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#[cfg_attr(
27 feature = "python",
28 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.trading", from_py_object)
29)]
30pub struct ExecutionAlgorithmConfig {
31 pub exec_algorithm_id: Option<ExecAlgorithmId>,
33 #[serde(default = "default_true")]
35 #[builder(default = true)]
36 pub log_events: bool,
37 #[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#[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 pub exec_algorithm_path: String,
62 pub config_path: String,
64 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}