How to calculate number of horizontal lines(obj_hline)?

 
int Hlines()
        {
        int numberofhlines;
        for(int i=ObjectsTotal(0,0,0)-1;i>0;i--)                        //I cannot get correct parameters for ObjectsTotal() so I can loop through all parameters
                {
                int type=ObjectGetInteger(i,0,OBJPROP_TYPE,0);          //I cannot assign the OBJPROP_TYPE to an int "type" so i can get the type of a selected object
                if(type==OBJ_HLINE)
                   {
                   numberofhlines=numberofhlines+1;
                   }
                
                }
        return numberofhlines;
        }

Hi. I am trying to follow a function that calculates number of buy Positions that works properly, and create a function that will calculate number of HLINE objects i have on my main screen on the current chart. But I fail to get it right.

On this one, I tried to

1. Loop through all the objects on the current symbol

2. Get the type of all the objects

3. If the type of the object is HLINE, i add 1 to a variable "numberofhlines" to get the number of HLINEs. 

4. I then return the value of those HLINES. But the problem here is, i get '543 155 680' instead of the number of HLINEs i have on the screen. Please help.

 

Code:

//+------------------------------------------------------------------+
//|                                                     Script 1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   Print(CalculateHLines());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CalculateHLines(void)
  {
   int  count_hlines=0;
   for(int i=ObjectsTotal(ChartID(),0,OBJ_HLINE)-1; i>=0; i--)
      count_hlines++;
//---
   return(count_hlines);
  }
//+------------------------------------------------------------------+
Files:
Script_1.mq5  3 kb
 
Vladimir Karputov:

Code:

Thank you very much sir. If I may ask, are there any books on MQL programming for MQL4 or 5? Besides the MQL documentation.

 
sir7williams :

Thank you very much sir. If I may ask, are there any books on MQL programming for MQL4 or 5? Besides the MQL documentation.

MQL5.com is the largest information repository. Use Documentation, Articles, Forum and CodeBade for learning.

 
Posted code
   int  count_hlines=0;
   for(int i=ObjectsTotal(ChartID(),0,OBJ_HLINE)-1; i>=0; i--)
      count_hlines++;
Simplified
   int  count_hlines=ObjectsTotal(ChartID(),0,OBJ_HLINE);