3 Bar Fractals Indicator

 
//------------------------------------------------------------------
#property copyright   
"Lemin" 
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  clrRed
#property indicator_color2  clrGreen
#property indicator_width1  1
#property indicator_width2  1
#property strict

//
//
//

extern int                FractalPeriod          = 15;             // Fractal period: 5 for built in fractal
extern int                Historybars            = 100;            // Tried to add option to limit arrows drawn on the chart
extern ENUM_APPLIED_PRICE PriceHigh              = PRICE_HIGH;     // Price high
extern ENUM_APPLIED_PRICE PriceLow               = PRICE_LOW;      // Price low
extern double             UpperArrowDisplacement = 0.4;            // Upper arrow displacement
extern double             LowerArrowDisplacement = 0.4;            // Lower arrow displacement
input bool                alertsOn               = true;           // Turn alerts on/off?
input bool                alertsOnCurrent        = true;          // Alerts on open bar on/off?
input bool                alertsMessage          = true;           // Alerts message on/off?
input bool                alertsSound            = false;          // Alerts sound on/off?
input bool                alertsNotify           = true;          // Alerts notification on/off?
input bool                alertsEmail            = false;          // Alerts email on/off?
input string              soundFile              = "alert2.wav";   // Sound file

double v1[],v2[];

//------------------------------------------------------------------

int OnInit()
{
   if (MathMod(FractalPeriod,2)==0) FractalPeriod = FractalPeriod+1;
   SetIndexBuffer(0,v1);  SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,234); 
   SetIndexBuffer(1,v2);  SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,233); 
   
   IndicatorShortName("swing points");
   return(INIT_SUCCEEDED);
}

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 i,limit=MathMin(MathMax(rates_total-prev_calculated,FractalPeriod),rates_total-1);
     
   //
   //
   //
   //
   //
     
   int half = FractalPeriod/2;    
   for(i=limit; i>=half && !_StopFlag; i--) 
   {
      bool   found     = true;
      double compareTo = iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i);
         for (int k=1; k<=half; k++)
            {
               if ((i+k)<rates_total && iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i+k)> compareTo) { found=false; break; }
               if ((i-k)>=0          && iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i-k)>=compareTo) { found=false; break; }
            }
         if (found) 
               v1[i] = high[i]+iATR(NULL,0,20,i)*UpperArrowDisplacement;
         else  v1[i] = EMPTY_VALUE;

         //
         //
         //
      
         found     = true;
         compareTo = iMA(NULL,0,1,0,MODE_SMA,PriceLow,i);
         for (int k=1; k<=half; k++)
            {
               if ((i+k)<rates_total && iMA(NULL,0,1,0,MODE_SMA,PriceLow,i+k)< compareTo) { found=false; break; }
               if ((i-k)>=0          && iMA(NULL,0,1,0,MODE_SMA,PriceLow,i-k)<=compareTo) { found=false; break; }
            }
          if (found)
               v2[i] = low[i]-iATR(NULL,0,20,i)*LowerArrowDisplacement;
          else v2[i] = EMPTY_VALUE;
         
   }
   static datetime previousLevel  = -1; 
   static int      previousSignal = 0;
   if (alertsOn)
   {
      int currentBar=-1;
               for (i=0; i<rates_total-1; i++) if (v2[i]!=EMPTY_VALUE || v1[i]!=EMPTY_VALUE) { currentBar = i; break; }
      if (currentBar>-1) checkAlert(currentBar,previousLevel,previousSignal,v1,v2 ,"");
   }
return(rates_total);
}

//+------------------------------------------------------------------+
//|                                                             
//+------------------------------------------------------------------+
//
//
//
//
//

void checkAlert(int currentBar, datetime& previousLevel, int& previousSignal, double& upBuffer[], double& dnBuffer[], string text)
{
   int previousBar = iBarShift(NULL,0,previousLevel);
   int currentSignal = 0;
         if (v1[currentBar]!=EMPTY_VALUE)                                currentSignal = -1;
         if (v2[currentBar]!=EMPTY_VALUE)                                currentSignal =  1;
         if (v2[currentBar]!=EMPTY_VALUE && v1[currentBar]!=EMPTY_VALUE) currentSignal =  0;

   //
   //
   //
   //
   //
   
   if (currentBar != previousBar || currentSignal != previousSignal)
   {
      string alertText;
      if (currentSignal != previousSignal && currentBar > previousBar && previousLevel != -1)
           alertText = "reverted to ";
      else alertText = "current signal ";            
      switch(currentSignal)
      {
         case  0 : doAlert(text+alertText+"up/down"); break;
         case  1 : doAlert(text+alertText+"up");      break;
         case -1 : doAlert(text+alertText+"down");
      }               
 
      //
      //
      //
      //
      //
              
      previousLevel  = Time[currentBar];
      previousSignal = currentSignal;
   }
}

//
//
//
//
//

void doAlert(string doWhat)
{
     string message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" Fractals "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(_Symbol+" Fractals ",message);
          if (alertsSound)   PlaySound(soundFile);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

Hey guys hope everyone is doing well.

I really need help.

I have this adjustable fractal indicator that is showing arrows after 5 bars when swing point occurs. Can anyone please edit for me to get signal at least 2 bars when the swing point forms. 

Also the history bar limit option. I tried to add it on the user settings but I got stuck .


Kindly anyone help me adjust those two options . I will be very grateful for the assistance.

 
Ismail Lemin:

Hey guys hope everyone is doing well.

I really need help.

I have this adjustable fractal indicator that is showing arrows after 5 bars when swing point occurs. Can anyone please edit for me to get signal at least 2 bars when the swing point forms. 

Also the history bar limit option. I tried to add it on the user settings but I got stuck .


Kindly anyone help me adjust those two options . I will be very grateful for the assistance.

Search for adaptive fractals you should be able to change that
 
Amos Tsopotsa #:
Search for adaptive fractals you should be able to change that
Hey Amos. Kindly help I know it sounds ridiculous that I don't know where to begin searching for Tha adaptive fractals but that code is all I got. I woudo be very confused if I try to change it. Please help where you can. 
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893