Skip to main content

nautilus_trading/examples/strategies/ema_cross/
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 the EMA crossover strategy.
17
18use nautilus_model::{
19    identifiers::{InstrumentId, StrategyId},
20    types::Quantity,
21};
22
23use crate::strategy::StrategyConfig;
24
25/// Configuration for the dual-EMA crossover strategy.
26#[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    /// Base strategy configuration.
33    pub base: StrategyConfig,
34    /// Instrument to subscribe to and trade.
35    pub instrument_id: InstrumentId,
36    /// Order quantity for each crossover signal.
37    pub trade_size: Quantity,
38    /// Fast EMA period. Shorter periods react faster.
39    pub fast_period: usize,
40    /// Slow EMA period. Longer periods filter noise.
41    pub slow_period: usize,
42}
43
44impl EmaCrossConfig {
45    /// Creates a new [`EmaCrossConfig`] with required fields and sensible defaults.
46    #[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}