Objectstotal function does strange things

 

Hello,

I am looping through all horizontal lines of the current chart using the ObjectsTotal function: int totalLines = ObjectsTotal(0, 0, OBJ_HLINE);

I have 3 horizontal lines in my chart that I want the Expert Advisor to trade from. Then I have 2 extra lines that show a treshold value.

What I am doing is, that I use the nearest support line as a line to buy from and the nearest resistance line as a line to sell from.

The code works beautifully as long as I don't really open positions. The moment I open a position with a stop loss and a take profit, the ObjectsTotal function still counts the same number of lines, but the names of the lines have changed. Of my 3 horizontal lines, 2 are suddenly missing, and also the treshold lines are missing from the ObjectName function. The lines are still visible on the chart, but as I loop through all horizontal lines and for each step read the ObjectName, not all my lines show up. It shows however a line with a name that signifies the Buy or Sell line. And same old buy and sell lines.

It seems like the ObjectsTotal function does not properly work, from the moment a position is opened.

Does anybody know a workaround?

Thank you in advance,

Kind regards,

Ruud


   // read the price levels, by looping through all horizontal lines
   
   // is there a resistance line, above the current price?
   smallestPriceValueAbove = -1; 
   largestPriceValueBelow = -1; 
   string priceLevelName; 
   
   // find the nearest resistance level
   int totalLines = ObjectsTotal(0, 0, OBJ_HLINE);
   Print ("There are [", totalLines, "] lines");
   for (int counter = 0; counter < totalLines; counter++) {
      priceLevelName = ObjectName(0, counter, 0,-1);
      
      if (StringFind(priceLevelName, "Horizontal Line", 0) != -1) {
         double priceLevelLine = ObjectGetDouble(0, priceLevelName, OBJPROP_PRICE);
         
         if ((priceLevelLine > currentBid) && ((priceLevelLine <= smallestPriceValueAbove) || (smallestPriceValueAbove == -1))) {
            smallestPriceValueAbove = priceLevelLine;
         }
      }
   }
   Print("Nearest resistance level = [", smallestPriceValueAbove, "]");
 
  1. Ruud Op: It seems like the ObjectsTotal function does not properly work,

    How To Ask Questions The Smart Way. 2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2.    int totalLines = ObjectsTotal(0, 0, OBJ_HLINE);
       ⋮
          priceLevelName = ObjectName(0, counter, 0,-1);
    Your first line finds that there are three HLINEs. Then you ask for the first, second and third object, not the first, second and third HLINE.