Flexible Fractal Channel

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

//|                                     Flexible Fractal Channel.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.001"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   4

//--- 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

//--- plot Line Up

#property indicator_label3  "Line UP"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_DASHDOT

#property indicator_width3  1

//--- plot Line DOWN

#property indicator_label4  "Line DOWN"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrBlue

#property indicator_style4  STYLE_DASHDOT

#property indicator_width4  1

//--- input parameters

input group             "Fractals"

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

input uchar                InpNumRight    = 4;     // 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[];

double   LineUpBuffer[];

double   LineDnBuffer[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LineUpBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,LineDnBuffer,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,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

//--- Set labels for the line

   string text="("+IntegerToString(InpNumLeft)+","+IntegerToString(InpNumRight)+")";

   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;

   if(prev_calculated==0)

     {

      limit=InpNumLeft;

      ArrayInitialize(UpperBuffer,EMPTY_VALUE);

      ArrayInitialize(LowerBuffer,EMPTY_VALUE);

     }

   for(int i=limit; i<rates_total-InpNumRight; 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];

         LineUpBuffer[i]=high[i];

        }

      else

        {

         UpperBuffer[i]=EMPTY_VALUE;

         LineUpBuffer[i]=LineUpBuffer[i-1];

        }

      //---

      if(fractal_dn_left && fractal_dn_right)

        {

         LowerBuffer[i]=low[i];

         LineDnBuffer[i]=low[i];

        }

      else

        {

         LowerBuffer[i]=EMPTY_VALUE;

         LineDnBuffer[i]=LineDnBuffer[i-1];

        }

      //---

     }

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

     {

      LineUpBuffer[i]=0.0;

      LineDnBuffer[i]=0.0;

     }

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

   return(rates_total);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---