Consecutive Bullish Candles Function logic problem

 

Hi ,, 

I appreciate your help for the below function that search in a range to extract the most number of bullish candles and return the first candle in the trend with an exception of maximum 2 bearish candles , in other words , if there were 7 candles in uptrend and there were 2 maximum bearish candles in between  it will ignore them and return the start of the bullish trend 

below is the code I am trying but getting only1 , it is a robot and there is a button to execute the function 

//+------------------------------------------------------------------+
//|                                              FunctionsTester.mq5 |
//|                                   Copyright 2023, ElGendiGoldMan |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, ElGendiGoldMan"
#property link      "https://www.mql5.com"
#property version   "1.00"

input int CandlesShift1 = 45; // Range to include 

int CandlesShift2 = CandlesShift1 -1; // Range to include 

input ENUM_TIMEFRAMES tf1= PERIOD_M30; // First Time Frame 
input ENUM_TIMEFRAMES tf2= PERIOD_H3; // Second Time Frame 
input color tf2S = clrYellow; // Time frame 2 Breasih color
input color tf2B = clrLimeGreen; // Time frame 2 Bullish color

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(1);
   ObjectCreate(0,"line",OBJ_VLINE,0,(iTime(Symbol(),PERIOD_CURRENT,1)),0);
   ObjectSetInteger(0,"line",OBJPROP_SELECTABLE,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  OnTick();
//DrawFutureCandles(
/// ==  Delete All objects ===
ObjectCreate(Symbol(),"consecutive",OBJ_BUTTON,0,0,0);
//--- set button coordinates
   ObjectSetInteger(Symbol(),"consecutive",OBJPROP_XDISTANCE,30);
   ObjectSetInteger(Symbol(),"consecutive",OBJPROP_YDISTANCE,60);

//--- set button size
   ObjectSetInteger(Symbol(),"consecutive",OBJPROP_XSIZE,150);
   ObjectSetInteger(Symbol(),"consecutive",OBJPROP_YSIZE,30);
//--- set the chart's corner, relative to which point coordinates are defined
   ObjectSetInteger(Symbol(),"consecutive",OBJPROP_CORNER,1); // عرض
   
//--- set the text
   ObjectSetString(Symbol(),"consecutive",OBJPROP_TEXT,"consecutive");
//--- set text font
/////////////////////*******************************************************
 
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
if(id==CHARTEVENT_OBJECT_CLICK)   
      // if Button was clicked
      { if(sparam=="consecutive")        {
      
      
                  // Chart CommentSHOW
Comment(findConsecutiveBullishCandles(1,50));

                        } 
                     
                     
                     
                     
                     
                     
       }}
  
  
//+------------------------------------------------------------------+


int findConsecutiveBullishCandles(int startIdx, int endIdx)
{
    int count = 0;
    int numBearishCandles = 0;
    int firstBullishCandleIdx = -1;
    
    for (int i = startIdx; i <= endIdx; i++)
    {
        // Check if the candle is bullish
        if (iOpen(Symbol(),PERIOD_CURRENT,i) < iClose(Symbol(),PERIOD_CURRENT,i))
        {
            // If this is the first bullish candle in the streak, save its index
            if (count == 0)
            {
                firstBullishCandleIdx = i;
            }
            
            // Increment the count of consecutive bullish candles
            count++;
            
            // Reset the count of bearish candles
            numBearishCandles = 0;
        }
        // Check if the candle is bearish
        else if (iOpen(Symbol(),PERIOD_CURRENT,i)  > iOpen(Symbol(),PERIOD_CURRENT,i))
        {
            // Increment the count of bearish candles
            numBearishCandles++;
            
            // If there are one or two bearish candles, allow the streak to continue
            if (numBearishCandles <= 2)
            {
                count++;
            }
            // If there are more than two bearish candles, reset the count and firstBullishCandleIdx
            else
            {
                count = 0;
                numBearishCandles = 0;
                firstBullishCandleIdx = -1;
            }
        }
        // If the candle is neutral, reset the count and firstBullishCandleIdx
        else
        {
            count = 0;
            numBearishCandles = 0;
            firstBullishCandleIdx = -1;
        }
        
        // If we have found a streak of consecutive bullish candles with the exception of one or two bearish candles, return the index of the first bullish candle in the streak
        if (count > 0 && numBearishCandles <= 2)
        {
            return firstBullishCandleIdx;
        }
    }
    
    // If we didn't find a streak, return -1
    return -1;
}


 
Say THX and take the idea created by Vladimir Karputov to get the counts of Bull-Bear bars from 1 to 7 ( except the current bar, and i added some lines to comment the counts of B/S's ) 

i added lines with labelled "...MNS" to show the counting results within the original source code. such as Comment("Count/Body of B/S for Last 1-7 bars "+ string(cnt_B)+" ("+DoubleToString(bodyB/_Point,0)+") - "
        +string(cnt_S)+" ("+DoubleToString(bodyS/_Point,0)+")");  // printing counts/Body added by MNS

Say THX and take the idea created by Vladimir Karputov to get the counts of Bull-Bear bars from 1 to 7 ( except the current bar, and i added some lines to comment the counts of B/S's ) 

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

//|                                                   Bar number.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"

....


            cnt_S=cnt_S+1; // count added by MNS

.....


            cnt_B=cnt_B+1; // count added by MNS

....


      Comment("Count of B/S for Last 1-7 bars "+ string(cnt_B)+" & "+string(cnt_S)+" ." ); // printing counts added by MNS

Files:
Bar_number.mq5  20 kb
 
@nail sertoglu #: Say THX and take the idea created by Vladimir Karputov to get the counts of Bull-Bear bars from 1 to 7 ( except the current bar, and i added some lines to comment the counts of B/S's )

Please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.

NB! Very important! DO NOT create a new post. EDIT your original post.

Reason: