Member-only story
How to Calculate MFI in Python
Are you ready to dive into the world of technical indicators? Let’s explore the Money Flow Index (MFI) and learn how to calculate it using Python. This guide will take you through the process, from understanding MFI to coding it yourself.
What is MFI?
The Money Flow Index (MFI) is a powerful technical indicator used by traders. It helps identify overbought or oversold conditions and potential trend reversals by combining price and volume data.
The MFI Formula
Don’t let the MFI formula scare you! We’ll break it down step by step:
- Calculate Typical Price (TP) = (High + Low + Close) / 3
- Calculate Money Flow (MF) = TP * Volume
- Determine Positive and Negative Money Flows
- Calculate Money Flow Ratio (MFR) = Positive MF / Negative MF
- Calculate MFI = 100 — (100 / (1 + MFR))
Coding MFI in Python
Let’s turn this formula into Python code:
import pandas as pd
def calculate_mfi(df, period=20):
TP = (df["high"] + df["low"] + df["close"]) / 3
MoneyFlow = TP * df["volume"]
positive_flow = []
negative_flow = []
for i in range(1, len(TP)):
if TP[i] > TP[i - 1]…









