Fractals_v1

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
Fractals_v1

This script is designed to identify and visualize specific price patterns on a trading chart known as "Fractals." In technical analysis, fractals represent structural turning points where the market shifts from an upward trend to a downward one, or vice versa. The script automates the detection of these peaks and valleys to help traders spot potential support and resistance levels.

Core Logic and Functionality

The script scans historical price data to find specific "high" and "low" points based on their relationship to neighboring bars (candles). It distinguishes between two types of signals:

1. Upward Fractals (Resistance Peaks) The tool looks for price peaks. It identifies a point as an "Up Fractal" if its high price is significantly higher than the highs of the candles immediately surrounding it. When the script detects such a peak, it places a visual marker on the chart to signify a potential area where the price might face downward pressure.

2. Downward Fractals (Support Valleys) Conversely, the tool searches for "bottoms." It identifies a point as a "Down Fractal" if its low price is lower than the lows of the surrounding candles. These markers indicate areas where the price has found a floor, potentially signaling a future upward move.

Advanced Pattern Recognition

Unlike basic fractal indicators that only look at the immediate five bars surrounding a point, this script uses a multi-layered logic to ensure higher accuracy:

  • Dynamic Range Scanning: The script doesn't just check the nearest neighbors; it evaluates various configurations ranging from 5-bar patterns up to 9-bar patterns.
  • Equality Handling: It is programmed to recognize "flat" peaks or valleys. This means if two adjacent candles share the same high or low price, the script can still identify them as part of a single fractal structure rather than ignoring them because they aren't strictly higher/lower than each other.
  • Sequential Verification: By checking for various lengths (6, 7, 8, and 9 bars), the script ensures that it captures complex price action where a peak might be wider or more gradual, providing a more comprehensive view of market structure than a simple three-bar check.

Visual Output

The script simplifies this complex mathematical scanning into two distinct visual elements on your chart:

  • Red Markers: Represent identified "Up Fractals" (Price peaks).
  • Blue Markers: Represent identified "Down Fractals" (Price valleys).

By automating this process, the script allows traders to quickly see where historical price action has pivoted, helping them identify key zones of interest without having to manually scan every candle on a chart.

Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Fractals_v1
//+------------------------------------------------------------------+
//|                                                     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
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];
      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];
          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];
          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];
              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];
         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];
             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];
         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];
             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];
         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];
             i--;
             continue;
           }                        
        }                                    
      i--;
     }

//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments