use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::tokens::DESIGN_TOKENS;
use egui::Color32;
#[derive(Clone)]
pub struct MoneyFlowIndex {
period: usize,
values: Vec<IndicatorValue>,
color: Color32,
visible: bool,
}
impl MoneyFlowIndex {
pub fn new(period: usize) -> Self {
Self {
period: period.max(1),
values: Vec::new(),
color: DESIGN_TOKENS.semantic.extended.bullish,
visible: true,
}
}
pub fn with_color(mut self, color: Color32) -> Self {
self.color = color;
self
}
#[inline]
fn typical_price(bar: &Bar) -> f64 {
(bar.high + bar.low + bar.close) / 3.0
}
}
impl Default for MoneyFlowIndex {
fn default() -> Self {
Self::new(14)
}
}
impl Indicator for MoneyFlowIndex {
fn name(&self) -> &str {
"MFI"
}
fn desc(&self) -> &str {
"Money Flow Index - Volume-weighted RSI (0-100)"
}
fn calculate(&mut self, data: &[Bar]) {
self.values.clear();
if data.len() < self.period + 1 {
for _ in 0..data.len() {
self.values.push(IndicatorValue::None);
}
return;
}
let typical_prices: Vec<f64> = data.iter().map(Self::typical_price).collect();
let raw_money_flows: Vec<f64> = data
.iter()
.zip(typical_prices.iter())
.map(|(bar, tp)| tp * bar.volume)
.collect();
let mut positive_flows = vec![0.0];
let mut negative_flows = vec![0.0];
for i in 1..data.len() {
if typical_prices[i] > typical_prices[i - 1] {
positive_flows.push(raw_money_flows[i]);
negative_flows.push(0.0);
} else if typical_prices[i] < typical_prices[i - 1] {
positive_flows.push(0.0);
negative_flows.push(raw_money_flows[i]);
} else {
positive_flows.push(0.0);
negative_flows.push(0.0);
}
}
for i in 0..data.len() {
if i < self.period {
self.values.push(IndicatorValue::None);
} else {
let start = i + 1 - self.period;
let pos_sum: f64 = positive_flows[start..=i].iter().sum();
let neg_sum: f64 = negative_flows[start..=i].iter().sum();
let mfi = if neg_sum.abs() < 1e-10 {
100.0 } else if pos_sum.abs() < 1e-10 {
0.0 } else {
let ratio = pos_sum / neg_sum;
100.0 - (100.0 / (1.0 + ratio))
};
self.values
.push(IndicatorValue::Single(mfi.clamp(0.0, 100.0)));
}
}
}
fn values(&self) -> &[IndicatorValue] {
&self.values
}
fn colors(&self) -> Vec<Color32> {
vec![self.color]
}
fn set_colors(&mut self, colors: Vec<Color32>) {
if !colors.is_empty() {
self.color = colors[0];
}
}
fn is_overlay(&self) -> bool {
false }
fn is_visible(&self) -> bool {
self.visible
}
fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
fn clone_box(&self) -> Box<dyn Indicator> {
Box::new(self.clone())
}
fn line_names(&self) -> Vec<String> {
vec![format!("MFI({})", self.period)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{Duration, Utc};
fn create_test_bars() -> Vec<Bar> {
let start = Utc::now();
(0..30)
.map(|i| {
let price = 100.0 + (i as f64 * 0.2).sin() * 10.0;
Bar {
time: start + Duration::minutes(i * 5),
open: price,
high: price + 2.0,
low: price - 2.0,
close: price + 0.5,
volume: 1000.0 + (i as f64 * 100.0),
}
})
.collect()
}
#[test]
fn test_mfi_range() {
let bars = create_test_bars();
let mut mfi = MoneyFlowIndex::new(14);
mfi.calculate(&bars);
for value in mfi.values() {
if let IndicatorValue::Single(v) = value {
assert!(*v >= 0.0 && *v <= 100.0, "MFI should be 0-100, got {}", v);
}
}
}
#[test]
fn test_mfi_calculation() {
let bars = create_test_bars();
let mut mfi = MoneyFlowIndex::new(14);
mfi.calculate(&bars);
assert_eq!(mfi.values().len(), bars.len());
for i in 0..14 {
assert!(matches!(mfi.values()[i], IndicatorValue::None));
}
for value in mfi.values().iter().skip(14) {
assert!(matches!(value, IndicatorValue::Single(_)));
}
}
#[test]
fn test_mfi_empty_data() {
let mut mfi = MoneyFlowIndex::new(14);
mfi.calculate(&[]);
assert!(mfi.values().is_empty());
}
}