Fractals_T
Download scriptThis script is a technical analysis tool designed to identify specific structural patterns in price movements, known as "Fractals." In trading, these patterns help identify potential turning points where the market might shift from an upward trend to a downward one, or vice versa.
Core Logic and Functionality
The tool scans historical price data to locate significant peaks (highs) and valleys (lows). Rather than just looking for any high or low point, it looks for specific geometric arrangements where a central price point is higher or lower than the candles surrounding it.
How it identifies these points: The script employs a multi-layered logic system that checks for different "widths" of fractal patterns:
- Standard 5-Bar Patterns: It first looks for a simple peak or valley where a single candle's high or low is greater (or lower) than the two candles preceding it and the two candles following it.
- Extended Multi-Bar Patterns: The script is sophisticated enough to recognize more complex structures involving up to 9 consecutive bars. It accounts for "flat" tops or bottoms—situations where multiple consecutive candles share the exact same high or low price before the market reverses.
- Dynamic Detection: By checking these various configurations, the tool ensures it captures both sharp V-shaped reversals and broader, flatter U-shaped structural shifts.
Visual Representation
When the logic identifies one of these patterns, it places a visual marker (an arrow) on your trading chart:
- Upward Fractals (Red): These appear above high price points. They signal areas where the price has reached a local peak relative to its neighbors, potentially indicating a ceiling or an area of resistance.
- Downward Fractals (Blue): These appear below low price points. They signal areas where the price has hit a local floor relative to its neighbors, potentially indicating a base or an area of support.
Customization
The script includes a specific parameter that allows you to control the "offset" or distance between the marker and the actual candle. This ensures that the visual icons do not overlap directly with the price bars, providing a cleaner and more readable view of the market structure.
//+------------------------------------------------------------------+
//| 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
//---- buffers
extern int T=15;
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]+T*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]-T*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]+T*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]-T*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]+T*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]-T*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]+T*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]-T*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]+T*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]-T*Point;
i--;
continue;
}
}
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments