Create fractal indicator

 
As would be the mathematical expression to construct the indicator fractal?.
I would like to create fractal indicator in matlab. To do that would have as it would be??
To calculate fractals should I consider maximum and minimum. If for example I have the first bullish candle, the second has a higher maximum than the second, the third has a maximum greater than the second, but the fourth candle has a lower minimum of the third then a fractal would appear not?
 
burnssss:
As would be the mathematical expression to construct the indicator fractal?.
I would like to create fractal indicator in matlab. To do that would have as it would be??
To calculate fractals should I consider maximum and minimum. If for example I have the first bullish candle, the second has a higher maximum than the second, the third has a maximum greater than the second, but the fourth candle has a lower minimum of the third then a fractal would appear not?


Simply said the first candle where a fractal can appear is bar 2

if(k>=2 && High[k] > High[k-2] && High[k] > High[k-1] && High[k] > High[k+1] && High[k] > High[k+2] ) fractal ...

or if(k>=2 && High[k] < High[k-2] && High[k] < High[k-1] && High[k] < High[k+1] && High[k] < High[k+2] ) fractal ...

 

I have no idea what Matlab is.

The usual code for fractals only calculate on a high with lower highs either side to qualify as a high fractal. Vice versa for a low.

Otherwise you would not get opposite fractals on the same bar.

For a high fractal. I would have thought that the code

if(iHighest(NULL,0,MODE_HIGH,3,index+1)==index+2)

would indicate a high fractal

 
because a script I export the data to another platform fractals you know of any?.
But I have to calculate the fractal with OHLC data, and for this I have to know how to calculate them perfectly
 

How could modify the following code to create a csv file with historic data of fractal indicator?

#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 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,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;
   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);
  }
//+------------------------------------------------------------------+

many thanks

 

iFractals

this is calculating fractals and returns its value

with a loop you can check every bar you want if there is a fractal

 
deVries:

iFractals

this is calculating fractals and returns its value

with a loop you can check every bar you want if there is a fractal

I could give an example?
It means that by using this command, it generates a file with fractals after?
The historical data we obtained fractals in the same way?
 
burnssss:
I could give an example?
It means that by using this command, it generates a file with fractals after?
The historical data we obtained fractals in the same way?


no you can find where is a fractal and if you check this

https://www.mql5.com/en/code/7716

it might be you find a way to get the values in a csv file

 
I read the article, but I have several questions for the implantation of the code. That is iFRACTALS function is the function that fractals recounts not, but then I have to implement the code for storage in csv, it?
I mean, you would use the normal code fractals, but adding iFRACTAL function and code for storage in csv?. How to place it in different parts of the scrip?. Sorry for the inconvenience, I'm learning MQL4 and I need to save the historic fractals for statistical calculations.
thank you very much
 

Could this code be tweaked to form a three days high/low as fractal points instead of five days high/low??

help would be highly appreciated

 
saikatdg: Could this code be tweaked to form a three days high/low as fractal points instead of five days high/low??

Tweak it? Just write it.

bool peek(int i){
  double a=High[i+1], b=High[i], c=High[i-1];
  return (a-b)*(b-c) < 0 && b > c;
}
Reason: