Candle in series

 

Hello, I need some help in code line 83 for a logic which will continue the consecutive candles until a bull candle's low is broken by a wick.

#property copyright "W.I.K, Copyright 2023"
#property link      "https://www.mql5.com"
#property version   "1.00"
#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);
}
//+------------------------------------------------------------------+

 Current Output : 

What i am expecting: 

So i want consecutive candles to continue even if there is an inside bar

but want to break when last bull candle's low broken by any candle.

I request to share some logic in line 83 

thanks

 

i think your check is upside down

You are moving from past to present

The trend is set by the bar you are processing

And then you go back and check prices

There should be some retention of the latest high low and its distance from the current bar that is processing 

and you'd check on each bar only

 
Lorentzos Roussos #:

i think your check is upside down

You are moving from past to present

The trend is set by the bar you are processing

And then you go back and check prices

There should be some retention of the latest high low and its distance from the current bar that is processing 

and you'd check on each bar only

thanks, i will see.

Reason: