MFI 1
Download scriptThe Money Flow Index (MFI) is a technical analysis tool designed to measure the "volume" and "momentum" of price movements. Think of it as a way to see not just if a price is moving, but how much trading activity—or "money flow"—is backing up that movement. By combining price action with trading volume, it helps identify whether a trend is being driven by strong conviction or lacks significant backing.
How the Logic Works
The script calculates the index by following these logical steps:
1. Establishing the Price Baseline The tool looks at three specific data points for every candlestick: the High, the Low, and the Closing price. It averages these together to determine a "Typical Price" for that specific period. This provides a more balanced view of the price action than looking at just the closing number alone.
2. Evaluating Momentum via Comparison The script then looks back over a user-defined period (the timeframe set in the settings) to compare current prices against previous ones.
- If the Typical Price is higher than the previous period, it suggests upward momentum.
- If the Typical Price is lower, it suggests downward momentum.
3. Factoring in Trading Volume This is the core of the "Money Flow" logic. The script multiplies the movement by the volume of trades occurring during that time.
- When price moves up on high volume, it adds to a "Positive Money Flow" total.
- When price moves down on high volume, it adds to a "Negative Money Flow" total. By multiplying the price by the volume, the tool gives more weight to price moves that occur during periods of heavy trading activity.
4. Calculating the Final Score Finally, the script takes these two totals—Positive and Negative Money Flow—and converts them into a single percentage value ranging from 0 to 100.
- A high score (closer to 100) indicates that money is flowing aggressively into the asset (buying pressure).
- A low score (closer to 0) indicates that money is flowing out of the asset (selling pressure).
Visual Interpretation
The result is displayed as a line on a separate window below your main chart. Traders typically watch for the line to cross specific thresholds (like 80 or 20) to identify "overbought" or "oversold" conditions, where the flow of money might be reaching an extreme point and potentially reversing.
//+------------------------------------------------------------------+
//| Money Flow Index.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20
#property indicator_level2 80
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int ExtMFIPeriod=1;
//---- buffers
double ExtMFIBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string sShortName;
//----
SetIndexBuffer(0,ExtMFIBuffer);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
//---- name for DataWindow and indicator subwindow label
sShortName="MFI("+ExtMFIPeriod+")";
IndicatorShortName(sShortName);
SetIndexLabel(0,sShortName);
//---- first values aren't drawn
SetIndexDrawBegin(0,ExtMFIPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Money Flow Index |
//+------------------------------------------------------------------+
int start()
{
int i,j,nCountedBars;
double dPositiveMF,dNegativeMF,dCurrentTP,dPreviousTP;
//---- insufficient data
if(Bars<=ExtMFIPeriod) return(0);
//---- bars count that does not changed after last indicator launch.
nCountedBars=IndicatorCounted();
//----
i=Bars-ExtMFIPeriod-1;
if(nCountedBars>ExtMFIPeriod)
i=Bars-nCountedBars-1;
while(i>=0)
{
dPositiveMF=0.0;
dNegativeMF=0.0;
dCurrentTP=(High[i]+Low[i]+Close[i])/3;
for(j=0; j<ExtMFIPeriod; j++)
{
dPreviousTP=(High[i+j+1]+Low[i+j+1]+Close[i+j+1])/3;
if(dCurrentTP>dPreviousTP)
dPositiveMF+=Volume[i+j]*dCurrentTP;
else
{
if(dCurrentTP<dPreviousTP)
dNegativeMF+=Volume[i+j]*dCurrentTP;
}
dCurrentTP=dPreviousTP;
}
//----
if(dNegativeMF!=0.0)
ExtMFIBuffer[i]=100-100/(1+dPositiveMF/dNegativeMF);
else
ExtMFIBuffer[i]=100;
//----
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments