MktFlwAnlMth
Download scriptThis script is designed to perform a "Market Flow Analysis" by tracking the structural history of price movements. Instead of just looking at current price, it identifies and measures the distance (in terms of time/bars) between significant price peaks and valleys.
Core Logic Overview
The primary goal of this tool is to identify "Higher Highs" (HH) and "Lower Lows" (LL) and determine how much time has passed since these critical structural points occurred. It distinguishes between two different types of market milestones:
- General Extremes: The most recent major high or low regardless of what happened before it.
- Contextual Extremes: Highs that occurred specifically after a previous Low, and Lows that occurred specifically after a previous High.
By tracking these specific sequences, the tool helps traders understand the "flow" of the market—identifying whether the price is currently in an expansion phase, a retracement phase, or a period of consolidation.
How It Works
- Tracking Age: For every new candle that forms, the script calculates how many bars have passed since the last major high and low were established. If the current price breaks above a previous high or below a previous low, the "age" resets to zero because a new structural point has been found.
- Contextual Filtering: The logic creates a relationship between highs and lows. For example, it specifically monitors for a High that occurs after a Low was established. This helps filter out noise and focuses on significant structural shifts rather than minor fluctuations.
- The "Max Age" Constraint: To keep the analysis relevant to current market conditions, users can set a "Max Age." If a peak or valley is too old (exceeding this limit), the script will automatically search backward through history to find the most recent valid extreme within that specific timeframe.
- Dynamic Updates: The script constantly updates these measurements in real-time. As price moves, it recalculates the distance to the significant levels, allowing you to see how "old" a current trend is or how far the market has drifted from its last major structural anchor.
Visual Representation
The indicator provides several visual layers on the chart:
- Distance Metrics: It tracks the number of bars since various structural events (Age HH, Age LL, etc.).
- Price Levels: It plots the actual price levels of those significant highs and lows.
Depending on your settings, you can choose to view either the "Age" (the time elapsed) or the actual "Price Levels" of these structural points to better visualize market structure.
//+----------------------------------------+
//| BREW_MktFlwAnlMth.mq4 |
//| Market Flow Analysis Method |
//| http://www.forexforprofits.com |
//+----------------------------------------+
#property copyright "Copyright © 2011, Brewmanz"
#property link "http://www.metaquotes.net/"
//#property indicator_separate_window
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Lime
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color2 Cyan
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_color3 Red
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
#property indicator_color4 Magenta
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
#property indicator_color5 Lime
#property indicator_style5 STYLE_SOLID
#property indicator_width5 2
#property indicator_color6 Cyan
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
#property indicator_color7 Red
#property indicator_style7 STYLE_SOLID
#property indicator_width7 2
#property indicator_color8 Magenta
#property indicator_style8 STYLE_SOLID
#property indicator_width8 1
//#include <LibUtil.mqh>
//---- input parameters
extern string Comment1="Set UseSeparateChartAges to true to show how Highest High etc Bar Aging is working";
extern bool UseSeparateChartAges=false;
extern string Comment2="Set MaxAge to limit how long a Highest High etc is valid for";
extern int MaxAge=999;
//---- display buffers
double buff_BarsSinceLastHH[];
double buff_BarsSinceHHAfterLL[];
double buff_BarsSinceLastLL[];
double buff_BarsSinceLLAfterHH[];
double buff_HH[];
double buff_HAfterL[];
double buff_LL[];
double buff_LAfterH[];
double buff_DBG[];
//---- work buffers
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
IndicatorBuffers(8);
IndicatorDigits(Digits);
//---- indicator line
SetIndexBuffer(0,buff_BarsSinceLastHH);
SetIndexBuffer(1,buff_BarsSinceHHAfterLL);
SetIndexBuffer(2,buff_BarsSinceLastLL);
SetIndexBuffer(3,buff_BarsSinceLLAfterHH);
SetIndexBuffer(4,buff_HH);
SetIndexBuffer(5,buff_HAfterL);
SetIndexBuffer(6,buff_LL);
SetIndexBuffer(7,buff_LAfterH);
if(UseSeparateChartAges)
{
SetIndexStyle( 0,DRAW_LINE);
SetIndexStyle( 1,DRAW_LINE);
SetIndexStyle( 2,DRAW_LINE);
SetIndexStyle( 3,DRAW_LINE);
SetIndexStyle( 4,DRAW_NONE);
SetIndexStyle( 5,DRAW_NONE);
SetIndexStyle( 6,DRAW_NONE);
SetIndexStyle( 7,DRAW_NONE);
}
else
{
SetIndexStyle( 0,DRAW_NONE);
SetIndexStyle( 1,DRAW_NONE);
SetIndexStyle( 2,DRAW_NONE);
SetIndexStyle( 3,DRAW_NONE);
SetIndexStyle( 4,DRAW_LINE);
SetIndexStyle( 5,DRAW_LINE);
SetIndexStyle( 6,DRAW_LINE);
SetIndexStyle( 7,DRAW_LINE);
}
//---- name for DataWindow and indicator subwindow label
short_name="MFAM("
+")";
IndicatorShortName(""+short_name);
SetIndexLabel(0,"AgeHH");
SetIndexLabel(1,"HH_AfterLL");
SetIndexLabel(2,"AgeLL");
SetIndexLabel(3,"LL_AfterHH");
SetIndexLabel(4,"HH");
SetIndexLabel(5,"HAfterL");
SetIndexLabel(6,"LL");
SetIndexLabel(7,"LAfterH");
//----
// SetIndexDrawBegin(0,MaPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
int start()
{
int ix,ixLimit,counted_bars=IndicatorCounted();
//----
if(counted_bars>0)counted_bars--;
ixLimit = Bars-counted_bars-1;
//----
for(ix=ixLimit;ix>=0;ix--)
{
if(ix==Bars-1)
{
// FirstEverBar
buff_BarsSinceLastHH [ix] = 0;
buff_BarsSinceHHAfterLL[ix] = 0;
buff_BarsSinceLastLL [ix] = 0;
buff_BarsSinceLLAfterHH[ix] = 0;
continue;
}
//--- New Bar?
static datetime NewBarTimeCheck = 0;
bool IsNewBar = false;
if(NewBarTimeCheck!=Time[ix])
{
IsNewBar = true;
NewBarTimeCheck = Time[ix];
}
// up age of all bars
int barsToLastHH = 1 + buff_BarsSinceLastHH [ix+1];
int barsToHHSinceLL = 1 + buff_BarsSinceHHAfterLL[ix+1];
int barsToLastLL = 1 + buff_BarsSinceLastLL [ix+1];
int barsToLLSinceHH = 1 + buff_BarsSinceLLAfterHH[ix+1];
// note any extreme price action
if(High[ix] > High[ix+barsToLastHH])
barsToLastHH = 0;
if(High[ix] > High[ix+barsToHHSinceLL])
barsToHHSinceLL = 0;
if(Low[ix] < Low[ix+barsToLastLL])
barsToLastLL = 0;
if(Low[ix] < Low[ix+barsToLLSinceHH])
barsToLLSinceHH = 0;
// check if Max Age has been exceeded
if(barsToLastHH > MaxAge)
barsToLastHH = iHighest(NULL, Period(), MODE_HIGH, MaxAge, ix) - ix;
if(barsToHHSinceLL > MaxAge)
barsToHHSinceLL = iHighest(NULL, Period(), MODE_HIGH, MaxAge, ix) - ix;
if(barsToLastLL > MaxAge)
barsToLastLL = iLowest (NULL, Period(), MODE_LOW, MaxAge, ix) - ix;
if(barsToLLSinceHH > MaxAge)
barsToLLSinceHH = iLowest (NULL, Period(), MODE_LOW, MaxAge, ix) - ix;
// check new HH then we have new Active Lo created
switch(barsToLastHH)
{
case 0:
barsToLastLL = barsToLLSinceHH;
break;
case 1:
barsToLLSinceHH = 0;
break;
}
// check new LL then we have new Active Hi created
switch(barsToLastLL)
{
case 0:
barsToLastHH = barsToHHSinceLL;
break;
case 1:
barsToHHSinceLL = 0;
break;
}
buff_BarsSinceLastHH [ix] = barsToLastHH;
buff_BarsSinceHHAfterLL[ix] = barsToHHSinceLL;
buff_BarsSinceLastLL [ix] = barsToLastLL;
buff_BarsSinceLLAfterHH[ix] = barsToLLSinceHH;
buff_HH [ix] = High[ix + barsToLastHH];
buff_HAfterL[ix] = High[ix + barsToHHSinceLL];
buff_LL [ix] = Low [ix + barsToLastLL];
buff_LAfterH[ix] = Low [ix + barsToLLSinceHH];
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments