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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use ema;
use crate::;
/// Returns the lookback period required for VEGAS (Volume and EMA Guided Adaptive Scaling) calculation
///
/// # Returns
/// * `Result<usize, KandError>` - The number of data points needed before first valid output (675)
///
/// # Errors
/// * `KandError::InvalidData` - If input data is empty
///
/// # Example
/// ```rust
/// use kand::ohlcv::vegas;
///
/// let lookback = vegas::lookback().unwrap();
/// assert_eq!(lookback, 675);
/// ```
pub const
/// Calculates VEGAS (Volume and EMA Guided Adaptive Scaling) indicator for the entire price array
///
/// # Description
/// VEGAS is a trend following indicator that uses multiple EMAs to define channels and boundaries.
/// It helps identify trend strength and potential trend changes through the spacing between EMAs.
///
/// # Mathematical Formula
/// The indicator consists of 4 EMAs with different periods:
/// ```text
/// Channel Upper = EMA(price, 144)
/// Channel Lower = EMA(price, 169)
/// Boundary Upper = EMA(price, 576)
/// Boundary Lower = EMA(price, 676)
///
/// Where EMA is calculated as:
/// EMA = Price * (2 / (n + 1)) + Previous_EMA * (1 - (2 / (n + 1)))
/// ```
///
/// # Parameters
/// * `input_price` - Array of price values
/// * `output_channel_upper` - Output array for upper channel (EMA 144)
/// * `output_channel_lower` - Output array for lower channel (EMA 169)
/// * `output_boundary_upper` - Output array for upper boundary (EMA 576)
/// * `output_boundary_lower` - Output array for lower boundary (EMA 676)
///
/// # Returns
/// * `Result<(), KandError>` - Ok if calculation succeeds
///
/// # Errors
/// * `KandError::InvalidData` - If input array is empty
/// * `KandError::LengthMismatch` - If output arrays have different lengths than input
/// * `KandError::InsufficientData` - If input length < 676
/// * `KandError::NaNDetected` - If any input value is NaN
///
/// # Example
/// ```rust
/// use kand::ohlcv::vegas;
///
/// let input_price = vec![10.0; 1000];
/// let mut channel_upper = vec![0.0; 1000];
/// let mut channel_lower = vec![0.0; 1000];
/// let mut boundary_upper = vec![0.0; 1000];
/// let mut boundary_lower = vec![0.0; 1000];
///
/// vegas::vegas(
/// &input_price,
/// &mut channel_upper,
/// &mut channel_lower,
/// &mut boundary_upper,
/// &mut boundary_lower,
/// )
/// .unwrap();
/// ```
/// Calculates latest VEGAS indicator values incrementally for real-time updates
///
/// # Description
/// Provides optimized calculation of latest VEGAS values for real-time price updates
/// without recalculating the entire series. This is particularly useful for streaming data.
///
/// # Mathematical Formula
/// ```text
/// For each EMA:
/// EMA_current = Price * multiplier + Previous_EMA * (1 - multiplier)
/// where multiplier = 2 / (period + 1)
/// ```
///
/// # Parameters
/// * `input_price` - Current price value
/// * `prev_channel_upper` - Previous EMA(144) value
/// * `prev_channel_lower` - Previous EMA(169) value
/// * `prev_boundary_upper` - Previous EMA(576) value
/// * `prev_boundary_lower` - Previous EMA(676) value
///
/// # Returns
/// * `Result<(TAFloat,TAFloat,TAFloat,TAFloat), KandError>` - Tuple of (`channel_upper`, `channel_lower`, `boundary_upper`, `boundary_lower`)
///
/// # Errors
/// * `KandError::NaNDetected` - If any input value is NaN
///
/// # Example
/// ```rust
/// use kand::ohlcv::vegas;
///
/// let current_price = 100.0;
/// let prev_values = (98.0, 97.5, 96.0, 95.5);
///
/// let new_values = vegas::vegas_inc(
/// current_price,
/// prev_values.0,
/// prev_values.1,
/// prev_values.2,
/// prev_values.3,
/// )
/// .unwrap();
/// ```