FractalsMod
Download scriptThis tool is designed to identify specific price patterns on a trading chart known as "Fractals." In technical analysis, fractals represent local peaks and valleys in price movement, helping traders visualize potential turning points or areas of significant resistance and support.
Core Logic and Functionality
The script scans through historical price data to find high and low points that stand out relative to their neighbors. Instead of simply looking at the very last candle, it evaluates a specific "window" of candles surrounding any given point to ensure the identified peak or valley is significant.
Key mechanisms include:
- Customizable Range Detection: The logic relies on two primary settings—left bars and right bars. These define how many candles to the left and right of a price point must be compared. For example, if both are set to 10, the script ensures that a high point is higher than the 10 candles preceding it and the 10 candles following it before marking it as a "Fractal Up."
- Dual-Direction Identification: The tool simultaneously tracks two types of movements:
- Upper Fractals (Peaks): It identifies points where the price has reached a maximum height compared to its surrounding neighbors.
- Lower Fractals (Valleys): It identifies points where the price has dropped to a minimum depth compared to its surrounding neighbors.
- Visual Mapping: Once these conditions are met, the script places visual markers (arrows) on the chart. This allows a user to see exactly where the market reached a local extreme, making it easier to spot structural changes in the price trend.
- Real-Time Feedback: The tool includes a dynamic display feature that provides the specific price values for the most recent identified "Up" and "Down" fractals directly on the screen, allowing for quick reference without needing to hover over the markers.
Summary for the User
Essentially, this script automates the tedious task of manually hunting for price peaks and valleys. By looking at a defined neighborhood of candles (the "left" and "right" bars), it filters out minor market noise and highlights only the most prominent structural turning points on your chart.
//+------------------------------------------------------------------+
//| FractalsMod.mq4 |
//| Manel Sanchon |
//| |
//+------------------------------------------------------------------+
#property copyright "Manel Sanchon"
#property link ""
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- input parameters
extern int leftbars = 10;
extern int rightbars = 10;
extern int shift = 0;
//---- buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
SetIndexBuffer(0,ExtUpperBuffer);
SetIndexBuffer(1,ExtLowerBuffer);
//---- drawing settings
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,108);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,108);
//----
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()
{
ObjectsDeleteAll();
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double Higher,Lower;
int countup=0;
int countdown=0;
int counted_bars=IndicatorCounted();
if(counted_bars < 0) return(-1);
if(counted_bars>0) counted_bars--;
int CalculateBars=Bars-counted_bars;
if(counted_bars==0) CalculateBars-=1+leftbars;
for(int Count=CalculateBars; Count>=0; Count--)
{
for(int j=1;j<=leftbars;j++)
{
if(Count+j<CalculateBars)
{
if(High[Count]>High[Count+j]) countup=countup+1;
if(Low[Count]<Low[Count+j]) countdown=countdown+1;
}
}
for(j=1;j<=rightbars;j++)
{
if(Count-j>=0)
{
if(High[Count]>High[Count-j]) countup=countup+1;
if(Low[Count]<Low[Count-j]) countdown=countdown+1;
}
}
if(countup==leftbars+rightbars) Higher=High[Count];
else Higher = ExtUpperBuffer[Count-shift+1];
if(countdown==leftbars+rightbars) Lower = Low[Count];
else Lower=ExtLowerBuffer[Count-shift+1];
ExtUpperBuffer[Count-shift] = Higher;
ExtLowerBuffer[Count-shift] = Lower;
countup=0;
countdown=0;
if(shift==0)
{
Comment("FractalMod("+leftbars+","+rightbars+";"+shift+")","\n Up="+Higher+";Down="+Lower);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments