Skip to main content

nautilus_indicators/average/
mod.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//! 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;
28pub mod zscore;
29
30use nautilus_model::enums::PriceType;
31use strum::{AsRefStr, Display, EnumIter, EnumString, FromRepr};
32
33use crate::{
34    average::{
35        dema::DoubleExponentialMovingAverage, ema::ExponentialMovingAverage,
36        hma::HullMovingAverage, rma::WilderMovingAverage, sma::SimpleMovingAverage,
37    },
38    indicator::MovingAverage,
39};
40
41#[repr(C)]
42#[derive(
43    Copy,
44    Clone,
45    Debug,
46    Display,
47    Hash,
48    PartialEq,
49    Eq,
50    PartialOrd,
51    Ord,
52    AsRefStr,
53    FromRepr,
54    EnumIter,
55    EnumString,
56)]
57#[strum(ascii_case_insensitive)]
58#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
59#[cfg_attr(
60    feature = "python",
61    pyo3::pyclass(
62        frozen,
63        eq,
64        eq_int,
65        hash,
66        module = "nautilus_trader.indicators",
67        from_py_object,
68    )
69)]
70#[cfg_attr(
71    feature = "python",
72    pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.indicators")
73)]
74pub enum MovingAverageType {
75    Simple,
76    Exponential,
77    DoubleExponential,
78    Wilder,
79    Hull,
80}
81
82#[derive(Debug)]
83pub struct MovingAverageFactory;
84
85impl MovingAverageFactory {
86    #[must_use]
87    #[rustfmt::skip]
88    pub fn create(
89        moving_average_type: MovingAverageType,
90        period: usize,
91    ) -> Box<dyn MovingAverage + Send + Sync> {
92        let price_type = Some(PriceType::Last);
93
94        match moving_average_type {
95            MovingAverageType::Simple => Box::new(SimpleMovingAverage::new(period, price_type)),
96            MovingAverageType::Exponential => Box::new(ExponentialMovingAverage::new(period, price_type)),
97            MovingAverageType::DoubleExponential => Box::new(DoubleExponentialMovingAverage::new(period, price_type)),
98            MovingAverageType::Wilder => Box::new(WilderMovingAverage::new(period, price_type)),
99            MovingAverageType::Hull => Box::new(HullMovingAverage::new(period, price_type)),
100        }
101    }
102}