Search code examples
objectlabelmql4drawrectangle

MQL4 Drawing a Dynamic Rectangle_Label with a Text in It


I am trying to draw a Rectangle Label with a text in it every tick.. I want a text to fit exactly in to a Rectangle_Label.. As a text i am using Label.. But cant get it to work exactly.. It is not correctly situated..

In Fact i would like to create a class that would do it all in one... Just like a rectangle with text in it that would be always having same co ordinance and size etc..

Any help would be greatly appreciated...

 bool createRectangleLabel(long chart_ID,string name,string labelName,int shift,double price,string text,double xSize,double ySize,double xOffSet,double yOffSet,double xDistance,double yDistance)
     {
      if(ObjectCreate(chart_ID,labelName,OBJ_RECTANGLE_LABEL,0,TimeCurrent()-shift,price))
    {
     Print(xDistance+"  "+yDistance);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_BGCOLOR,clrBlack);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_XDISTANCE,xDistance);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_YDISTANCE,yDistance);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_YSIZE,ySize);
     ObjectSetInteger(chart_ID,labelName,OBJPROP_XSIZE,xSize);
     ObjectSetString(chart_ID,labelName,OBJPROP_TEXT,text);
     ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,ANCHOR_CENTER);

     return true;
    }
  else
    {
     Print("createRectangleLabel return error code: ",GetLastError());
     Print("+--------------------------------------------------------------+");
     return false;
    }
 }
   bool createLineText(long chart_ID,string name,string labelName,int shift,double price,string text)
 {
  int xDistance=0;
  int yDistance=0;
  int xSize,xOffSet;
  int ySize,yOffSet;
  bool i=ChartTimePriceToXY(chart_ID,0,TimeCurrent(),price,xDistance,yDistance);

  if(ObjectCreate(chart_ID,name,OBJ_LABEL,0,TimeCurrent()-shift,price))
    {
     ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,clrWhite);
     ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,xDistance);
     ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,yDistance);
     ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
     ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,ANCHOR_CENTER);
     ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clrWhite);
     ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,10);
     xSize = ObjectGet(name,OBJPROP_XSIZE);
     ySize = ObjectGet(name,OBJPROP_YSIZE);
     xOffSet = ObjectGet(name,OBJPROP_XOFFSET);
     yOffSet = ObjectGet(name,OBJPROP_YOFFSET);
     TextGetSize(name,xSize,ySize);
     createRectangleLabel(chart_ID,name,labelName,shift,price,text,xSize,ySize,xOffSet,yOffSet,xDistance,yDistance);
     return true;
    }
  else
    {
     Print("createLineText return error code: ",GetLastError());
     Print("+--------------------------------------------------------------+");
     return false;
    }
 }

Solution

  • You're thinking along the right lines when you say that you'd like to create a class. Fortunately for you, the standard library already includes all the classes you need to make chart objects. Documentation

    Example Indicator:

    #property strict
    #property indicator_chart_window
    
    #include <ChartObjects\ChartObjectsTxtControls.mqh>
    
    class MyRectLabel : public CChartObjectRectLabel
    {
       CChartObjectLabel m_label;
    public:
       bool Create(long chart, const string name, const int window, 
                   const int X, const int Y, const int sizeX, const int sizeY)
       {
          if(!CChartObjectRectLabel::Create(chart,name,window,X,Y,sizeX,sizeY))
             return false;
          return m_label.Create(chart, name + "_", window, X + 8, Y + 12);
       }
       bool Color(const color clr){
          return m_label.Color(clr);
       }
       bool Description(const string text){
          return m_label.Description(text);
       }
       bool FontSize(const int size){
          return m_label.FontSize(size);
       }
       bool ToolTip(const string text){
          return (this.ToolTip(text) && m_label.Tooltip(text));
       }
    };
    //+------------------------------------------------------------------+
    MyRectLabel rect_label;
    //+------------------------------------------------------------------+
    int OnInit()
    {
       if(!rect_label.Create(0, "rlabel", 0, 5, 25, 100, 50)
          || !rect_label.BackColor(clrWhiteSmoke)
          || !rect_label.Description("LABEL!")
          || !rect_label.Tooltip("I am a rectangle label")
          || !rect_label.Color(clrBlack)
          || !rect_label.FontSize(18)
       )
          return INIT_FAILED;
       return INIT_SUCCEEDED;
    }
    //+------------------------------------------------------------------+
    int start()
    { 
       static double last_price = 0.;
       rect_label.Description(DoubleToString(Bid, _Digits));
       if(Bid > last_price)
          rect_label.Color(clrLimeGreen);
       else
          rect_label.Color(clrRed);
       last_price = Bid;
       return 0; 
    }