EA Function to determine double bottoms and inverse head and shoulders using fractals

 

I am trying to complete a function which can identify the chart patterns for double bottoms, double tops, head and shoulders and inverse head and shoulders.

The upper and lower fractals are shown on a 15 min chart of Brent Crude here with the help of the fractal Support Resistance indicator (attached).  The inverse head and shoulders was a perfect entry for a big move up.

I can store the nearest upper and lower fractals for double bottoms/tops, but I am getting stuck storing the second nearest upper and lower fractals in my EA to use for head and should pattern trades. Please note the Right Shoulder fractal doesn't appear until the second bar up which means the Left Shoulder (second nearest lower fractal) has to be used to determine the entry on the first bar up from this fractal level on the right side. Stop should be placed a little under the entry bar low.

I would appreciate it if someone could kindly correct the code for storing the second nearest upper and lower fractal values please - thanks. Code at bottom of post.

Brent crude Inverse head and shoulders shown using fractals



//+----Determines the 1st & 2nd upper & lower fractal bar indices, and values for these fractals

   double UpFractal_Bar_1,UpFractal_1,UpFractal_Bar_2,UpFractal_2,DownFractal_Bar_1,DownFractal_1,DownFractal_Bar_2,DownFractal_2,

          TimeOfDownFractal_1,TimeOfUpFractal_1,TimeOfDownFractal_2,TimeOfUpFractal_2;

  

//--- finding the bar index of the nearest upper fractal

   for(int a=1;a<Bars;a++){

      if(iFractals(NULL, 0, MODE_UPPER,a)!=0){

         break;

         UpFractal_Bar_1=a;

         TimeOfUpFractal_1=Time[a];

         }//end if

      }//end for

      

//--- the value of the nearest upper fractal

      UpFractal_1=iFractals(NULL, 0, MODE_UPPER,UpFractal_Bar_1);


//for loop to find the bar index of the second nearest upper fractal

   for(int b=UpFractal_Bar_1+1;b<Bars;b++){

      if(iFractals(NULL, 0, MODE_UPPER,b)!=0){

         break;

         UpFractal_Bar_2=b;

         TimeOfUpFractal_2=Time[b];

         }//end if

      }//end for


//--- the value of the second nearest upper fractal

      UpFractal_2=iFractals(NULL, 0, MODE_UPPER,UpFractal_Bar_2);



//--- finding the bar index of the nearest lowest fractal

   for(int c=1;c<Bars;c++){

      if(iFractals(NULL, 0, MODE_LOWER,c)!=0){

         break;

         DownFractal_Bar_1=c;

         TimeOfDownFractal_1=Time[c];

         }//end if

      }//end for

      

//--- the value of the nearest lower fractal

      DownFractal_1=iFractals(NULL, 0, MODE_LOWER,DownFractal_Bar_1);

      

//--- finding the index bar of the second nearest lower fractal

   for(int d=UpFractal_Bar_1+1;d<Bars;d++){

      if(iFractals(NULL, 0, MODE_LOWER,d)!=0){

         break;

         DownFractal_Bar_2=d;

         TimeOfDownFractal_2=Time[d];

         }//end if

      }//end for


//--- the value of the second nearest lower fractal      

      DownFractal_2=iFractals(NULL, 0, MODE_LOWER,DownFractal_Bar_2);

 
  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. if(iFractals(NULL, 0, MODE_UPPER,a)!=0){
       break;
    
       UpFractal_Bar_1=a;
       TimeOfUpFractal_1=Time[a];
    }//end if
    Code after a break will never execute.
 

whroeder1, thanks so much for your reply and corrective information - I'm still learning.

I have done more research and found a function which works better than my original code as shown. This function can be used to action for your trade entry and possibly exit too with conditional statements .

e.g. findFractal(1, 1, 0) = value of second nearest upper fractal

==> first value in brackets = no. of fractals back and zero = nearest fractal

==> second value determines Upper(1) or Lower fractal(2)

==> third value = timeframe  and zero = current chart timeframe)


findFractal(0, 2, 0) = value of nearest lower fractal for current chart timeframe.


With this function you can find double bottoms/tops or head & shoulders more easily and action on them with stop or limit orders.

if((iLow(NULL, 0 , 1) <=  findFractal(0, 2, 0)) && (iClose(NULL, 0 , 1) > findFractal(0, 2, 0)))

//Find Fractals

   double findFractal(int nbr, int mode, int timeframe)
   {
   int i=3, n;
   for(n=0;n<=nbr;n++)
   {
      while(iFractals(Symbol(),timeframe,mode,i) == 0)
         i++;
      if(n<nbr)
         i++;
   }
   return(iFractals(Symbol(),timeframe,mode,i));
}