Indicators: iGDR_Fractal_Levels

 

iGDR_Fractal_Levels:

iGDR_Fractal_Levels indicator shows the fractals average values over a certain period of time. The indicator displays only three levels on the chart showing them in different colors. The same levels are plotted to the right from the text labels, though considering the variation used for calculation of an average value.

We must always bear in mind that deals with the use of fractals are opened only in the direction of these fractals breakout. The same thing with the levels. We must enter the market in the direction of these levels breakout, as that is the strongest signal for market entry, while nearby levels can be used as initial stop levels and profit targets.

Author: Nikolay Kositsin

iGDR_Fractal_Levels

 
Why is there only a 200 bar limit?
 
Good Indicator... Good Job...
 
Automated-Trading:

iGDR_Fractal_Levels:

Author: Nikolay Kositsin

Indicator does not work
 
Unfortunately, yes. It doesn't work. 4 compilation errors, and stupid ones at that. But the indicator is interesting.
 

got error while compiling

how to fix it

 
can't compile because of errors
 
Benkele Goitsemodimo #can't compile because of errors

Fixed.

 

//Indicator interesting. Corrected code

      //+------------------------------------------------------------------+
//|iGDR_Fractal_Levels.mq5 |
//| Copyright © 2008-2009, GreenDog, Russia |
//|krot@inbox.ru |
//+------------------------------------------------------------------+
//---- indicator authorship
#property copyright "Copyright © 2008-2009, GreenDog."
//---- link to author's website
#property link      "krot@inbox.ru"
//---- indicator version number
#property version   "1.00"
//---- draw indicator in the main window
#property indicator_chart_window 
#property indicator_buffers  1
#property indicator_plots    1
//+----------------------------------------------+
//|| Declaring an enumeration |
//+----------------------------------------------+
enum FRACTAL_MODE
  {
   MODE_HIGH_LOW=0, // by extrema
   MODE_LOW,        // by vertex
   MODE_HIGH        // by troughs
  };
//+----------------------------------------------+
//|| Declaring an enumeration |
//+----------------------------------------------+
enum FRACTAL_TYPE
  {
   TYPE1=0, // strict
   TYPE2    // non-strict
  };
//+----------------------------------------------+
//|| Indicator input parameters |
//+----------------------------------------------+
input uint frNum_=2;                      // Number of bars of the fractal, 2=5-bar fractal, 3=7-bar fractal, etc.
input FRACTAL_TYPE frType= TYPE2;         // Type of fractal definition 0=strict, 1=non-strict
input FRACTAL_MODE frMode =MODE_HIGH_LOW; // Mode 
input double dlt_=0.24;                   // Error from average bar height
input uint sBars_=200;                    // Number of bars (max. 200)
//----
input color  BG1_Color = PaleGreen;  // Background colour of the first levels
input color  TL1Color  = Green;      // Colour of the first level lines
input color  BG2_Color = Yellow;     // Background colour of the second levels
input color  TL2Color  = DarkOrange; // Colour of the lines of the second levels
input color  BG3_Color = Pink;       // Background colour of the third levels
input color  TL3Color  = Red;        // Colour of third level lines
//+----------------------------------------------+
uint lastBars=0;
uint frNum,sBars;
datetime lastTime=0;
color BGColor[3],TLColor[3];
double aData[240][2],aRes[3][2];
//+------------------------------------------------------------------+
//| Custom indicator initialisation function |
//+------------------------------------------------------------------+ 
void OnInit()
  {
//---- initialising constants
   frNum=frNum_;
   if(frNum_<2) frNum=2;
   sBars=sBars_;
   if(sBars_>200) sBars=200;
   if(sBars_<10) sBars=10;
   BGColor[0]=BG1_Color;
   BGColor[1]=BG2_Color;
   BGColor[2]=BG3_Color;
   TLColor[0]=TL1Color;
   TLColor[1]=TL2Color;
   TLColor[2]=TL3Color;
//----
  }
//+------------------------------------------------------------------+
//| Creating a text label|
//+------------------------------------------------------------------+
void CreateTextLabel(long     chart_id,   // chart identifier
                     string   name,       // object name
                     int      nwin,       // window index
                     datetime time1,      // time of 1 price level
                     double   price1,     // 1 price level
                     color    Color,      // line colour
                     string   text,       // text
                     string   font,       // font 
                     int      fontsize)   // font size
  {
//----
   ObjectCreate(chart_id,name,OBJ_TEXT,nwin,time1,price1);
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_BACK,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true);
   ObjectSetString(chart_id,name,OBJPROP_FONT,font);
   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
   ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,fontsize);
//----
  }
//+------------------------------------------------------------------+
//|| Resetting the text label|
//+------------------------------------------------------------------+
void SetTextLabel(long     chart_id,    // chart identifier
                  string   name,        // object name
                  int      nwin,        // window index
                  datetime time1,       // time of 1 price level
                  double   price1,      // 1 price level
                  color    Color,       // line colour
                  int      style,       // line style
                  int      width,       // line thickness
                  string   text,        // text
                  string   font,        // font
                  int      fontsize)    // font size
  {
//----
   if(ObjectFind(chart_id,name)==-1)
     {
      CreateTextLabel(chart_id,name,nwin,time1,price1,Color,text,font,fontsize);
     }
   else
     {
      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
      ObjectMove(chart_id,name,0,time1,price1);
      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
     }
//----
  }
//+------------------------------------------------------------------+
//| Creating a channel|
//+------------------------------------------------------------------+
void CreateChannel(long     chart_id,      // chart identifier
                   string   name,          // object name
                   int      nwin,          // window index
                   datetime time1,         // time of 1 price level
                   double   price1,        // 1 price level
                   datetime time2,         // time of price level 2
                   double   price2,        // 2 price level
                   datetime time3,         // time of price level 3
                   double   price3,        // 3 price level
                   color    Color,         // line colour
                   int      style,         // line style
                   int      width,         // line thickness
                   string   text)          // text
  {
//----
   ObjectCreate(chart_id,name,OBJ_CHANNEL,nwin,time1,price1,time2,price2);
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
   ObjectSetInteger(chart_id,name,OBJPROP_FILL,true);
   ObjectSetInteger(chart_id,name,OBJPROP_BACK,true);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,true);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true);
//----
  }
//+------------------------------------------------------------------+
//|| Reinstalling the channel|
//+------------------------------------------------------------------+
void SetChannel(long     chart_id,    // chart identifier
                string   name,        // object name
                int      nwin,        // window index
                datetime time1,       // time of 1 price level
                double   price1,      // 1 price level
                datetime time2,       // time of price level 2
                double   price2,      // 2 price level
                datetime time3,       // time of price level 3
                double   price3,      // 3 price level
                color    Color,       // line colour
                int      style,       // line style
                int      width,       // line thickness
                string   text)        // text
  {
//----
   if(ObjectFind(chart_id,name)==-1)
     {
      CreateChannel(chart_id,name,nwin,time1,price1,time2,price2,time3,price3,Color,style,width,text);
     }
   else
     {
      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
      ObjectMove(chart_id,name,0,time1,price1);
      ObjectMove(chart_id,name,1,time2,price2);
      ObjectMove(chart_id,name,2,time3,price3);
      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
     }
//----
  }
//+------------------------------------------------------------------+
//| Creating a trend line|
//+------------------------------------------------------------------+
void CreateTline(long     chart_id,    // chart identifier
                 string   name,        // object name
                 int      nwin,        // window index
                 datetime time1,       // time of 1 price level
                 double   price1,      // 1 price level
                 datetime time2,       // time of price level 2
                 double   price2,      // 2 price level
                 color    Color,       // line colour
                 int      style,       // line style
                 int      width,       // line thickness
                 string   text)        // text
  {
//----
   ObjectCreate(chart_id,name,OBJ_TREND,nwin,time1,price1,time2,price2);
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
   ObjectSetInteger(chart_id,name,OBJPROP_BACK,false);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,true);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);
   ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true);
//----
  }
//+------------------------------------------------------------------+
//|| Resetting the trend line|
//+------------------------------------------------------------------+
void SetTline(long     chart_id,    // chart identifier
              string   name,        // object name
              int      nwin,        // window index
              datetime time1,       // time of 1 price level
              double   price1,      // 1 price level
              datetime time2,       // time of price level 2
              double   price2,      // 2 price level
              color    Color,       // line colour
              int      style,       // line style
              int      width,       // line thickness
              string   text)        // text
  {
//----
   if(ObjectFind(chart_id,name)==-1)
     {
      CreateTline(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,text);
     }
   else
     {
      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
      ObjectMove(chart_id,name,0,time1,price1);
      ObjectMove(chart_id,name,1,time2,price2);
      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
     }
//----
  }
//+------------------------------------------------------------------+
//| Finding the top fractal|
//+------------------------------------------------------------------+ 
bool upFrac(int cnt,const double &High[])
  {
//----
   for(int i=1; i<=int(frNum); i++)
     {
      if(frType==TYPE1)
        {
        //---- if strict condition and post or pre-bars are above-equal to neighbour - no fractal
         if(High[cnt+i]>=High[cnt+i-1] || High[cnt-i]>=High[cnt-i+1]) return(false);
        }
      else
        { 
        //---- if the condition is not strict and post or pre-bars are above the centre - no fractal
         if(High[cnt+i]>High[cnt] || High[cnt-i]>High[cnt]) return(false);
        }
     }
//----
   return(true);
  }
//+------------------------------------------------------------------+
//| Finding the bottom fractal|
//+------------------------------------------------------------------+ 
bool dwFrac(int cnt,const double &Low[])
  {
//----
   for(int i=1; i<=int(frNum); i++)
     {
      if(frType==TYPE1)
        { 
         //---- if strict condition and post or pre-bars are lower-equal to neighbour - no fractal
         if(Low[cnt+i]<=Low[cnt+i-1] || Low[cnt-i]<=Low[cnt-i+1]) return(false);
        }
      else
        { 
         //---- if the condition is not strict and post or pre-bars are below the centre - no fractal
         if(Low[cnt+i]<Low[cnt] || Low[cnt-i]<Low[cnt]) return(false);
        }
     }
//----
   return(true);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialisation function |
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason)
  {
//----
   Comment("");
   for(int i=0; i<3; i++)
     {
      string name="IPGDR_Lv"+string(i);
      ObjectDelete(0,name);
      ObjectDelete(0,name+"Up");
      ObjectDelete(0,name+"Dw");
      ObjectDelete(0,name+"Tx");
     }
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // amount of history in bars on the current tick
                const int prev_calculated,// amount of history in bars on the previous tick
                const datetime &time[],
                const double &open[],
                const double& high[],     // price array of price maxima for indicator calculation
                const double& low[],      // price array of price minima for indicator calculation
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---- check the number of bars for sufficiency for calculation
   if(rates_total<int(sBars)) return(0);

//---- indexing of elements in arrays, as in timeseries 
   ArraySetAsSeries(time,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);

   lastBars=rates_total;

//---- search for fractals, put them into the array, but not more than the allowed number of fractals.
   int sh=0;
   ArrayInitialize(aData,0);
   double lastExt=0;
   int total=int(sBars-2*frNum);
   int start=int(frNum+1);

   for(int i=start; i<total; i++)
     {
    // if(frMode!=MODE_LOW && upFrac(i,high))
    if(frMode!=FRACTAL_MODE::MODE_LOW && upFrac(i,high))
        { 
         //---- add vertices [except trough mode]
         //---- skip duplicate neighbouring vertices under non-strict condition
         if(!(frType!=TYPE1 && lastExt>0 && lastExt==high[i]))
           {
            aData[sh][0]=high[i];
            lastExt=high[i];
            sh++;
           }
        }

      if(sh>=240) break;

     // if(frMode!=MODE_HIGH && dwFrac(i,low))
     if(frMode!=FRACTAL_MODE::MODE_HIGH && dwFrac(i,low))
        { 
         //---- add depressions [except vertex mode]
         //---- skip duplicate neighbouring troughs under non-strict condition
         if(!frType!=TYPE1 && lastExt>0 && lastExt==low[i])
           {
            aData[sh][0]=low[i];
            lastExt=low[i];
            sh++;
           }
        }
      if(sh>=240) break;
     }

//---- determine the average candle spread and the corresponding tolerance
   double dlt,sHL=0;
   for(int i=1; i<=int(sBars); i++) sHL+=(high[i]-low[i]);
   sHL/=sBars;
   dlt=sHL*dlt_;

//---- define ratings for each level
   for(int i=0; i<sh; i++)
      for(int j=i+1; j<sh; j++)
         if(aData[j][0]>aData[i][0]-dlt && aData[j][0]<aData[i][0]+dlt)
           {
            aData[i][1]+=1;
            aData[j][1]+=1;
           }

//---- identify the three strongest levels
   double cur[2],tmp[2];
   aRes[0][0]=aData[0][0];
   aRes[0][1]=aData[0][1];

   for(int i=1; i<sh; i++)
     {
      cur[0]=aData[i][0];
      cur[1]=aData[i][1];

      if(cur[1]>aRes[0][1])
        { 
         //---- if the rating is greater than rank 1, then rank 1 becomes the current one
         tmp[0]=aRes[0][0];
         tmp[1]=aRes[0][1];
         aRes[0][0]=cur[0];
         aRes[0][1]=cur[1];
         cur[0]=tmp[0];
         cur[1]=tmp[1];
        }

      //---- if the rating is higher than the 2nd place and yet does not belong to the 1st place, replace the 2nd place
      if(cur[1]>aRes[1][1] && (cur[0]<aRes[0][0]-dlt || cur[0]>aRes[0][0]+dlt))
        {
         tmp[0]=aRes[1][0]; tmp[1]=aRes[1][1]; aRes[1][0]=cur[0]; aRes[1][1]=cur[1]; cur[0]=tmp[0]; cur[1]=tmp[1];
        }
      //---- if the rating is higher than the 3rd place and yet does not belong to the 1st and 2nd place, substitute the 3rd place
      if(cur[1]>aRes[2][1] && (cur[0]<aRes[0][0]-dlt || cur[0]>aRes[0][0]+dlt) && (cur[0]<aRes[1][0]-dlt || cur[0]>aRes[1][0]+dlt))
        {
         aRes[2][0]=cur[0];
         aRes[2][1]=cur[1];
        }
     }

   for(int i=0; i<3; i++)
     {
      double dwL=aRes[i][0]-dlt,upL=aRes[i][0]+dlt;
      string name="IPGDR_Lv"+string(i);

      SetChannel(0,name,0,time[24],upL,time[1],upL,time[24],dwL,BGColor[i],STYLE_SOLID,1,name);
      SetTline(0,name+"Up",0,time[24],upL,time[1],upL,TLColor[i],STYLE_SOLID,1,name+"Up");
      SetTline(0,name+"Dw",0,time[24],dwL,time[1],dwL,TLColor[i],STYLE_SOLID,1,name+"Dw");
      SetTextLabel(0,name+"Tx",0,time[32],upL+2*_Point,TLColor[i],STYLE_SOLID,1,DoubleToString(aRes[i][0],_Digits),"tahoma",14);
     }

//---- output comments
   string rem1="",rem2="";
   if(frType==TYPE2) rem1=rem1+"Classic ";
   else rem1=rem1+"Strictly ";

   rem1=rem1+string(frNum*2+1)+"-bar fractals.";
  // if(frMode==MODE_LOW) rem1=rem1+", troughs";
 if(frMode==FRACTAL_MODE::MODE_LOW) rem1=rem1+", bottoms.";
   //else if(frMode==MODE_HIGH) rem1=rem1+", vertex";
else if(frMode==FRACTAL_MODE::MODE_HIGH) rem1=rem1+", uppers.";
   rem1=rem1+"\n Found."+string(sh)+" fractal(s) \n Strongest levels ";

   StringConcatenate(rem2,aRes[0][0],"[",aRes[0][1],"], ",
                     aRes[1][0],"[",aRes[1][1],"], ",aRes[2][0],"[",aRes[2][1],
                     "], error ±",NormalizeDouble(dlt/_Point,1)," paragraph(s)");
   Comment(rem1+rem2);

//----
   ChartRedraw(0);
//---- 
   return(rates_total);
  }
//+------------------------------------------------------------------+