Money Flow Index (MFI): A Volume-Weighted RSI for Sophisticated Traders
Introduction to the Money Flow Index (MFI)
The Money Flow Index (MFI) is a momentum indicator that measures the flow of money into and out of a security over a specified period. It is related to the Relative Strength Index (RSI), but incorporates volume, making it a volume-weighted RSI. The MFI is used to identify overbought and oversold conditions, as well as to spot divergences that may signal a trend reversal.
This article will guide you through the calculation of the MFI, its implementation using NumPy, and how to apply SciPy for a more in-depth statistical analysis of this effective indicator.
The Mathematical Formulation of MFI
The MFI calculation involves several steps:
-
Typical Price: First, calculate the typical price for each period:
Typical Price = (High + Low + Close) / 3Typical Price = (High + Low + Close) / 3 -
Raw Money Flow: Then, calculate the raw money flow:
Raw Money Flow = Typical Price * VolumeRaw Money Flow = Typical Price * Volume -
Positive and Negative Money Flow: Separate the money flow into positive and negative money flow. Positive money flow is the sum of raw money flow for periods where the typical price is higher than the previous period's typical price. Negative money flow is the sum of raw money flow for periods where the typical price is lower.
-
Money Flow Ratio and MFI: Finally, calculate the money flow ratio and the MFI:
Money Flow Ratio = Positive Money Flow / Negative Money Flow MFI = 100 - (100 / (1 + Money Flow Ratio))Money Flow Ratio = Positive Money Flow / Negative Money Flow MFI = 100 - (100 / (1 + Money Flow Ratio))
Implementing MFI with NumPy
NumPy's array operations are well-suited for the efficient calculation of the MFI. Here is a Python function that demonstrates this:
import numpy as np
def calculate_mfi(high, low, close, volume, n=14):
typical_price = (high + low + close) / 3
raw_money_flow = typical_price * volume
positive_money_flow = np.zeros_like(raw_money_flow)
negative_money_flow = np.zeros_like(raw_money_flow)
for i in range(1, len(typical_price)):
if typical_price[i] > typical_price[i-1]:
positive_money_flow[i] = raw_money_flow[i]
else:
negative_money_flow[i] = raw_money_flow[i]
mfi = np.zeros_like(close, dtype=float)
for i in range(n - 1, len(close)):
sum_positive_mf = np.sum(positive_money_flow[i - n + 1:i + 1])
sum_negative_mf = np.sum(negative_money_flow[i - n + 1:i + 1])
if sum_negative_mf == 0:
mfi[i] = 100
else:
money_flow_ratio = sum_positive_mf / sum_negative_mf
mfi[i] = 100 - (100 / (1 + money_flow_ratio))
return mfi
import numpy as np
def calculate_mfi(high, low, close, volume, n=14):
typical_price = (high + low + close) / 3
raw_money_flow = typical_price * volume
positive_money_flow = np.zeros_like(raw_money_flow)
negative_money_flow = np.zeros_like(raw_money_flow)
for i in range(1, len(typical_price)):
if typical_price[i] > typical_price[i-1]:
positive_money_flow[i] = raw_money_flow[i]
else:
negative_money_flow[i] = raw_money_flow[i]
mfi = np.zeros_like(close, dtype=float)
for i in range(n - 1, len(close)):
sum_positive_mf = np.sum(positive_money_flow[i - n + 1:i + 1])
sum_negative_mf = np.sum(negative_money_flow[i - n + 1:i + 1])
if sum_negative_mf == 0:
mfi[i] = 100
else:
money_flow_ratio = sum_positive_mf / sum_negative_mf
mfi[i] = 100 - (100 / (1 + money_flow_ratio))
return mfi
Statistical Analysis of MFI with SciPy
We can use SciPy to analyze the distribution of MFI values. For example, we can create a histogram of the MFI data and fit a probability distribution to it using the fit function from scipy.stats. This can help in understanding the statistical properties of the MFI and in identifying extreme values.
from scipy.stats import norm
# Assuming 'mfi_data' is a NumPy array of MFI values
mu, std = norm.fit(mfi_data)
from scipy.stats import norm
# Assuming 'mfi_data' is a NumPy array of MFI values
mu, std = norm.fit(mfi_data)
Sample Data and MFI Calculation
Let's consider the following data for a stock:
| Day | High | Low | Close | Volume | MFI (n=3) |
|---|---|---|---|---|---|
| 1 | 102 | 99 | 101 | 10000 | 0.00 |
| 2 | 103 | 100 | 102 | 12000 | 0.00 |
| 3 | 104 | 101 | 103 | 11000 | 100.00 |
| 4 | 103 | 100 | 101 | 13000 | 65.79 |
| 5 | 102 | 99 | 100 | 15000 | 45.45 |
Actionable Trading Examples
-
Overbought/Oversold Levels: The MFI is typically interpreted as overbought when it is above 80 and oversold when it is below 20. These levels can signal potential reversals. A move from above 80 back below it can be a sell signal, while a move from below 20 back above it can be a buy signal.
-
Divergences: As with the RSI, divergences between the MFI and the price can be effective signals. A bearish divergence occurs when the price makes a new high, but the MFI fails to make a new high. A bullish divergence occurs when the price makes a new low, but the MFI makes a higher low.
Conclusion
The Money Flow Index is a valuable indicator that combines price and volume to provide a more comprehensive picture of market momentum. By using NumPy for efficient calculation and SciPy for statistical analysis, traders can develop a deeper understanding of the MFI and incorporate it into their quantitative trading strategies. This data-driven approach can lead to more robust and profitable trading systems.
