Count objects

 

can anyone tell me what mistake I'm making here? this function should return the value of all red objects on the chart. but he just keeps counting and doesn't stop. 


int counter_zones_red(){
int object_all = ObjectsTotal(ChartID(),0,OBJ_RECTANGLE);
for (int i = 0; i<=object_all; i++){
  string object_name = ObjectName(ChartID(), i);
  
 if(ObjectGetInteger(ChartID(), object_name,OBJPROP_COLOR)==clrRed){
 counter_red = counter_red+1; 
   }
 }
 return counter_red;
}

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
you need set counter_red = 0 when start
 


do you think so? I declared the variable globally above

int counter_zones_red(){
int object_all = ObjectsTotal(ChartID(),0,OBJ_RECTANGLE);
counter_red = 0;
for (int i = 0; i<=object_all; i++){
  string object_name = ObjectName(ChartID(), i);
  
 if(ObjectGetInteger(ChartID(), object_name,OBJPROP_COLOR)==clrRed){
 counter_red = counter_red+1; 
   }
 }
 return counter_red;
}
 
Trinh Dat #:
you need set counter_red = 0 when start

I got it, thanks :)

 
You got the count of rectangles. But, you are getting names of all object types. ObjectName - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Example: you can set the window number and color:

//+------------------------------------------------------------------+
//|                                         Color Object Counter.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.00"
//--- input parameters
input color    InputColor  = clrRed;// color
input int      InpSubWindow= 0;     // window ('-1' all subwindows)
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(InpSubWindow<-1)
      return(INIT_PARAMETERS_INCORRECT);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   long chart_id=ChartID();
   int total=ObjectsTotal(chart_id,InpSubWindow,-1);
   int counter=0;
   for(int i=total-1; i>=0; i--)
     {
      string name=ObjectName(chart_id,i,InpSubWindow,-1);
      long clr=clrNONE;
      if(!ObjectGetInteger(chart_id,name,OBJPROP_COLOR,0,clr))
         return;
      if(clr==InputColor)
         counter++;
     }
//---
   Comment("There are ",counter," objects on the chart with the color ",InputColor);
  }
//+------------------------------------------------------------------+
Files:
 
Thanks for the replies :) I got it already :)
 
Please keep posting in this thread. No need to create duplicates and clones of topics.
 
OK no problem. Sorry if I messed something up, I haven't been on MQL5 that long. Here is the code again:
for (int i = ObjectsTotal(ChartID(),0,OBJ_HLINE)-1; i>=0; i--){

   string objects_lines = ObjectName(ChartID(), i);
   
   double price_green_down_position   = 0;
   double price_green_up_position     = 0; 
   double price_green_middle_position = 0;
   
   double price_red_down_position     = 0;
   double price_red_up_position       = 0; 
   double price_red_middle_position   = 0;
    
      if(ObjectGetInteger(0,objects_lines,OBJPROP_COLOR)==clrYellow)    price_green_down_position   = ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE);
      if(ObjectGetInteger(0,objects_lines,OBJPROP_COLOR)==clrBlack)     price_green_middle_position = ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE);
      if(ObjectGetInteger(0,objects_lines,OBJPROP_COLOR)==clrOrangeRed) price_green_up_position     = ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE);
      
      if(ObjectGetInteger(0,objects_lines,OBJPROP_COLOR)==clrBrown)     price_red_down_position     = ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE);
      if(ObjectGetInteger(0,objects_lines,OBJPROP_COLOR)==clrWhite)     price_red_middle_position   = ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE);
      if(ObjectGetInteger(0,objects_lines,OBJPROP_COLOR)==clrAqua)      price_red_up_position       = ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE);     
      
      if(PositionsTotal()==0 && Loss == Percent){
         if(ask <= price_green_up_position && ask >= price_green_middle_position && ask >= price_green_down_position) BUYPercent();
         if(bid <= price_red_up_position   && bid <= price_red_middle_position   && bid >= price_red_down_position  ) SELLPercent();
         
         }
       

}
 
Yango # Here is the code again:

#4 is your answer again.

 
William Roeder #:

#4 is your answer again.


okay, i saw it. I had already tried what you meant with all object types, and that doesn't work either.