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        module = "nautilus_trader.indicators",
66        from_py_object,
67    )
68)]
69#[cfg_attr(
70    feature = "python",
71    pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.indicators")
72)]
73pub enum MovingAverageType {
74    Simple,
75    Exponential,
76    DoubleExponential,
77    Wilder,
78    Hull,
79}
80
81#[derive(Debug)]
82pub struct MovingAverageFactory;
83
84impl MovingAverageFactory {
85    #[must_use]
86    #[rustfmt::skip]
87    pub fn create(
88        moving_average_type: MovingAverageType,
89        period: usize,
90    ) -> Box<dyn MovingAverage + Send + Sync> {
91        let price_type = Some(PriceType::Last);
92
93        match moving_average_type {
94            MovingAverageType::Simple => Box::new(SimpleMovingAverage::new(period, price_type)),
95            MovingAverageType::Exponential => Box::new(ExponentialMovingAverage::new(period, price_type)),
96            MovingAverageType::DoubleExponential => Box::new(DoubleExponentialMovingAverage::new(period, price_type)),
97            MovingAverageType::Wilder => Box::new(WilderMovingAverage::new(period, price_type)),
98            MovingAverageType::Hull => Box::new(HullMovingAverage::new(period, price_type)),
99        }
100    }
101}