Function calculate_mfi

Source
pub fn calculate_mfi(df: &DataFrame, window: usize) -> PolarsResult<Series>
Expand description

Calculates the Money Flow Index (MFI), a volume-weighted version of RSI

MFI is an oscillator that ranges from 0 to 100 and is particularly useful for intraday trading as it combines price and volume to identify overbought or oversold conditions.

§Arguments

  • df - DataFrame containing OHLCV data with “high”, “low”, “close”, and “volume” columns
  • window - Lookback period for calculating the MFI

§Returns

  • PolarsResult<Series> - Series containing MFI values named “mfi_{window}”

§Formula

The MFI is calculated using the following steps:

  1. Calculate the typical price: (high + low + close) / 3
  2. Calculate the money flow: typical price * volume
  3. Compare the typical price with previous period to determine positive or negative flow
  4. Calculate positive and negative money flow sums over the lookback period
  5. Calculate the money ratio: positive money flow / negative money flow
  6. Calculate MFI: 100 - (100 / (1 + money ratio))

§Example

use polars::prelude::*;
use ta_lib_in_rust::indicators::volume::calculate_mfi;

// Create or load a DataFrame with OHLCV data
let df = DataFrame::default(); // Replace with actual data

// Calculate MFI with period 14
let mfi = calculate_mfi(&df, 14).unwrap();