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” columnswindow
- 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:
- Calculate the typical price: (high + low + close) / 3
- Calculate the money flow: typical price * volume
- Compare the typical price with previous period to determine positive or negative flow
- Calculate positive and negative money flow sums over the lookback period
- Calculate the money ratio: positive money flow / negative money flow
- 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();