1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::;
/// Calculates the lookback period required for RMA calculation.
///
/// Returns the number of data points needed before RMA can start producing valid values.
/// The lookback period equals the period minus 1, since RMA requires a full period of data
/// to calculate the first value.
///
/// # Arguments
/// * `param_period` - The period length used for RMA calculation (must be >= 2)
///
/// # Returns
/// * `Result<usize, KandError>` - The lookback period on success
///
/// # Errors
/// * `KandError::InvalidParameter` - If period is less than 2
///
/// # Examples
/// ```
/// use kand::ohlcv::rma;
/// let period = 14;
/// let lookback = rma::lookback(period).unwrap();
/// assert_eq!(lookback, 13); // lookback is period - 1
/// ```
pub const
/// Calculates the Running Moving Average (RMA) for a price series.
///
/// RMA is a type of moving average that gives more weight to recent prices while still maintaining
/// some influence from all past prices. It is similar to EMA but uses a different smoothing factor.
///
/// # Mathematical Formula
/// ```text
/// RMA = (Current Price * α) + Previous RMA * (1 - α)
/// where α = 1/period
/// ```
///
/// # Calculation Steps
/// 1. Calculate initial SMA value using first `period` prices
/// 2. For remaining values, apply RMA formula using smoothing factor α = 1/period
/// 3. Fill initial values before period with NaN
///
/// # Arguments
/// * `input` - Array of price values to calculate RMA
/// * `param_period` - The smoothing period (must be >= 2)
/// * `output_rma` - Array to store calculated RMA values
///
/// # Returns
/// * `Result<(), KandError>` - Empty result on success
///
/// # Errors
/// * `KandError::InvalidData` - If input array is empty
/// * `KandError::LengthMismatch` - If input and output arrays have different lengths
/// * `KandError::InvalidParameter` - If period is less than 2
/// * `KandError::InsufficientData` - If input length is less than period
/// * `KandError::NaNDetected` - If input contains NaN values (with "`deep-check`" feature)
///
/// # Examples
/// ```
/// use kand::ohlcv::rma;
/// let prices = vec![1.0, 2.0, 3.0, 4.0, 5.0];
/// let period = 3;
/// let mut rma_values = vec![0.0; 5];
/// rma::rma(&prices, period, &mut rma_values).unwrap();
/// ```
/// Calculates a single new RMA value incrementally.
///
/// This function enables real-time RMA calculation by computing the next value
/// using only the current price and previous RMA, without requiring historical data.
///
/// # Mathematical Formula
/// ```text
/// RMA = (Current Price * α) + Previous RMA * (1 - α)
/// where α = 1/period
/// ```
///
/// # Arguments
/// * `input_current` - The current price value
/// * `prev_rma` - The previous RMA value
/// * `param_period` - The smoothing period (must be >= 2)
///
/// # Returns
/// * `Result<TAFloat, KandError>` - The new RMA value on success
///
/// # Errors
/// * `KandError::InvalidParameter` - If period is less than 2
/// * `KandError::NaNDetected` - If any input is NaN (with "`deep-check`" feature)
///
/// # Examples
/// ```
/// use kand::ohlcv::rma;
/// let current_price = 10.0;
/// let prev_rma = 9.5;
/// let period = 14;
/// let new_rma = rma::rma_inc(current_price, prev_rma, period).unwrap();
/// ```