nautilus_trading/examples/strategies/ema_cross/
config.rs1use nautilus_model::{
19 identifiers::{InstrumentId, StrategyId},
20 types::Quantity,
21};
22
23use crate::strategy::StrategyConfig;
24
25#[derive(Debug, Clone)]
27#[cfg_attr(
28 feature = "python",
29 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.trading", from_py_object)
30)]
31pub struct EmaCrossConfig {
32 pub base: StrategyConfig,
34 pub instrument_id: InstrumentId,
36 pub trade_size: Quantity,
38 pub fast_period: usize,
40 pub slow_period: usize,
42}
43
44impl EmaCrossConfig {
45 #[must_use]
47 pub fn new(
48 instrument_id: InstrumentId,
49 trade_size: Quantity,
50 fast_period: usize,
51 slow_period: usize,
52 ) -> Self {
53 Self {
54 base: StrategyConfig {
55 strategy_id: Some(StrategyId::from("EMA_CROSS-001")),
56 order_id_tag: Some("001".to_string()),
57 ..Default::default()
58 },
59 instrument_id,
60 trade_size,
61 fast_period,
62 slow_period,
63 }
64 }
65
66 #[must_use]
67 pub fn with_strategy_id(mut self, strategy_id: StrategyId) -> Self {
68 self.base.strategy_id = Some(strategy_id);
69 self
70 }
71
72 #[must_use]
73 pub fn with_order_id_tag(mut self, tag: String) -> Self {
74 self.base.order_id_tag = Some(tag);
75 self
76 }
77}
78
79#[cfg(feature = "python")]
80#[pyo3::pymethods]
81impl EmaCrossConfig {
82 #[new]
83 #[pyo3(signature = (
84 instrument_id,
85 trade_size,
86 fast_period=10,
87 slow_period=50,
88 strategy_id=None,
89 order_id_tag=None,
90 ))]
91 fn py_new(
92 instrument_id: InstrumentId,
93 trade_size: Quantity,
94 fast_period: usize,
95 slow_period: usize,
96 strategy_id: Option<StrategyId>,
97 order_id_tag: Option<String>,
98 ) -> Self {
99 let mut config = Self::new(instrument_id, trade_size, fast_period, slow_period);
100
101 if let Some(id) = strategy_id {
102 config.base.strategy_id = Some(id);
103 }
104
105 if let Some(tag) = order_id_tag {
106 config.base.order_id_tag = Some(tag);
107 }
108
109 config
110 }
111
112 #[getter]
113 fn instrument_id(&self) -> InstrumentId {
114 self.instrument_id
115 }
116
117 #[getter]
118 fn trade_size(&self) -> Quantity {
119 self.trade_size
120 }
121
122 #[getter]
123 fn fast_period(&self) -> usize {
124 self.fast_period
125 }
126
127 #[getter]
128 fn slow_period(&self) -> usize {
129 self.slow_period
130 }
131}