Skip to main content

macd

Function macd 

Source
pub fn macd(
    data: &[f64],
    fast_period: usize,
    slow_period: usize,
    signal_period: usize,
) -> Result<MacdResult>
Expand description

Calculate Moving Average Convergence Divergence (MACD).

MACD shows the relationship between two moving averages and helps identify trend changes. Standard parameters are (12, 26, 9).

§Arguments

  • data - Price data (typically close prices)
  • fast_period - Fast EMA period (typically 12)
  • slow_period - Slow EMA period (typically 26)
  • signal_period - Signal line EMA period (typically 9)

§Formula

  • MACD Line = 12-period EMA - 26-period EMA
  • Signal Line = 9-period EMA of MACD Line
  • Histogram = MACD Line - Signal Line

§Example

use finance_query::indicators::macd;

let prices: Vec<f64> = (1..=50).map(|x| x as f64).collect();
let result = macd(&prices, 12, 26, 9).unwrap();

assert_eq!(result.macd_line.len(), prices.len());
assert_eq!(result.signal_line.len(), prices.len());
assert_eq!(result.histogram.len(), prices.len());