use std::collections::VecDeque;
use crate::bar_indicators::indicator_value::IndicatorValue;
#[derive(Clone)]
pub struct ChaikinMoneyFlow {
period: usize,
money_flow_volumes: VecDeque<f64>,
volumes: VecDeque<f64>,
sum_money_flow: f64,
sum_volume: f64,
cmf_value: f64,
index: usize,
filled: bool,
}
impl ChaikinMoneyFlow {
pub fn new(period: usize) -> Self {
assert!(period > 0 && period <= 512, "Period must be between 1 and 512");
Self {
period,
money_flow_volumes: VecDeque::with_capacity(period),
volumes: VecDeque::with_capacity(period),
sum_money_flow: 0.0,
sum_volume: 0.0,
cmf_value: 0.0,
index: 0,
filled: false,
}
}
pub fn update_bar(&mut self, _open: f64, high: f64, low: f64, close: f64, volume: f64) -> f64 {
let range = high - low;
let money_flow_multiplier = if range.abs() < 1e-12 {
0.0 } else {
((close - low) - (high - close)) / range
};
let money_flow_volume = money_flow_multiplier * volume;
if self.money_flow_volumes.len() >= self.period {
let old_mf = self.money_flow_volumes.pop_front().unwrap();
self.sum_money_flow -= old_mf;
}
self.money_flow_volumes.push_back(money_flow_volume);
self.sum_money_flow += money_flow_volume;
if self.volumes.len() >= self.period {
let old_vol = self.volumes.pop_front().unwrap();
self.sum_volume -= old_vol;
}
self.volumes.push_back(volume);
self.sum_volume += volume;
self.index += 1;
if self.money_flow_volumes.len() >= self.period {
self.filled = true;
}
if self.filled {
self.cmf_value = if self.sum_volume.abs() < 1e-12 {
0.0
} else {
self.sum_money_flow / self.sum_volume
};
}
self.cmf_value
}
pub fn value(&self) -> IndicatorValue {
IndicatorValue::Single(self.cmf_value)
}
pub fn is_ready(&self) -> bool {
self.filled
}
pub fn period(&self) -> usize {
self.period
}
pub fn reset(&mut self) {
self.money_flow_volumes.clear();
self.volumes.clear();
self.sum_money_flow = 0.0;
self.sum_volume = 0.0;
self.cmf_value = 0.0;
self.index = 0;
self.filled = false;
}
pub fn market_condition(&self) -> &'static str {
match self.cmf_value {
v if v >= 0.2 => "Strong Accumulation",
v if v >= 0.05 => "Accumulation",
v if v <= -0.2 => "Strong Distribution",
v if v <= -0.05 => "Distribution",
_ => "Neutral"
}
}
pub fn trading_signal(&self) -> i8 {
if !self.is_ready() {
return 0;
}
match self.cmf_value {
v if v >= 0.1 => 1, v if v <= -0.1 => -1, _ => 0 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chaikin_money_flow_creation() {
let cmf = ChaikinMoneyFlow::new(20);
assert!(!cmf.is_ready());
assert_eq!(cmf.value().main(), 0.0);
}
#[test]
fn test_chaikin_money_flow_warmup() {
let mut cmf = ChaikinMoneyFlow::new(20);
for i in 0..25 {
let price = 100.0 + (i as f64 * 0.1).sin() * 5.0;
cmf.update_bar(price, price + 1.0, price - 1.0, price, 1000.0);
}
assert!(cmf.is_ready());
}
#[test]
fn test_chaikin_money_flow_range() {
let mut cmf = ChaikinMoneyFlow::new(20);
for i in 0..40 {
let price = 100.0 + (i as f64 * 0.2).sin() * 10.0;
let value = cmf.update_bar(price, price + 1.0, price - 1.0, price, 1000.0);
assert!(value >= -1.0 && value <= 1.0, "CMF should be in [-1, 1]");
}
}
#[test]
fn test_chaikin_money_flow_reset() {
let mut cmf = ChaikinMoneyFlow::new(20);
for i in 0..25 {
cmf.update_bar(100.0 + i as f64, 105.0, 95.0, 101.0, 1000.0);
}
cmf.reset();
assert!(!cmf.is_ready());
assert_eq!(cmf.value().main(), 0.0);
}
}