Flexible Fractal

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Flexible Fractal
ÿþ//+------------------------------------------------------------------+

//|                                             Flexible Fractal.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.003"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Fractal_UP

#property indicator_label1  "Fractal UP"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Fractal_DOWN

#property indicator_label2  "Fractal DOWN"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input group             "Fractals"

input uchar                InpNumLeft     = 2;     // Number of bars on the left

input uchar                InpNumRight    = 2;     // Number of bars on the right

input bool                 InpDoNotRedraw = true;  // Do not redraw

input uchar                InpCodeUpArrow = 119;   // Arrow code for 'Fractal Up' (font Wingdings)

input uchar                InpCodeDnArrow = 119;   // Arrow code for 'Fractal Down' (font Wingdings)

input int                  InpShift       = 10;    // Vertical shift of arrows in pixel

//--- indicator buffers

double   UpperBuffer[];

double   LowerBuffer[];

//---

int      m_offset_bars;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

   m_offset_bars=(InpDoNotRedraw)?1:0;

//--- indicator buffers mapping

   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,InpCodeUpArrow);

   PlotIndexSetInteger(1,PLOT_ARROW,InpCodeDnArrow);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-InpShift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,InpShift);

//--- sets drawing line empty value--

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- Set labels for the line

   string text="("+IntegerToString(InpNumLeft)+","+IntegerToString(InpNumRight)+","+(InpDoNotRedraw?"true":"false")+")";

   PlotIndexSetString(0,PLOT_LABEL,"Fractal Up"+text);

   PlotIndexSetString(1,PLOT_LABEL,"Fractal Down"+text);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Flexible Fractals"+text);

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| 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[])

  {

   /*

   Left = 3, Right = 3

   'i' - current bar

       Fractal UP              No Fractal UP                 Fractal UP

            i +1  +2 +3             i +1  +2 +3                   i +1  +2 +3

            x                                                     x

            |                       |  |                       |  |

         |  |  |                 |  |  |  |                 |  |  |  |

      |  |  |  |  |           |  |  |  |  |  |           |  |  |  |  |  |

   |  |  |  |  |  |  |     |  |  |  |  |  |  |  |     |  |  |  |  |  |  |  |





   |  |  |  |  |  |  |     |  |  |  |  |  |  |  |     |  |  |  |  |  |  |  |

      |  |  |  |  |           |  |  |  |  |  |           |  |  |  |  |  |

         |  |  |                 |  |  |  |                 |  |  |  |

            |                       |  |                       |  |

            x                                                     x

   -3 -2 -1  i             -3 -2 -1  i                -3 -2 -1     i

       Fractal DN              No Fractal DN                 Fractal DN

   */

//---

   if(rates_total<InpNumLeft+InpNumRight)

      return(0);

   int limit=prev_calculated-1-InpNumRight-m_offset_bars;

   if(prev_calculated==0)

     {

      limit=InpNumLeft/*-m_offset_bars*/;

      ArrayInitialize(UpperBuffer,0.0);

      ArrayInitialize(LowerBuffer,0.0);

     }

   for(int i=limit; i<rates_total-InpNumRight-m_offset_bars; i++)

     {

      /*if(high[i]==high[i-1])

         Print("high[i]==high[i-1], ",time[i]);

      if(low[i]==low[i-1])

         Print("low[i]==low[i-1], ",time[i]);*/

      bool fractal_up_left    = true;

      bool fractal_up_right   = true;

      bool fractal_dn_left    = true;

      bool fractal_dn_right   = true;

      //---

      for(int j=1; j<=InpNumLeft; j++)

        {

         if(fractal_up_left)

            if(high[i]<high[i-j])

               fractal_up_left=false;

         if(fractal_dn_left)

            if(low[i]>low[i-j])

               fractal_dn_left=false;

        }

      for(int j=1; j<=InpNumRight; j++)

        {

         if(fractal_up_right)

            if(high[i]<=high[i+j])

               fractal_up_right=false;

         if(fractal_dn_right)

            if(low[i]>=low[i+j])

               fractal_dn_right=false;

        }

      if(fractal_up_left && fractal_up_right)

         UpperBuffer[i]=high[i];

      else

         for(int j=0; j<=InpNumRight+m_offset_bars; j++)

            UpperBuffer[i+j]=0.0;

      if(fractal_dn_left && fractal_dn_right)

         LowerBuffer[i]=low[i];

      else

         for(int j=0; j<=InpNumRight+m_offset_bars; j++)

            LowerBuffer[i+j]=0.0;

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments