fract_TFractal[1]
Download scriptThis tool is designed to identify specific price turning points, known as "fractals," on a trading chart. Its primary purpose is to visually highlight significant peaks and troughs in market movement, helping traders recognize structural shifts in price action.
How the Logic Works
The script scans the historical price data of your chart to find instances where the high or low of a specific candle stands out relative to its neighboring candles. It distinguishes between two types of movements:
- Upward Fractals (Peaks): The script looks for points where the market reaches a local high. When it identifies a peak, it places a visual marker (typically an arrow) on the chart.
- Downward Fractals (Troughs): Conversely, it looks for points where the market reaches a local low, identifying "bottoms" in price movement and marking them accordingly.
Identification Methods
Instead of relying on a single strict rule, this script uses a multi-layered detection system to ensure it captures various types of price structures:
- Standard Fractals: The simplest form of detection where a single candle's high is higher than the two candles immediately preceding it and the two candles immediately following it.
- Extended Range Fractals (6, 7, 8, and 9 Bars): To ensure no important structural points are missed, the script includes advanced logic that searches for broader patterns. It checks if a price point acts as a peak or trough across wider windows of time—specifically looking at sequences of up to nine candles back and forward.
Key Features for the User
- Comprehensive Coverage: By checking multiple "bar" lengths (from standard 5-bar structures up to larger 9-bar configurations), the script is more robust than basic fractal indicators, capturing both minor price ripples and major structural turns.
- Visual Clarity: The logic automatically handles the placement of markers on the chart, labeling "Up" fractals and "Down" fractals separately so you can easily distinguish between resistance (tops) and support (bottoms).
- Automated Scanning: The tool automatically traverses the history of the chart to populate these markers, providing a consistent historical perspective on where the market has pivoted in the past.
//+------------------------------------------------------------------+
//| TFractals.mq4 |
//| Copyright © 2009, Titan Global Forex Trading, Inc. |
//| http://www.titanglobalfx.com|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Titan Global Forex Trading, Inc."
#property link "http://www.titanglobalfx.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
//---- input parameters
//---- buffers
double ExtUpFractalsBuffer[];
double ExtDownFractalsBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
SetIndexBuffer(0,ExtUpFractalsBuffer);
SetIndexBuffer(1,ExtDownFractalsBuffer);
//---- drawing settings
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,217);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,218);
//----
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;
bool bFound;
double dCurrent;
nCountedBars=IndicatorCounted();
//---- last counted bar will be recounted
if(nCountedBars<=2)
i=Bars-nCountedBars-3;
if(nCountedBars>2)
{
nCountedBars--;
i=Bars-nCountedBars-1;
}
//----Up and Down Fractals
while(i>=2)
{
//----Fractals up
bFound=false;
dCurrent=High[i];
if(dCurrent>High[i+1] && dCurrent>High[i+2] && dCurrent>High[i-1] && dCurrent>High[i-2])
{
bFound=true;
ExtUpFractalsBuffer[i]=dCurrent;
}
//----6 bars Fractal
if(!bFound && (Bars-i-1)>=3)
{
if(dCurrent==High[i+1] && dCurrent>High[i+2] && dCurrent>High[i+3] &&
dCurrent>High[i-1] && dCurrent>High[i-2])
{
bFound=true;
ExtUpFractalsBuffer[i]=dCurrent;
}
}
//----7 bars Fractal
if(!bFound && (Bars-i-1)>=4)
{
if(dCurrent>=High[i+1] && dCurrent==High[i+2] && dCurrent>High[i+3] && dCurrent>High[i+4] &&
dCurrent>High[i-1] && dCurrent>High[i-2])
{
bFound=true;
ExtUpFractalsBuffer[i]=dCurrent;
}
}
//----8 bars Fractal
if(!bFound && (Bars-i-1)>=5)
{
if(dCurrent>=High[i+1] && dCurrent==High[i+2] && dCurrent==High[i+3] && dCurrent>High[i+4] && dCurrent>High[i+5] &&
dCurrent>High[i-1] && dCurrent>High[i-2])
{
bFound=true;
ExtUpFractalsBuffer[i]=dCurrent;
}
}
//----9 bars Fractal
if(!bFound && (Bars-i-1)>=6)
{
if(dCurrent>=High[i+1] && dCurrent==High[i+2] && dCurrent>=High[i+3] && dCurrent==High[i+4] && dCurrent>High[i+5] &&
dCurrent>High[i+6] && dCurrent>High[i-1] && dCurrent>High[i-2])
{
bFound=true;
ExtUpFractalsBuffer[i]=dCurrent;
}
}
//----Fractals down
bFound=false;
dCurrent=Low[i];
if(dCurrent<Low[i+1] && dCurrent<Low[i+2] && dCurrent<Low[i-1] && dCurrent<Low[i-2])
{
bFound=true;
ExtDownFractalsBuffer[i]=dCurrent;
}
//----6 bars Fractal
if(!bFound && (Bars-i-1)>=3)
{
if(dCurrent==Low[i+1] && dCurrent<Low[i+2] && dCurrent<Low[i+3] &&
dCurrent<Low[i-1] && dCurrent<Low[i-2])
{
bFound=true;
ExtDownFractalsBuffer[i]=dCurrent;
}
}
//----7 bars Fractal
if(!bFound && (Bars-i-1)>=4)
{
if(dCurrent<=Low[i+1] && dCurrent==Low[i+2] && dCurrent<Low[i+3] && dCurrent<Low[i+4] &&
dCurrent<Low[i-1] && dCurrent<Low[i-2])
{
bFound=true;
ExtDownFractalsBuffer[i]=dCurrent;
}
}
//----8 bars Fractal
if(!bFound && (Bars-i-1)>=5)
{
if(dCurrent<=Low[i+1] && dCurrent==Low[i+2] && dCurrent==Low[i+3] && dCurrent<Low[i+4] && dCurrent<Low[i+5] &&
dCurrent<Low[i-1] && dCurrent<Low[i-2])
{
bFound=true;
ExtDownFractalsBuffer[i]=dCurrent;
}
}
//----9 bars Fractal
if(!bFound && (Bars-i-1)>=6)
{
if(dCurrent<=Low[i+1] && dCurrent==Low[i+2] && dCurrent<=Low[i+3] && dCurrent==Low[i+4] && dCurrent<Low[i+5] &&
dCurrent<Low[i+6] && dCurrent<Low[i-1] && dCurrent<Low[i-2])
{
bFound=true;
ExtDownFractalsBuffer[i]=dCurrent;
}
}
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments