Fractals[1]

Author: Copyright � 2005, MetaQuotes Software Corp.
Fractals[1]

This script is a technical analysis tool designed to identify specific price patterns known as "Fractals" on a trading chart. In simple terms, it scans historical price data to pinpoint significant peaks and valleys, helping traders visualize potential turning points in the market.

How the Logic Works

The script functions by systematically reviewing each candle (bar) on your chart to determine if its high or low point stands out significantly compared to the candles surrounding it. It categorizes these movements into two main types:

1. Upward Fractals (Resistance Points) The tool looks for "peaks." To mark a price point as an upward fractal, the script checks if a candle's high price is higher than the highs of the bars immediately preceding and following it. It doesn't just look at the closest neighbors; it applies a tiered logic to ensure the peak is distinct:

  • Standard Peak: The simplest form where one high point stands out above two bars on either side.
  • Extended Peaks: If a standard peak isn't found, the script expands its search to include patterns spanning 6, 7, 8, or even 9 consecutive bars. This allows it to identify broader "mountain tops" in the market where the price stayed at a similar high level for a short duration before dropping.

2. Downward Fractals (Support Points) Similarly, the tool looks for "valleys." It identifies points where a candle's low price is lower than the lows of the surrounding bars. Just like with the peaks, it utilizes a multi-layered check:

  • Standard Valley: A clear low point flanked by higher prices on both sides.
  • Extended Valleys: It performs a secondary search to find broader "troughs" involving 6 to 9 bars. This helps in identifying significant price floors where the market may have consolidated before moving upward.

Key Technical Features

  • Visual Markers: Once the logic confirms a fractal has been found, the script places a visual arrow on the chart (Red for peaks/Upward and Blue for valleys/Downward) at the exact price level.
  • Volume Verification: The script includes a safety check to ensure that trading volume exists during the calculation; this prevents the tool from identifying "ghost" patterns in periods where no actual trading occurred.
  • Historical Scanning: The logic is designed to run backward through your chart history, ensuring that every previous bar is analyzed to provide a complete map of historical price structure.

In summary, this script automates the tedious task of manually looking for significant price "swings," highlighting where the market has historically shifted direction or hit a temporary ceiling/floor.

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
Fractals[1]
//+------------------------------------------------------------------+
//|                                                     Fractals.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                                 Update by Kharko |
//+------------------------------------------------------------------+
#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
#property indicator_width1 1
#property indicator_width2 1
//---- 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    
   i=Bars-nCountedBars+3;
//----Up and Down Fractals
   while(i>=3)
     {
      //----Fractals up
      bFound=false;
      dCurrent=High[i];
      if(dCurrent>High[i+1] && dCurrent>High[i+2] && dCurrent>High[i-1] && dCurrent>High[i-2] && Volume[i-3]>0)
        {
         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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
        {
         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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
           {
            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] && Volume[i-3]>0)
           {
            bFound=true;
            ExtDownFractalsBuffer[i]=dCurrent;
           }                      
        }                                    
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments