1use std::collections::VecDeque;
31
32use crate::error::IndicatorError;
33use crate::types::MacdResult;
34
35pub fn ema(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
40 if period == 0 {
41 return Err(IndicatorError::InvalidParameter {
42 name: "period".into(),
43 value: 0.0,
44 });
45 }
46 if prices.len() < period {
47 return Err(IndicatorError::InsufficientData {
48 required: period,
49 available: prices.len(),
50 });
51 }
52 let mut result = vec![f64::NAN; prices.len()];
53 let alpha = 2.0 / (period as f64 + 1.0);
54 let first_sma: f64 = prices.iter().take(period).sum::<f64>() / period as f64;
55 result[period - 1] = first_sma;
56 for i in period..prices.len() {
57 result[i] = prices[i] * alpha + result[i - 1] * (1.0 - alpha);
58 }
59 Ok(result)
60}
61
62pub fn ema_nan_aware(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
74 if period == 0 {
75 return Err(IndicatorError::InvalidParameter {
76 name: "period".into(),
77 value: 0.0,
78 });
79 }
80 let mut result = vec![f64::NAN; prices.len()];
81 let alpha = 2.0 / (period as f64 + 1.0);
82
83 let Some(start) = prices.iter().position(|v| !v.is_nan()) else {
85 return Ok(result); };
87
88 result[start] = prices[start];
89 for i in (start + 1)..prices.len() {
90 result[i] = if prices[i].is_nan() {
91 f64::NAN
92 } else {
93 prices[i] * alpha + result[i - 1] * (1.0 - alpha)
94 };
95 }
96 Ok(result)
97}
98
99pub fn sma(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
101 if period == 0 {
102 return Err(IndicatorError::InvalidParameter {
103 name: "period".into(),
104 value: 0.0,
105 });
106 }
107 if prices.len() < period {
108 return Err(IndicatorError::InsufficientData {
109 required: period,
110 available: prices.len(),
111 });
112 }
113 let mut result = vec![f64::NAN; prices.len()];
114 for i in (period - 1)..prices.len() {
115 let sum: f64 = prices[(i + 1 - period)..=i].iter().sum();
116 result[i] = sum / period as f64;
117 }
118 Ok(result)
119}
120
121pub fn true_range(high: &[f64], low: &[f64], close: &[f64]) -> Result<Vec<f64>, IndicatorError> {
123 if high.len() != low.len() || high.len() != close.len() {
124 return Err(IndicatorError::InsufficientData {
125 required: high.len(),
126 available: low.len().min(close.len()),
127 });
128 }
129 let mut result = vec![f64::NAN; high.len()];
130 if !high.is_empty() {
131 result[0] = high[0] - low[0];
132 }
133 for i in 1..high.len() {
134 let tr1 = high[i] - low[i];
135 let tr2 = (high[i] - close[i - 1]).abs();
136 let tr3 = (low[i] - close[i - 1]).abs();
137 result[i] = tr1.max(tr2).max(tr3);
138 }
139 Ok(result)
140}
141
142pub fn atr(
148 high: &[f64],
149 low: &[f64],
150 close: &[f64],
151 period: usize,
152) -> Result<Vec<f64>, IndicatorError> {
153 let tr = true_range(high, low, close)?;
154 ema(&tr, period)
155}
156
157pub fn rsi(prices: &[f64], period: usize) -> Result<Vec<f64>, IndicatorError> {
159 if prices.len() < period + 1 {
160 return Err(IndicatorError::InsufficientData {
161 required: period + 1,
162 available: prices.len(),
163 });
164 }
165 let mut result = vec![f64::NAN; prices.len()];
166 let mut gains = vec![0.0; prices.len()];
167 let mut losses = vec![0.0; prices.len()];
168 for i in 1..prices.len() {
169 let change = prices[i] - prices[i - 1];
170 if change > 0.0 {
171 gains[i] = change;
172 } else {
173 losses[i] = -change;
174 }
175 }
176 let avg_gains = ema(&gains, period)?;
177 let avg_losses = ema(&losses, period)?;
178 for i in period..prices.len() {
179 if avg_losses[i] == 0.0 {
180 result[i] = 100.0;
181 } else {
182 let rs = avg_gains[i] / avg_losses[i];
183 result[i] = 100.0 - (100.0 / (1.0 + rs));
184 }
185 }
186 Ok(result)
187}
188
189pub fn macd(
191 prices: &[f64],
192 fast_period: usize,
193 slow_period: usize,
194 signal_period: usize,
195) -> MacdResult {
196 let fast_ema = ema_nan_aware(prices, fast_period)?;
199 let slow_ema = ema_nan_aware(prices, slow_period)?;
200 let mut macd_line = vec![f64::NAN; prices.len()];
201 for i in 0..prices.len() {
202 if !fast_ema[i].is_nan() && !slow_ema[i].is_nan() {
203 macd_line[i] = fast_ema[i] - slow_ema[i];
204 }
205 }
206 let signal_line = ema_nan_aware(&macd_line, signal_period)?;
210 let mut histogram = vec![f64::NAN; prices.len()];
211 for i in 0..prices.len() {
212 if !macd_line[i].is_nan() && !signal_line[i].is_nan() {
213 histogram[i] = macd_line[i] - signal_line[i];
214 }
215 }
216 Ok((macd_line, signal_line, histogram))
217}
218
219#[derive(Debug, Clone)]
228pub struct EMA {
229 period: usize,
230 alpha: f64,
231 value: f64,
232 initialized: bool,
233 warmup: VecDeque<f64>,
234}
235
236impl EMA {
237 pub fn new(period: usize) -> Self {
238 Self {
239 period,
240 alpha: 2.0 / (period as f64 + 1.0),
241 value: 0.0,
242 initialized: false,
243 warmup: VecDeque::with_capacity(period),
244 }
245 }
246
247 pub fn update(&mut self, price: f64) {
248 if !self.initialized {
249 self.warmup.push_back(price);
250 if self.warmup.len() >= self.period {
251 self.value = self.warmup.iter().sum::<f64>() / self.period as f64;
252 self.initialized = true;
253 self.warmup.clear();
254 }
255 } else {
256 self.value = price * self.alpha + self.value * (1.0 - self.alpha);
257 }
258 }
259
260 pub fn value(&self) -> f64 {
261 if self.initialized {
262 self.value
263 } else {
264 f64::NAN
265 }
266 }
267
268 pub fn is_ready(&self) -> bool {
269 self.initialized
270 }
271
272 pub fn reset(&mut self) {
273 self.value = 0.0;
274 self.initialized = false;
275 self.warmup.clear();
276 }
277}
278
279#[derive(Debug, Clone)]
281pub struct ATR {
282 #[allow(dead_code)]
283 period: usize,
284 ema: EMA,
285 prev_close: Option<f64>,
286}
287
288impl ATR {
289 pub fn new(period: usize) -> Self {
290 Self {
291 period,
292 ema: EMA::new(period),
293 prev_close: None,
294 }
295 }
296
297 pub fn update(&mut self, high: f64, low: f64, close: f64) {
298 let tr = if let Some(prev) = self.prev_close {
299 (high - low)
300 .max((high - prev).abs())
301 .max((low - prev).abs())
302 } else {
303 high - low
304 };
305 self.ema.update(tr);
306 self.prev_close = Some(close);
307 }
308
309 pub fn value(&self) -> f64 {
310 self.ema.value()
311 }
312
313 pub fn is_ready(&self) -> bool {
314 self.ema.is_ready()
315 }
316}
317
318#[derive(Debug, Clone)]
320pub struct StrategyIndicators {
321 pub ema_fast: Vec<f64>,
322 pub ema_slow: Vec<f64>,
323 pub atr: Vec<f64>,
324}
325
326#[derive(Debug, Clone)]
328pub struct IndicatorCalculator {
329 pub fast_ema_period: usize,
330 pub slow_ema_period: usize,
331 pub atr_period: usize,
332}
333
334impl Default for IndicatorCalculator {
335 fn default() -> Self {
336 Self {
337 fast_ema_period: 8,
338 slow_ema_period: 21,
339 atr_period: 14,
340 }
341 }
342}
343
344impl IndicatorCalculator {
345 pub fn new(fast_ema: usize, slow_ema: usize, atr_period: usize) -> Self {
346 Self {
347 fast_ema_period: fast_ema,
348 slow_ema_period: slow_ema,
349 atr_period,
350 }
351 }
352
353 pub fn calculate_all(
354 &self,
355 close: &[f64],
356 high: &[f64],
357 low: &[f64],
358 ) -> Result<StrategyIndicators, IndicatorError> {
359 Ok(StrategyIndicators {
360 ema_fast: ema(close, self.fast_ema_period)?,
361 ema_slow: ema(close, self.slow_ema_period)?,
362 atr: atr(high, low, close, self.atr_period)?,
363 })
364 }
365}
366
367#[derive(Debug, Clone)]
373pub struct IncrementalEma {
374 alpha: f64,
375 state: f64,
376 initialized: bool,
377}
378
379impl IncrementalEma {
380 pub fn new(period: usize) -> Self {
382 Self {
383 alpha: 2.0 / (period as f64 + 1.0),
384 state: 0.0,
385 initialized: false,
386 }
387 }
388
389 pub fn update(&mut self, price: f64) -> Option<f64> {
393 Some(self.step(price))
394 }
395
396 fn step(&mut self, price: f64) -> f64 {
398 if !self.initialized {
399 self.state = price;
400 self.initialized = true;
401 } else {
402 self.state = self.alpha * price + (1.0 - self.alpha) * self.state;
403 }
404 self.state
405 }
406
407 pub fn current(&self) -> Option<f64> {
409 if self.initialized {
410 Some(self.state)
411 } else {
412 None
413 }
414 }
415}
416
417pub struct IncrementalAtr {
422 ema: IncrementalEma,
423 prev_close: Option<f64>,
424}
425
426impl IncrementalAtr {
427 pub fn new(period: usize) -> Self {
429 Self {
430 ema: IncrementalEma::new(period),
431 prev_close: None,
432 }
433 }
434
435 pub fn update(&mut self, high: f64, low: f64, close: f64) -> Option<f64> {
437 let tr = if let Some(prev) = self.prev_close {
438 let tr1 = high - low;
439 let tr2 = (high - prev).abs();
440 let tr3 = (low - prev).abs();
441 tr1.max(tr2).max(tr3)
442 } else {
443 high - low
444 };
445
446 self.prev_close = Some(close);
447 Some(self.ema.step(tr))
448 }
449}
450
451#[derive(Debug, Clone)]
460pub struct IncrementalRsi {
461 gain_ema: IncrementalEma,
462 loss_ema: IncrementalEma,
463 prev_price: Option<f64>,
464}
465
466impl IncrementalRsi {
467 pub fn new(period: usize) -> Self {
469 Self {
470 gain_ema: IncrementalEma::new(period),
471 loss_ema: IncrementalEma::new(period),
472 prev_price: None,
473 }
474 }
475
476 pub fn update(&mut self, price: f64) -> Option<f64> {
479 let prev = self.prev_price.replace(price)?;
480 let change = price - prev;
481 let (gain, loss) = if change > 0.0 {
482 (change, 0.0)
483 } else {
484 (0.0, -change)
485 };
486 let avg_gain = self.gain_ema.step(gain);
487 let avg_loss = self.loss_ema.step(loss);
488 let rsi = if avg_loss == 0.0 {
489 100.0
490 } else {
491 let rs = avg_gain / avg_loss;
492 100.0 - 100.0 / (1.0 + rs)
493 };
494 Some(rsi)
495 }
496}
497
498#[derive(Debug, Clone)]
506pub struct IncrementalMacd {
507 fast: IncrementalEma,
508 slow: IncrementalEma,
509 signal: IncrementalEma,
510}
511
512impl IncrementalMacd {
513 pub fn new(fast_period: usize, slow_period: usize, signal_period: usize) -> Self {
515 Self {
516 fast: IncrementalEma::new(fast_period),
517 slow: IncrementalEma::new(slow_period),
518 signal: IncrementalEma::new(signal_period),
519 }
520 }
521
522 pub fn update(&mut self, price: f64) -> Option<(f64, f64, f64)> {
526 let macd = self.fast.step(price) - self.slow.step(price);
527 let signal = self.signal.step(macd);
528 Some((macd, signal, macd - signal))
529 }
530}
531
532#[derive(Debug, Clone, Copy, PartialEq)]
534pub struct BollingerBandsValue {
535 pub middle: f64,
537 pub upper: f64,
539 pub lower: f64,
541 pub bandwidth: f64,
543 pub percent_b: f64,
545}
546
547#[derive(Debug, Clone)]
556pub struct IncrementalBollinger {
557 window: VecDeque<f64>,
558 period: usize,
559 std_mult: f64,
560}
561
562impl IncrementalBollinger {
563 pub fn new(period: usize, std_mult: f64) -> Self {
566 Self {
567 window: VecDeque::with_capacity(period.max(1)),
568 period,
569 std_mult,
570 }
571 }
572
573 pub fn update(&mut self, price: f64) -> Option<BollingerBandsValue> {
576 self.window.push_back(price);
577 if self.window.len() > self.period {
578 self.window.pop_front();
579 }
580 if self.window.len() < self.period || self.period < 2 {
581 return None;
582 }
583
584 let mean: f64 = self.window.iter().sum::<f64>() / self.period as f64;
585 let var: f64 =
587 self.window.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / (self.period - 1) as f64;
588 let std = var.sqrt();
589
590 let upper = mean + self.std_mult * std;
591 let lower = mean - self.std_mult * std;
592 let bandwidth = if mean == 0.0 {
593 f64::NAN
594 } else {
595 (upper - lower) / mean
596 };
597 let band_range = upper - lower;
598 let percent_b = if band_range == 0.0 {
599 f64::NAN
600 } else {
601 (price - lower) / band_range
602 };
603
604 Some(BollingerBandsValue {
605 middle: mean,
606 upper,
607 lower,
608 bandwidth,
609 percent_b,
610 })
611 }
612}
613
614#[cfg(test)]
615mod tests {
616 use super::*;
617
618 #[test]
619 fn test_incremental_bollinger_warmup_then_value() {
620 let mut bb = IncrementalBollinger::new(5, 2.0);
621 for p in [10.0, 11.0, 12.0, 13.0] {
622 assert!(bb.update(p).is_none(), "no value before `period` samples");
623 }
624 assert!(bb.update(14.0).is_some(), "value once the window is full");
625 }
626
627 #[test]
628 fn test_incremental_bollinger_constant_prices_zero_width() {
629 let mut bb = IncrementalBollinger::new(4, 2.0);
630 let mut last = None;
631 for _ in 0..4 {
632 last = bb.update(10.0);
633 }
634 let v = last.unwrap();
635 assert!((v.middle - 10.0).abs() < 1e-12);
636 assert!((v.upper - 10.0).abs() < 1e-12);
637 assert!((v.lower - 10.0).abs() < 1e-12);
638 assert!((v.bandwidth - 0.0).abs() < 1e-12);
639 assert!(v.percent_b.is_nan(), "zero-width band → %b undefined");
640 }
641
642 #[test]
643 fn test_incremental_bollinger_matches_sample_stddev() {
644 let mut bb = IncrementalBollinger::new(8, 2.0);
646 let mut last = None;
647 for p in [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0] {
648 last = bb.update(p);
649 }
650 let v = last.unwrap();
651 let std = (32.0_f64 / 7.0).sqrt();
652 assert!((v.middle - 5.0).abs() < 1e-9);
653 assert!((v.upper - (5.0 + 2.0 * std)).abs() < 1e-9);
654 assert!((v.lower - (5.0 - 2.0 * std)).abs() < 1e-9);
655 }
656
657 #[test]
658 fn test_incremental_rsi_first_sample_is_none() {
659 let mut rsi = IncrementalRsi::new(14);
660 assert_eq!(rsi.update(10.0), None);
661 assert!(rsi.update(11.0).is_some());
662 }
663
664 #[test]
665 fn test_incremental_rsi_all_gains_saturates_at_100() {
666 let mut rsi = IncrementalRsi::new(14);
667 let mut last = None;
668 for p in [10.0, 11.0, 12.0, 13.0, 14.0, 15.0] {
669 last = rsi.update(p);
670 }
671 assert!((last.unwrap() - 100.0).abs() < 1e-9);
673 }
674
675 #[test]
676 fn test_incremental_rsi_stays_in_bounds() {
677 let mut rsi = IncrementalRsi::new(5);
678 let prices = [44.0, 44.3, 44.1, 44.2, 43.6, 44.3, 44.8, 45.0, 44.7, 44.9];
679 let mut produced = 0;
680 for p in prices {
681 if let Some(v) = rsi.update(p) {
682 assert!((0.0..=100.0).contains(&v), "RSI out of bounds: {v}");
683 produced += 1;
684 }
685 }
686 assert_eq!(produced, prices.len() - 1);
687 }
688
689 #[test]
690 fn test_incremental_macd_composes_like_batch() {
691 let mut m = IncrementalMacd::new(12, 26, 9);
692 let mut fast = IncrementalEma::new(12);
693 let mut slow = IncrementalEma::new(26);
694 let mut sig = IncrementalEma::new(9);
695 for p in [10.0, 11.0, 10.5, 12.0, 13.0, 12.5, 11.0, 11.5] {
696 let (macd, signal, hist) = m.update(p).unwrap();
697 let expect_macd = fast.update(p).unwrap() - slow.update(p).unwrap();
698 let expect_sig = sig.update(expect_macd).unwrap();
699 assert!((macd - expect_macd).abs() < 1e-12);
700 assert!((signal - expect_sig).abs() < 1e-12);
701 assert!((hist - (expect_macd - expect_sig)).abs() < 1e-12);
702 }
703 }
704
705 #[test]
706 fn test_ema_sma_seed() {
707 let prices = vec![22.27, 22.19, 22.08, 22.17, 22.18];
708 let result = ema(&prices, 5).unwrap();
709 let expected = (22.27 + 22.19 + 22.08 + 22.17 + 22.18) / 5.0;
710 assert!((result[4] - expected).abs() < 1e-9);
711 }
712
713 #[test]
714 fn test_true_range_first() {
715 let h = vec![50.0, 52.0];
716 let l = vec![48.0, 49.0];
717 let c = vec![49.0, 51.0];
718 let tr = true_range(&h, &l, &c).unwrap();
719 assert_eq!(tr[0], 2.0);
720 assert_eq!(tr[1], 3.0);
721 }
722
723 #[test]
724 fn test_ema_incremental() {
725 let mut e = EMA::new(3);
726 e.update(10.0);
727 assert!(!e.is_ready());
728 e.update(20.0);
729 assert!(!e.is_ready());
730 e.update(30.0);
731 assert!(e.is_ready());
732 assert!((e.value() - 20.0).abs() < 1e-9);
733 }
734
735 #[test]
736 fn test_incremental_ema_returns_value() {
737 let mut e = IncrementalEma::new(3); assert_eq!(e.current(), None);
739 assert_eq!(e.update(10.0), Some(10.0)); assert_eq!(e.current(), Some(10.0));
741 let v = e.update(20.0).unwrap(); assert!((v - 15.0).abs() < 1e-9);
743 }
744
745 #[test]
746 fn test_incremental_atr_first_is_range() {
747 let mut a = IncrementalAtr::new(3);
748 assert_eq!(a.update(12.0, 10.0, 11.0), Some(2.0));
750 }
751}