wlxFractals_v1
Download scriptThis script functions as a technical analysis tool designed to identify specific structural patterns in price action known as "fractals." In trading, these fractals serve as visual markers for potential reversal points or significant support and resistance levels by highlighting peaks and valleys in the market's movement.
Core Logic Overview
The script scans historical price data to find specific formations where a single high point (for upward fractals) or a single low point (for downward fractals) stands out against its immediate neighbors. It ensures that these points are significant enough to be considered "fractals" based on the parameters you set, rather than just minor fluctuations in price.
How it Identifies Patterns
To ensure accuracy, the script evaluates every price bar by looking at a defined number of bars both to the left and to the right of the current point:
- Upward Fractals (Peaks): The logic searches for a high point that is higher than all neighboring highs on its right side and satisfies a specific relationship with the highs on its left side. When these conditions are met, it places a visual marker at that peak.
- Downward Fractals (Valleys): Conversely, the script looks for a low point that is lower than all neighboring lows on its right side and compares it against the lows on its left side. When this condition is satisfied, it marks that valley.
Key Technical Behaviors
- Symmetry Checking: The tool doesn't just look for "higher" or "lower" prices; it uses a balancing logic to ensure the peak or valley is distinct. It checks if the price remains consistent or moves in a specific direction within the surrounding area.
- Dynamic Scanning: The script processes the chart history automatically, calculating the necessary range of bars to inspect based on your input settings. This allows it to distinguish between "noise" (small movements) and actual structural changes in the market.
- Visual Output: Once the logic confirms a fractal has formed, it renders two distinct types of arrows on your chart: one for the upper peaks (DeepSkyBlue) and one for the lower valleys (Red), providing an immediate visual cue of the market's structure.
User Customization
The logic is driven by several adjustable parameters that allow you to change the "sensitivity" of the detection:
- You can define how many bars to look at to the left and right of a potential point.
- You can adjust the equality settings, which determine how the script handles price points that are exactly equal to one another, ensuring the markers appear consistently according to your preferred trading style.
//+------------------------------------------------------------------+
//| wlxFractals.mq4 |
//| Copyright © 2004, by konKop, GOODMAN, Mstera, af + wellx |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, by wellx"
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red
//---- input parameters
extern int Equals=5;
extern int nLeftUp=2;
extern int nRightUp=2;
extern int nLeftDown=2;
extern int nRightDown=2;
//---- buffers
double FractalsUp[];
double FractalsDown[];
int pos=0, cntup=0, cntdown=0, cnt=0;
int r=0,l=0,e=0;
int fup=0,fdown=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,217);
SetIndexBuffer(0,FractalsUp);
SetIndexEmptyValue(0,0.0);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,218);
SetIndexBuffer(1,FractalsDown);
SetIndexEmptyValue(1,0.0);
cntup=nLeftUp+nRightUp+Equals+1;
cntdown=nLeftDown+Equals+1;
if (cntup>=cntdown) cnt=cntup;
if (cntup<cntdown) cnt=cntdown;
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i=0, j=0;
int cbars=IndicatorCounted();
if (cbars<0) return(-1);
if (cbars>0) cbars--;
pos=0;
if (cbars > (Bars-cnt-1)) pos=(Bars-cnt-1);
else pos=cbars+nRightUp;
while (pos>=nRightUp)
{
FractalsUp[pos] =NULL;
FractalsDown[pos]=NULL;
//ôÐÀÊÒÀË ÂÂÅÐÕ
r=nRightUp; //ïðîâåðÿåì ïðàâóþ ñòîðîíó ôðàêòàëà
for (i=1;i<=r;i++)
{
if (High[pos]<=High[pos-i]) break;
}
//åñëè ñïðàâà âñå ÎÊ òî i äîëæíî áûòü ðàâíî r+1
if (i==r+1) //FractalsUp[pos]=High[pos];
{
l=nLeftUp; //ïðîâåðÿåì ëåâóþ ñòîðîíó ôðàêòàëà
e=Equals;
for (j=1;j<=l+Equals;j++)
{
if (High[pos] < High[pos+j]) break;
if (High[pos] > High[pos+j]) l--;
if (High[pos] == High[pos+j])e--;
if (l==0)
{
FractalsUp[pos]=High[pos];
break;
}
if (e<0) break;
}
}
//ÔÐÀÊÒÀË ÂÍÈÇ
r=nRightDown; //ïðîâåðÿåì ïðàâóþ ñòîðîíó ôðàêòàëà
for (i=1;i<=r;i++)
{
if (Low[pos]>=Low[pos-i]) break;
}
if (i==r+1) //FractalsUp[pos]=High[pos];
{
l=nLeftDown; //ïðîâåðÿåì ëåâóþ ñòîðîíó ôðàêòàëà
e=Equals;
for (j=1;j<=l+Equals;j++)
{
if (Low[pos] > Low[pos+j]) break;
if (Low[pos] < Low[pos+j]) l--;
if (Low[pos] == Low[pos+j])e--;
if (l==0)
{
FractalsDown[pos]=Low[pos];
break;
}
if (e<0) break;
}
}
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments