Find last fractal

 

Hello

Example:

If I want my EA to Find the second last fractal up, that was above Moving Average 100

And compare it to the last fractal up, thats above Moving Average 100

If the last fractal up is lower then the second last = SELL

I think i got this right,

But i need help putting in the Moving average?

Becuase i need the Moving average value that was when the second last fractal was there?

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));
}









if( findFractal(1, MODE_UPPER, timeframe) < findFractal(0, MODE_UPPER, *timeframe of your choice*) ) = SIGNAL_SELL

Cheers!

 
pontuso:


But i need help putting in the Moving average?

Becuase i need the Moving average value that was when the second last fractal was there?

Something like this . . .

while(iFractals(Symbol(), timeframe, mode, i) == 0 && iMA(Symbol(), timeframe, 100, 0, MODE_SMA, PRICE_HIGH, i ) > iFractals(Symbol(),timeframe,mode,i) )

. . . . you will need to tweak the iMA call to suit your needs . . .

If you want to compare both fractals against the same MA then do the MA calculation first and assign it to a variable, then do the fractal stuff and compare it to the stored MA value. So you would have 2 for loops . . .

 
pontuso: Becuase i need the Moving average value that was when the second last fractal was there?
You are returning the value of the second fraction. Return the candle index instead. Then you can get either.
 
Ok thanx guys for the fast answer. I will try this out!
 

Hi ,


Struggling here and I want to make this as easy as possible but for some reason I cannot locate create buffered to highlight upper fractals that are lower than the previous upper fractal. Somehow my code bombs out even though I specify previous fractal as [i-1]

could someone help solve this puzzle for me please



#property indicator_chart_window
#property indicator_buffers 6

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 3
#property indicator_width6 3

#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Purple
#property indicator_color4 Purple
#property indicator_color5 Lime
#property indicator_color6 Red

extern int LeftBars  = 2;
extern int RightBars = 2;

double LineUpBuffer1[];
double LineDownBuffer2[];
double FractalUpBuffer3[];
double FractalDownBuffer4[];
double LowestFractalUpBuffer5[];
double HighestFractalLowerBuffer5[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexStyle(0, DRAW_LINE);
   SetIndexArrow(0, 158);
   SetIndexBuffer(0, LineUpBuffer1);
   SetIndexEmptyValue(0, 0.0);
   SetIndexLabel(0, "UpperFactalLine");
   
   SetIndexStyle(1, DRAW_LINE);
   SetIndexArrow(1, 158);
   SetIndexBuffer(1, LineDownBuffer2);
   SetIndexEmptyValue(1, 0.0);
   SetIndexLabel(1, "LowerFractalLine");

   SetIndexStyle(2, DRAW_ARROW);
   SetIndexArrow(2, 119);
   SetIndexBuffer(2, FractalUpBuffer3);
   SetIndexEmptyValue(2, 0.0);
   SetIndexLabel(2, "FractalArrowUP");
   
   SetIndexStyle(3, DRAW_ARROW);
   SetIndexArrow(3, 119);
   SetIndexBuffer(3, FractalDownBuffer4);
   SetIndexEmptyValue(3, 0.0);
   SetIndexLabel(3, "FractalArrowDown");

   SetIndexStyle(4, DRAW_ARROW);
   SetIndexArrow(4, 161);
   SetIndexBuffer(4, LowestFractalUpBuffer5);
   SetIndexEmptyValue(4, 0.0);
   SetIndexLabel(4, "BreakPreviousFractalUp");
   
   SetIndexStyle(5, DRAW_ARROW);
   SetIndexArrow(5, 161);
   SetIndexBuffer(5, HighestFractalLowerBuffer5);
   SetIndexEmptyValue(5, 0.0);
   SetIndexLabel(5, "BreakPreviousFractalDown");

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars > 0)   counted_bars--;
   int limit = Bars - counted_bars;
   if(counted_bars==0) limit-=1+MathMax(LeftBars,RightBars);

   for(int i = limit-1; i >= 0; i--)
   {
      LineUpBuffer1[i] = isFractalUp(i, LeftBars, RightBars,limit);
     
      if(LineUpBuffer1[i] == 0)
         LineUpBuffer1[i] = LineUpBuffer1[i+1];
      else
       FractalUpBuffer3[i] = LineUpBuffer1[i];
     
           LineDownBuffer2[i] = isFractalDown(i, LeftBars, RightBars,limit);
      if(LineDownBuffer2[i] == 0)
         LineDownBuffer2[i] = LineDownBuffer2[i+1];
      else
         FractalDownBuffer4[i] = LineDownBuffer2[i];
     
     
     
//This highlighlight the highest lower fractals from previous fractal
 if(FractalDownBuffer4[i] > FractalDownBuffer4[i-1] && FractalDownBuffer4[i] >= FractalDownBuffer4[i+1])  
       LowestFractalUpBuffer5[i] = Low[i];


 //This highlights the lowest fractsl from the previous,, wel should but its not arghhhhhhh
if(FractalUpBuffer3[i] < FractalUpBuffer3[i-1] && FractalUpBuffer3[i] <= FractalUpBuffer3[i+1])  
       LowestFractalUpBuffer5[i] = High[i];

    
             
   }
    return(0);
}





double isFractalUp(int index, int lBars, int rBars, int maxind)
{
   int left = lBars, right = rBars;
   double max = High[index]; //
   for(int i = index - right; i <= (index + left); i++)
   {
     if (i<0 || i>maxind) return(0);
      if(!(High[i] > 0.0))return(0);
      if(max < High[i] && i != index)
      {
         if(max < High[i])  return(0);
         if(MathAbs(i - index) > 1) return(0);
      }
   }
   return(max);
}




double isFractalDown(int index, int lBars, int rBars, int maxind)
{
   int left = lBars, right = rBars;
   double min = Low[index] ;
   for(int i = index - right; i <= (index + left); i++)
   {
      if (i<0 || i>maxind) return(0);
      if(!(Low[i] > 0.0))return(0);
      //if(min >= Low[i] && i != index)
      if(min > Low[i] && i != index)
      {
         if(min > Low[i])
            return(0);

         if(MathAbs(i - index) > 1)
            return(0);
      }

   }
   return(min);
   }