Loop to count consecutive bull or bear bars - page 2

 
int Consecutive(int Count) {
    // Declare arrays to hold open and close prices
    double openPrices[];
    double closePrices[];

    // Load open and close prices for the last 'Count' bars
    CopyOpen(_Symbol, PERIOD_H1, 0, Count, openPrices);
    CopyClose(_Symbol, PERIOD_H1, 0, Count, closePrices);

    int BB = 0;
    int consecutiveBull = 0;
    int consecutiveBear = 0;
    int lastDirection = 0;  // 1 for bull, -1 for bear

    // Iterate through the loaded prices
    for (int i = 0; i < Count; i++) {
        if (closePrices[i] > openPrices[i]) {
            if (lastDirection == 1) {
                consecutiveBull++;
            } else {
                consecutiveBull = 1; // Reset and start new count
                lastDirection = 1;
            }
            BB = MathMax(BB, consecutiveBull); // Keep track of maximum consecutive bulls
        } else if (closePrices[i] < openPrices[i]) {
            if (lastDirection == -1) {
                consecutiveBear++;
            } else {
                consecutiveBear = 1; // Reset and start new count
                lastDirection = -1;
            }
            BB = MathMin(BB, -consecutiveBear); // Keep track of maximum consecutive bears (as negative value)
        }
    }

    return BB;  // Return the max consecutive count (positive for bulls, negative for bears)
}
 
Dua Yong Rew:

Besides using loop to count consecutive bull or bear bar, is there any more efficient methods?


this is an example.

//+------------------------------------------------------------------+
//|                           Scirpt-Count-Consecutive-Bull-Bear.mq5 |
//|                                      Copyright 2024,Rosh Jardine |
//|                        https://www.mql5.com/en/users/roshjardine |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024,Rosh Jardine"
#property link      "https://www.mql5.com/en/users/roshjardine"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
   {
      //---
      int count_in = 0;
      count_in = CONSECUTIVE_BB(true,150);
      Print("highest bull consecutive = ",IntegerToString(count_in));
      count_in = CONSECUTIVE_BB(false,150);
      Print("highest bear consecutive = ",IntegerToString(count_in));

   }
//+------------------------------------------------------------------+
int CONSECUTIVE_BB(bool param_count_bull_bo,int param_look_back_in)
   {
      int look_back_in = (param_look_back_in<2) ? 3 : param_look_back_in;
      //--- for testing purpose only
      string pattern_text = "";
      for (int i=1;i<=look_back_in;i++)
         {
            if (iClose(_Symbol,PERIOD_CURRENT,i)>iOpen(_Symbol,PERIOD_CURRENT,i))
               {
                  pattern_text += "a";
               }
            else 
               {
                  if (iClose(_Symbol,PERIOD_CURRENT,i)<iOpen(_Symbol,PERIOD_CURRENT,i))
                     {
                        pattern_text += "b";
                     }
                  else 
                     {
                        pattern_text += "c";
                     }  
               }                 
         }
      Print("pattern_text=",pattern_text); 
      string consecutive_text = ""; string last_consecutive_text = ""; string highest_consecutive_text = "";
      if (param_count_bull_bo)
         {
            for (int i=1;i<look_back_in-1;i++)
               {
                  if (
                        iClose(_Symbol,PERIOD_CURRENT,i)>iOpen(_Symbol,PERIOD_CURRENT,i) &&
                        iClose(_Symbol,PERIOD_CURRENT,i+1)>iOpen(_Symbol,PERIOD_CURRENT,i+1)
                     )
                     {
                        consecutive_text += "a";
                        last_consecutive_text = consecutive_text;
                     }
                  else 
                     {
                        if (StringLen(last_consecutive_text)>StringLen(highest_consecutive_text))
                           {
                              Print("last_consecutive_text bull=",last_consecutive_text);     
                              highest_consecutive_text = last_consecutive_text;
                           }
                        consecutive_text = "";
                        i++;
                     }   
                  
               }
            Print("highest_consecutive_text bull=",highest_consecutive_text);     
         }
      else 
         {
            for (int i=1;i<look_back_in-1;i++)
               {
                  if (
                        iClose(_Symbol,PERIOD_CURRENT,i)<iOpen(_Symbol,PERIOD_CURRENT,i) &&
                        iClose(_Symbol,PERIOD_CURRENT,i+1)<iOpen(_Symbol,PERIOD_CURRENT,i+1)
                     )
                     {
                        consecutive_text += "b";
                        last_consecutive_text = consecutive_text;
                     }
                  else 
                     {
                        if (StringLen(last_consecutive_text)>StringLen(highest_consecutive_text))
                           {
                              Print("last_consecutive_text bear=",last_consecutive_text);     
                              highest_consecutive_text = last_consecutive_text;
                           }
                        consecutive_text = "";
                        i++;
                     }   
                  
               }
            Print("highest_consecutive_text bear=",highest_consecutive_text);     
         }   
      //--- include the first predecessor +1, count only successors return string length
      return(StringLen(highest_consecutive_text)+1);
   }
  
     
 
 

Thank you all for the new knowledge 

I didn't know there are so much to be learnt for a simple function counting consecutive bars

Reason: