AsymmetricFractalsNotRedraw

Author: Copyright 2018-2021, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
AsymmetricFractalsNotRedraw
ÿþ//+------------------------------------------------------------------+

//|        AsymmetricFractalsNotRedraw.mq5 (barabashkakvn's edition) |

//|                   Copyright 2018-2021, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018-2021, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.002"

#property description "Original: https://www.mql5.com/ru/code/20120"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot UP

#property indicator_label1  "Fractal Up"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot DN

#property indicator_label2  "Fractal Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- input parameters

input uint        InpNumBefore   = 4;     // Number of bars on the left

input uint        InpNumAfter    = 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   BufferUP[];

double   BufferDN[];

//--- global variables

int      num_before;

int      num_after;

int      k;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   num_before=int(InpNumBefore<1 ? 1 : InpNumBefore);

   num_after=int(InpNumAfter<1 ? 1 : InpNumAfter);

   k=(InpDoNotRedraw)?1:0;

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDN,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);

//--- Set labels for the line

   string text="("+IntegerToString(InpNumBefore)+","+IntegerToString(InpNumAfter)+")";

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

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

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"AsymmetricFractals("+(string)num_before+","+(string)num_after+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

//---

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

  {

//--- checking for the minimum number of bars for calculation

   int max=num_before+num_after;

   if(rates_total<max)

      return 0;

//--- checking and calculating the number of calculated bars

   int limit=rates_total-prev_calculated;

   if(InpDoNotRedraw && limit==0)

     {

      BufferUP[0]=EMPTY_VALUE;

      BufferDN[0]=EMPTY_VALUE;

      return(rates_total);

     }

   if(limit>1)

     {

      limit=rates_total-max-2;

      ArrayInitialize(BufferUP,EMPTY_VALUE);

      ArrayInitialize(BufferDN,EMPTY_VALUE);

     }

//--- setting Array Indexing as Timeseries

// time[0] -> D'2019.01.01 23:00:00', time[rates_total-1] ->D'2020.09.16 01:00:00'

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

//--- calculation of the indicator

//--- cycle from 'rates_total' to '0'

   for(int i=limit; i>=k && !IsStopped(); i--)

     {

      if(i>1)

        {

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

            int d=0;

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

            int f=0;

        }

      bool fractal_up=true;

      bool fractal_dn=true;

      //--- find

      if(num_before>1 && num_after>1)

        {

         for(int n=1; n<=num_before; n++)

           {

            if(high[i+num_after+n]>high[i+num_after])

               fractal_up=false;

            if(low[i+num_after+n]<=low[i+num_after])

               fractal_dn=false;

           }

        }

      else

        {

         for(int n=1; n<=num_before; n++)

           {

            if(high[i+num_after+n]>=high[i+num_after])

               fractal_up=false;

            if(low[i+num_after+n]<low[i+num_after])

               fractal_dn=false;

           }

        }

      for(int n=1 ; n<=num_after; n++)

        {

         if(high[i+num_after-n]>=high[i+num_after])

            fractal_up=false;

         if(low[i+num_after-n]<=low[i+num_after])

            fractal_dn=false;

        }

      //--- fractals

      if(fractal_up)

         BufferUP[i+num_after]=high[i+num_after];

      else

         BufferUP[i+num_after]=EMPTY_VALUE;

      if(fractal_dn)

         BufferDN[i+num_after]=low[i+num_after];

      else

         BufferDN[i+num_after]=EMPTY_VALUE;

     }

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