Skip to main content

qtrade_indicators/
lib.rs

1//! Technical analysis indicators for trading.
2/*!
3Each indicator is gated behind a Cargo feature flag so you only compile what you need.
4
5# Feature Flags
6
7| Feature | Module | Description |
8|---------|--------|-------------|
9| `median_price` | `median_price` | `(high + low) / 2` |
10| `tr` | `true_range` | True Range volatility measure |
11| `wf` | `williams_fractals` | Williams Fractals |
12| `sma` | `simple_moving_average` | Simple Moving Average |
13| `smma` | `smoothed_moving_average` | Smoothed Moving Average |
14| `ema` | `exponential_moving_average` | Exponential Moving Average |
15| `rma` | `running_moving_average` | Running Moving Average (Wilder's) |
16| `wma` | `weighted_moving_average` | Weighted Moving Average |
17| `atr` | `average_true_range` | Average True Range (auto-enables tr, sma, smma, ema, rma, wma) |
18| `macd` | `moving_average_convergence_divergence` | MACD (auto-enables ema) |
19| `supertrend` | `supertrend` | Supertrend indicator |
20
21Use the `dev` feature to enable all indicators:
22```toml
23[dependencies]
24qtrade-indicators = { version = "0.1", features = ["dev"] }
25```
26
27# Indicator Enum
28
29The [`Indicator`] enum provides a named identifier for each available indicator.
30
31```rust
32use qtrade_indicators::Indicator;
33
34let name = Indicator::AverageTrueRange.to_string();
35assert_eq!(name, "Average True Range");
36```
37*/
38
39use std::fmt;
40pub mod indicator_error;
41
42pub enum Indicator {
43    MedianPrice,                        // median_price
44    TrueRange,                          // tr
45    WilliamsFractals,                   // wf
46    SimpleMovingAverage,                // sma
47    SmoothedMovingAverage,              // smma
48    ExponentialMovingAverage,           // ema
49    RunningMovingAverage,               // rma
50    WeightedMovingAverage,              // wma
51    AverageTrueRange,                   // atr
52    MovingAverageConvergenceDivergence, // macd
53    Supertrend,                         // supertrend
54}
55impl Indicator {
56    fn as_str(&self) -> &'static str {
57        match self {
58            Indicator::MedianPrice => "Median Price",
59            Indicator::TrueRange => "True Range",
60            Indicator::WilliamsFractals => "Williams Fractals",
61            Indicator::SimpleMovingAverage => "Simple Moving Average",
62            Indicator::SmoothedMovingAverage => "Smoothed Moving Average",
63            Indicator::ExponentialMovingAverage => "Exponential Moving Average",
64            Indicator::RunningMovingAverage => "Running Moving Average",
65            Indicator::WeightedMovingAverage => "Weighted Moving Average",
66            Indicator::AverageTrueRange => "Average True Range",
67            Indicator::MovingAverageConvergenceDivergence => {
68                "Moving Average Convergence Divergence"
69            }
70            Indicator::Supertrend => "Supertrend",
71        }
72    }
73}
74
75impl fmt::Display for Indicator {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        write!(f, "{}", self.as_str())
78    }
79}
80
81#[cfg(feature = "median_price")]
82pub mod median_price;
83
84#[cfg(feature = "tr")]
85pub mod true_range;
86
87#[cfg(feature = "wf")]
88pub mod williams_fractals;
89
90#[cfg(feature = "sma")]
91pub mod simple_moving_average;
92
93#[cfg(feature = "smma")]
94pub mod smoothed_moving_average;
95
96#[cfg(feature = "ema")]
97pub mod exponential_moving_average;
98
99#[cfg(feature = "rma")]
100pub mod running_moving_average;
101
102#[cfg(feature = "wma")]
103pub mod weighted_moving_average;
104
105#[cfg(feature = "atr")]
106pub mod average_true_range;
107
108#[cfg(feature = "macd")]
109pub mod moving_average_convergence_divergence;
110
111#[cfg(feature = "supertrend")]
112pub mod supertrend;