Consecutive Candle Indicator

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Consecutive Candle Indicator
ÿþ//+------------------------------------------------------------------+

//|                                 Consecutive Candle Indicator.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Bullish

#property indicator_label1  "Bullish"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrDeepSkyBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Bearish

#property indicator_label2  "Bearish"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrTomato

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input uchar    InpBullishCode    = 159;         // Arrow code of arrows 'Bullish'(font Wingdings)

input uchar    InpBearishCode    = 159;         // Arrow code of arrows 'Bearish'(font Wingdings)

input int      InpBullishShift   = 10;          // Vertical shift of arrows 'Bullish' in pixel

input int      InpBearishShift   = 10;          // Vertical shift of arrows 'Bearish' in pixel

//--- indicator buffers

double   BullishBuffer[];

double   BearishBuffer[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,BullishBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,BearishBuffer,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_ARROW,InpBullishCode);           // plot 'Bullish'

   PlotIndexSetInteger(1,PLOT_ARROW,InpBearishCode);           // plot 'Bearish'

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

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-InpBullishShift);   // shift 'Bullish'

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT, InpBearishShift);   // shift 'Bearish'

//--- an empty value for plotting, for which there is no drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//---

   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<3)

      return(0);

   int limit=prev_calculated-1;

//--- first start

   if(prev_calculated==0)

      limit=0;

//--- main loop

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

     {

      int count_bullish=0;

      int count_bearish=0;

      int j=i;

      int trend=0; // '1' -> 'i' bar bullish, '-1' -> 'i' bar bearish

      double highest=DBL_MIN;

      double lowest=DBL_MAX;

      if(open[j]<close[j])

         trend=1;

      else

         if(open[j]>close[j])

            trend=-1;

         else

            continue;

      for(j; j>=0; j--)

        {

         if(trend==1)

           {

            if(open[j]<close[j])

              {

               if(high[j]>highest)

                  highest=high[j];

              }

            else

               break;

           }

         else // trend==-1

           {

            if(open[j]>close[j])

              {

               if(low[j]<lowest)

                  lowest=low[j];

              }

            else

               break;

           }

        }

      if(trend==1)

        {

         for(int k=i; k>j; k--)

           {

            BullishBuffer[k]=highest;

            BearishBuffer[k]=0.0;

           }

        }

      else

         if(trend==-1)

           {

            for(int k=i; k>j; k--)

              {

               BullishBuffer[k]=0.0;

               BearishBuffer[k]=lowest;

              }

           }

     }

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

   return(rates_total);

  }

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

Comments