ObjectGetInteger() not getting the information correctly

 

Hello,

I've been all morning stuck with this problem. My indicator has a function to draw a rectangle label and a label so it shows a label with background. The problem comes that when I use the ObjectGetInteger to get the xSize of the label to apply it to the rectangle label it doesn't work. Tried to debug many times and the value was always 0 except if I redraw the chart before getting the information. Even then, sometime it didn't work. The labels in which it happenned were random, not always the same. So I'm really lost not knowing what could be the problem.
Moreover, this function was working fine before I rewrite the code to do it in multiple charts at the same time which is the same but doing it in loop.
Here is the code. The two lines before setting it was just to know while debugging if the info was right or not, in the string case is always right, in the x_size case is not, depending on chartRedraw() and somehting random I can't find:

void CreateRectangleWithText(long chartIdentifier, string _id, int x, int y, int ySize, color bgColor, string text){
                  
         string rectID="R_"+_id;
         ObjectCreate(chartIdentifier, rectID, OBJ_RECTANGLE_LABEL, 0, 0, 0);
         ObjectSetInteger(chartIdentifier, rectID, OBJPROP_XDISTANCE, x);
         ObjectSetInteger(chartIdentifier, rectID, OBJPROP_YDISTANCE, y);
         ObjectSetInteger(chartIdentifier, rectID, OBJPROP_YSIZE, ySize);
         ObjectSetInteger(chartIdentifier, rectID, OBJPROP_BGCOLOR, bgColor);
         ObjectSetInteger(chartIdentifier, rectID, OBJPROP_BORDER_TYPE, BORDER_FLAT);
         
         
         string textID="T_"+_id;
         ObjectCreate(chartIdentifier, textID, OBJ_LABEL, 0, 0, 0);
         ObjectSetInteger(chartIdentifier, textID, OBJPROP_XDISTANCE, x-1);
         ObjectSetInteger(chartIdentifier, textID, OBJPROP_YDISTANCE, y+2);
         ObjectSetString(chartIdentifier, textID, OBJPROP_TEXT, " "+text);
         ObjectSetInteger(chartIdentifier, textID, OBJPROP_COLOR, GetTextColor(bgColor));
         ObjectSetString(chartIdentifier, textID, OBJPROP_FONT, "Arial Black");
         ObjectSetInteger(chartIdentifier, textID, OBJPROP_FONTSIZE, 8);      

         //ChartRedraw(chartIdentifier);
         long textXSize=ObjectGetInteger(chartIdentifier, textID, OBJPROP_XSIZE);
         string textText=ObjectGetString(chartIdentifier, textID, OBJPROP_TEXT);
         ObjectSetInteger(chartIdentifier, rectID, OBJPROP_XSIZE, ObjectGetInteger(chartIdentifier, textID, OBJPROP_XSIZE));
         
      }

And here is the loop where I am doing it

long currChart=ChartFirst();
         while(currChart!=-1){        
            if(ChartSymbol(0)==ChartSymbol(currChart)){ 
               list[currPos].Draw(currChart, true);
               DrawOptions(currChart, list[currPos].objID);
               ChartRedraw(currChart);
            }
            currChart=ChartNext(currChart);
         }
 

After some debugging and rewriting the code from scratch from a backup where it worked, I found that when it was working I was drawing the options up to 6 times because I put the line inside another loop, which shouldn't have been, and after every loop there was a ChartRedraw().

So, this gets me to a conclusion, it might be wrong though. The xSize of a LABEL won't change until the label is drawn by a ChartRedraw() because I am guessing it needs to draw the text to calculate its size. Only doing one ChartRedraw() is not enough because it gets to the ObjectGetInteeger line before the label has been drawn. After some tests I've seen that I need to do at least 2 times the CreateOptions which is a bunch of recrtangles with labels to get it well drawn. So it draws it wrong the first time, but the second time it "updates" with the correct information.

Has anyone experienced the same issue? Any idea to solve this without having to loop just to "win some time" to get the correct xSize? Because that doesn't seem a good solution for me. The only viable solution I see would be calculating myself the xSize with the length of the string, the font size and the font. Any other possible solutions?

Thank you.

 
After some digging I found that this problem was already solved in another post. Sorry I didn't not search enough.

https://www.mql5.com/en/forum/35582

Any moderator feel free to delete this post if considered necessary. Thank you and sorry for the inconvinience.
the size of text object label - How to write codes to Custom Indicator?
the size of text object label - How to write codes to Custom Indicator?
  • 2014.08.19
  • Irwan Adnan
  • www.mql5.com
But when the chart is rendered and show object, this value become to be right. So, my guess is chart should be rendered by an internal mt4 routine and then we can get such values. How to write these codes to custom indicator