Fractals+3
Download scriptThis script is designed to identify and display "Fractals" on a price chart. In trading, a fractal represents a local peak or valley where the price moves in a specific pattern, highlighting potential areas of support and resistance. The tool automatically scans historical price data to find these points and marks them with visual symbols on your chart.
How the Logic Works
The script operates by scanning the "highs" and "lows" of recent candlesticks to identify two distinct types of formations:
1. Upward Fractals (Resistance Points) The logic looks for a price peak. It identifies a point as an "Up Fractal" when the high price of a specific candle is higher than the highs of the candles surrounding it. Essentially, it confirms that the price reached a local ceiling before moving downward. When this condition is met, the script places a red arrow above that price point.
2. Downward Fractals (Support Points) Conversely, the logic searches for price troughs. It identifies a "Down Fractal" when the low price of a specific candle is lower than the lows of its neighboring candles. This indicates a local floor where the price stopped dropping before moving upward. The script then places a blue arrow below that price point.
Smart Pattern Recognition
Rather than looking for a single simple shape, this script is quite sophisticated in how it defines these peaks and valleys. It accounts for several different types of formations:
- Standard 5-Bar Patterns: It identifies the most basic fractal where one high/low is surrounded by two candles on each side.
- Extended Multi-Bar Patterns: The logic is programmed to recognize more complex "flat" tops and bottoms. This means it can detect fractals even if multiple consecutive candles share the exact same high or low price (patterns spanning 6, 7, 8, or 9 bars).
Visual Execution
To ensure these markers do not clutter the actual price lines, the script includes a customizable "offset" setting. This pushes the arrows slightly above the highs and below the lows, ensuring they are clearly visible to the trader without overlapping the candlesticks.
By automating this detection, the script provides traders with a clear visual map of market structure, highlighting where the price has historically pivoted between upward and downward trends.
//+------------------------------------------------------------------+
//| Fractals.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_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- input parameters
extern int otstup=3;
//---- buffers
double dUpFractalsBuffer[];
double dDownFractalsBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
SetIndexBuffer(0,dUpFractalsBuffer);
SetIndexBuffer(1,dDownFractalsBuffer);
//---- drawing settings
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,119);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,119);
//----
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
//---- name for DataWindow
SetIndexLabel(0,"Fractal Up");
SetIndexLabel(1,"Fractal Down");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,nCountedBars;
nCountedBars=IndicatorCounted();
//---- check for possible errors
if(nCountedBars<0) return(-1);
//---- last counted bar will be recounted
if(nCountedBars<=2)
i=Bars-nCountedBars-3;
if(nCountedBars>2)
{
nCountedBars--;
i=Bars-nCountedBars-1;
}
while(i>=2)
{
//----Up and Down Fractals
//----5 bars Fractal
if(High[i]>High[i+1] && High[i]>High[i+2] && High[i]>High[i-1] && High[i]>High[i-2])
dUpFractalsBuffer[i]= High[i]+otstup*Point;
if(Low[i]<Low[i+1] && Low[i]<Low[i+2] && Low[i]<Low[i-1] && Low[i]<Low[i-2])
{
dDownFractalsBuffer[i]= Low[i]-otstup*Point;
i--;
continue;
}
//----6 bars Fractal
if((Bars-i-1)>=3)
{
if(High[i]==High[i+1] && High[i]>High[i+2] && High[i]>High[i+3] && High[i]>High[i-1] && High[i]>High[i-2])
dUpFractalsBuffer[i]= High[i]+otstup*Point;
if(Low[i]==Low[i+1] && Low[i]<Low[i+2] && Low[i]<Low[i+3] && Low[i]<Low[i-1] && Low[i]<Low[i-2])
{
dDownFractalsBuffer[i]= Low[i]-otstup*Point;
i--;
continue;
}
}
//----7 bars Fractal
if((Bars-i-1)>=4)
{
if(High[i]>=High[i+1] && High[i]==High[i+2] && High[i]>High[i+3] && High[i]>High[i+4] && High[i]>High[i-1] &&
High[i]>High[i-2])
dUpFractalsBuffer[i]= High[i]+otstup*Point;
if(Low[i]<=Low[i+1] && Low[i]==Low[i+2] && Low[i]<Low[i+3] && Low[i]<Low[i+4] && Low[i]<Low[i-1] &&
Low[i]<Low[i-2])
{
dDownFractalsBuffer[i]= Low[i]-otstup*Point;
i--;
continue;
}
}
//----8 bars Fractal
if((Bars-i-1)>=5)
{
if(High[i]>=High[i+1] && High[i]==High[i+2] && High[i]==High[i+3] && High[i]>High[i+4] && High[i]>High[i+5] &&
High[i]>High[i-1] && High[i]>High[i-2])
dUpFractalsBuffer[i]= High[i]+otstup*Point;
if(Low[i]<=Low[i+1] && Low[i]==Low[i+2] && Low[i]==Low[i+3] && Low[i]<Low[i+4] && Low[i]<Low[i+5] &&
Low[i]<Low[i-1] && Low[i]<Low[i-2])
{
dDownFractalsBuffer[i]= Low[i]-otstup*Point;
i--;
continue;
}
}
//----9 bars Fractal
if((Bars-i-1)>=6)
{
if(High[i]>=High[i+1] && High[i]==High[i+2] && High[i]>=High[i+3] && High[i]==High[i+4] && High[i]>High[i+5] &&
High[i]>High[i+6] && High[i]>High[i-1] && High[i]>High[i-2])
dUpFractalsBuffer[i]= High[i]+otstup*Point;
if(Low[i]<=Low[i+1] && Low[i]==Low[i+2] && Low[i]<=Low[i+3] && Low[i]==Low[i+4] && Low[i]<Low[i+5] &&
Low[i]<Low[i+6] && Low[i]<Low[i-1] && Low[i]<Low[i-2])
{
dDownFractalsBuffer[i]= Low[i]-otstup*Point;
i--;
continue;
}
}
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments