This tool is designed to measure the flow of money into and out of a specific asset over a set period. Essentially, it helps traders understand the strength behind price movements by looking at both the price changes and the volume of trading activity occurring simultaneously.
How the Logic Works
The indicator operates by analyzing "Typical Price," which is a simple average calculated from the highest, lowest, and closing prices of each candle. By using this average rather than just the closing price, the tool provides a more balanced view of how the market moved during a specific timeframe.
To determine the direction of money flow, the script performs the following steps:
- Comparison: It looks back at a predefined number of periods (the default is 14) and compares the current Typical Price against the previous one.
- Directional Scoring:
- If the price moves upward, the volume from that period is considered "Positive Money Flow."
- If the price moves downward, that same volume is counted as "Negative Money Flow."
- Cumulative Weighting: The script sums up all the positive and negative flows over the chosen timeframe. By multiplying the volume by the price, it ensures that high-volume moves have a larger impact on the final result than low-volume moves.
Reading the Results
The final output is displayed as a line on a scale from 0 to 100:
- Momentum Strength: A higher value indicates that more money is flowing into the asset (buying pressure), while a lower value suggests more money is flowing out (selling pressure).
- Overbought and Oversold Levels: The tool includes specific visual markers at the 20 and 80 levels. When the line reaches these extremes, it typically signals that the asset may be significantly overbought or oversold, potentially hinting at a trend reversal.
In short, this script filters out the "noise" of minor price fluctuations to show you whether the heavy hitters in the market are currently pushing the price up or pulling it down.
//+------------------------------------------------------------------+
//| 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=14;
//---- 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