nautilus_indicators/average/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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//! Moving average type indicators.
17
18pub mod ama;
19pub mod dema;
20pub mod ema;
21pub mod hma;
22pub mod lr;
23pub mod rma;
24pub mod sma;
25pub mod vidya;
26pub mod vwap;
27pub mod wma;
28
29use nautilus_model::enums::PriceType;
30use strum::{AsRefStr, Display, EnumIter, EnumString, FromRepr};
31
32use crate::{
33    average::{
34        dema::DoubleExponentialMovingAverage, ema::ExponentialMovingAverage,
35        hma::HullMovingAverage, rma::WilderMovingAverage, sma::SimpleMovingAverage,
36    },
37    indicator::MovingAverage,
38};
39
40#[repr(C)]
41#[derive(
42    Copy,
43    Clone,
44    Debug,
45    Display,
46    Hash,
47    PartialEq,
48    Eq,
49    PartialOrd,
50    Ord,
51    AsRefStr,
52    FromRepr,
53    EnumIter,
54    EnumString,
55)]
56#[strum(ascii_case_insensitive)]
57#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
58#[cfg_attr(
59    feature = "python",
60    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.indicators")
61)]
62pub enum MovingAverageType {
63    Simple,
64    Exponential,
65    DoubleExponential,
66    Wilder,
67    Hull,
68}
69
70pub struct MovingAverageFactory;
71
72impl MovingAverageFactory {
73    #[must_use]
74    pub fn create(
75        moving_average_type: MovingAverageType,
76        period: usize,
77    ) -> Box<dyn MovingAverage + Send + Sync> {
78        let price_type = Some(PriceType::Last);
79
80        match moving_average_type {
81            MovingAverageType::Simple => Box::new(SimpleMovingAverage::new(period, price_type)),
82            MovingAverageType::Exponential => {
83                Box::new(ExponentialMovingAverage::new(period, price_type))
84            }
85            MovingAverageType::DoubleExponential => {
86                Box::new(DoubleExponentialMovingAverage::new(period, price_type))
87            }
88            MovingAverageType::Wilder => Box::new(WilderMovingAverage::new(period, price_type)),
89            MovingAverageType::Hull => Box::new(HullMovingAverage::new(period, price_type)),
90        }
91    }
92}