Fractal indicator code

 

I have a problem with this code. It is a great indicator however, the fractals are not redrawn, which means if the indicator draw a fractal it doesnt remove it if another fractal formed within the fractal range (2 candles before and after, 5 candles before and after ... etc depends on the user). Can anybody help me fix this code to make it redraw if another fractal formed within the fractal range? ... thanks!


//+------------------------------------------------------------------+
//|                                                    sFractals.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepPink
#property indicator_color2 PaleGreen
#property indicator_width1 2
#property indicator_width2 2

//---- buffers
double ExtUpFractalsBuffer[];
double ExtDownFractalsBuffer[];

extern string Fractal_Desc=" ******* Fractal settings:";
extern int  CandlesBeforeAfter      = 5;
extern string ArrowShape_Desc="1 Arrow1,2 Arrow2,3 Arrow3,4 Arrow4,5 Circle1,6 Circle2,7 Circle3,8 Rectangle1,9 Rectangle2,10 Diamond1,11 Diamond2,12 Hand";
extern int  ArrowShape              = 1;
extern int  ArrowSize               = 0;
extern double  ArrowOffset          = 1.5;

extern string Alert_Desc=" ******* Alert settings:";
extern int  AlertCandle            = 0;
extern bool SoundAlerts            = FALSE;
extern bool PopupAlerts            = FALSE;
extern bool PushNotificationAlerts = FALSE;
extern bool EmailAlerts            = FALSE;
extern string SoundFileLong        = "alert.wav";
extern string SoundFileShort       = "alert2.wav";
datetime alert_time;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping 
    SetIndexBuffer(0, ExtUpFractalsBuffer);
    SetIndexBuffer(1, ExtDownFractalsBuffer);
//----
    SetIndexDrawBegin(1,0);
    SetIndexDrawBegin(0,0);
    SetIndexEmptyValue(0, 0.0);
    SetIndexEmptyValue(1, 0.0); 
//---- drawing settings
    SetIndexStyle(0, DRAW_ARROW);
    SetIndexStyle(1, DRAW_ARROW);
//---- name for DataWindow
    SetIndexLabel(0, "sFractal Up");
    SetIndexLabel(1, "sFractal Down");
//---
    switch(ArrowShape){
       case 1:SetIndexArrow(0, 234);SetIndexArrow(1, 233);break;
       case 2:SetIndexArrow(0, 242);SetIndexArrow(1, 241);break;
       case 3:SetIndexArrow(0, 226);SetIndexArrow(1, 225);break;
       case 4:SetIndexArrow(0, 222);SetIndexArrow(1, 221);break;
       case 5:SetIndexArrow(0, 159);SetIndexArrow(1, 159);break;
       case 6:SetIndexArrow(0, 164);SetIndexArrow(1, 164);break;
       case 7:SetIndexArrow(0, 174);SetIndexArrow(1, 174);break;
       case 8:SetIndexArrow(0, 110);SetIndexArrow(1, 110);break;
       case 9:SetIndexArrow(0, 112);SetIndexArrow(1, 112);break;
       case 10:SetIndexArrow(0, 119);SetIndexArrow(1, 119);break;
       case 11:SetIndexArrow(0, 116);SetIndexArrow(1, 116);break;
       case 12:SetIndexArrow(0, 72);SetIndexArrow(1, 71);break;
    }
//---- Constants
    ArrowOffset=ArrowOffset/100;
//---- initialization done  
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i,limit,pos,nCountedBars=IndicatorCounted();

   if(nCountedBars<0)
      return(-1);
//---- last counted bar will be recounted
   if(nCountedBars==0)
      limit=Bars -(CandlesBeforeAfter*2+1);
   else
      limit=Bars-nCountedBars;
//----Up and Down Fractals
   for(i=limit; i>=0; i--)
     {
      pos=ArrayMaximum(High,CandlesBeforeAfter*2+1,i);

      if(pos==i+CandlesBeforeAfter)
        {
         ExtUpFractalsBuffer[pos]=High[pos]+ArrowOffset;

         if(Time[0]>alert_time)
           {
            alert_time=Time[0];
            doAlerts("HIGH FRACTAL",SoundFileShort);
           }
        }

      pos=ArrayMinimum(Low,CandlesBeforeAfter*2+1,i);

      if(pos==i+CandlesBeforeAfter)
        {
         ExtDownFractalsBuffer[pos]=Low[pos]-ArrowOffset;

         if(Time[0]>alert_time)
           {
            alert_time=Time[0];
            doAlerts("LOW FRACTAL",SoundFileLong);
           }
        }
     }

   return(0);
  }
 
// Sending Emails and Alerts
void doAlerts(string msg,string SoundFile) {
   msg=WindowExpertName()+" "+msg+" "+Symbol()+", period "+TFtoStr(Period());
   string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
   if (PopupAlerts) Alert(msg);
   if (EmailAlerts) SendMail(emailsubject,msg);
   if (PushNotificationAlerts) SendNotification(msg);
   if (SoundAlerts) PlaySound(SoundFile);
}//void doAlerts(string msg,string SoundFile) {

   string TFtoStr(int period) {
   if (period==0) period=Period();
   switch(period) {
      case 1     : return("M1");  break;
      case 5     : return("M5");  break;
      case 15    : return("M15"); break;
      case 30    : return("M30"); break;
      case 60    : return("H1");  break;
      case 240   : return("H4");  break;
      case 1440  : return("D1");  break;
      case 10080 : return("W1");  break;
      case 43200 : return("MN1"); break;
      default    : return(DoubleToStr(period,0));
   }
   return("UNKNOWN");
}//string TFtoStr(int period) {

//+------------------------------------------------------------------+
 
Don't look at bar zero.
 
William Roeder:
Don't look at bar zero.
I dont understand. Do you know how to fix it?
 
Khalid Aldarawy: I dont understand. Do you know how to fix it?

Change your code so it doesn't look at bar zero. What part of that was unclear? Yes I know how to fix it. Do you?

 
William Roeder:

Change your code so it doesn't look at bar zero. What part of that was unclear? Yes I know how to fix it. Do you?

I don't know how to code, I only can try and modify using trial and error :) but I dont know terms. If you can edit that part and share it with me I will be thankful :)
 
No excuse, learn!
  1. MT4: Learn to code it.
    MT5: Learn to code. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  2. or pay (Freelance) someone to code it.
              Hiring to write script - General - MQL5 programming forum
  3. We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.

  4.    for(i=limit; i>=0; i--)
    
    Read the documentation on for. Then tell me what values your i variable takes. Then tell me how to avoid zero.
 
William Roeder:
No excuse, learn!
  1. MT4: Learn to code it.
    MT5: Learn to code. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  2. or pay (Freelance) someone to code it.
              Hiring to write script - General - MQL5 programming forum
  3. We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.

  4. Read the documentation on for. Then tell me what values your i variable takes. Then tell me how to avoid zero.

LOL

 
Here is my recent implementation of the fractal indicator, which I think solves your problem. I hope it appeals to you.
Fractal
Fractal
  • www.mql5.com
This indicator is used for indentifying pivots of different degrees.
Reason: