Reverse Filling

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

//|                                              Reverse Filling.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.001"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   2

//--- plot Reverse High

#property indicator_label1  "Reverse High"

#property indicator_type1   DRAW_FILLING

#property indicator_color1  clrLimeGreen,clrLavender

//--- plot Reverse Low

#property indicator_label2  "Reverse Low"

#property indicator_type2   DRAW_FILLING

#property indicator_color2  clrLimeGreen,clrLavender

//--- indicator buffers

double   ReverseHighBuffer1[];

double   ReverseHighBuffer2[];

double   ReverseLowBuffer1[];

double   ReverseLowBuffer2[];

//---

bool     m_init_error               = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ReverseHighBuffer1,INDICATOR_DATA);

   SetIndexBuffer(1,ReverseHighBuffer2,INDICATOR_DATA);

   SetIndexBuffer(2,ReverseLowBuffer1,INDICATOR_DATA);

   SetIndexBuffer(3,ReverseLowBuffer2,INDICATOR_DATA);

//--- INDICATOR_EMPTY_VALUE (empty value) will not participate in calculation of

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   /*//--- set the display of the symbol

      PlotIndexSetString(0,PLOT_LABEL,"Reverse High;"+"Reverse Low;");*/

//---

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

  {

   if(rates_total<10)

      return(0);

   if(m_init_error)

      return(0);

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=3;

      for(int i=0; i<=limit; i++)

        {

         ReverseHighBuffer1[i]=0.0;

         ReverseHighBuffer2[i]=0.0;

         ReverseLowBuffer1[i]=0.0;

         ReverseLowBuffer2[i]=0.0;

        }

     }

   for(int i=limit; i<rates_total; i++)

     {

      //--- check: does the pattern UP exist?

      ReverseHighBuffer1[i]=0.0;

      ReverseHighBuffer2[i]=0.0;

      bool pattern_up_exist=(high[i]>high[i-1] && high[i-1]<high[i-2] && high[i-2]<high[i-3] &&

                             low[i]>low[i-1] && low[i-2]<low[i-3]);

      if(pattern_up_exist)

        {

         double max=DBL_MIN,min=DBL_MAX;

         for(int j=i-3; j<=i; j++)

           {

            if(high[j]>max)

               max=high[j];

            if(low[j]<min)

               min=low[j];

           }

         for(int j=i-3; j<=i; j++)

           {

            ReverseHighBuffer1[j]=max;

            ReverseHighBuffer2[j]=min;

           }

         continue;

        }

      //--- check: does the pattern DOWN exist?

      ReverseLowBuffer1[i]=0.0;

      ReverseLowBuffer2[i]=0.0;

      bool pattern_down_exist=(low[i]<low[i-1] && low[i-1]>low[i-2] && low[i-2]>low[i-3] &&

                               high[i]<high[i-1] && high[i-2]>high[i-3]);

      if(pattern_down_exist)

        {

         double max=DBL_MIN,min=DBL_MAX;

         for(int j=i-3; j<=i; j++)

           {

            if(high[j]>max)

               max=high[j];

            if(low[j]<min)

               min=low[j];

           }

         for(int j=i-3; j<=i; j++)

           {

            ReverseLowBuffer1[j]=min;

            ReverseLowBuffer2[j]=max;

           }

        }

     }

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

   return(rates_total);

  }

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

Comments