Help formatting OBJ_TEXT

 

I made a very rough indicator with ObjectCreate/OBJ_TEXT that floats next to the current bar and I have these problems with it:


- How do I position it? It is not just too close to the bar, it actually writes text over the bar and looks bad. I "fixed" that by padding the text it is supposed to print with a lot of spaces before the actual text, but it's a very ugly kludge in the code. I tried adding and tweaking the OBJPROP_XDISTANCE and OBJPROP_XOFFSET properties but they have no effect so I am obviously doing it wrong. What is the proper way of shifting the text towards the right so it sits farther grom the last bar?


- Not all text fits. The first version of it worked fine, then I added more text and now I see it doesn't fit. There seems to be a limit of about 18 characters give or take. Can I make room for more text?


- If I want a border, how do I set the style? The manual page has a long table of Identifier, Description and Property Types, but the Border Style table only has Identifier and Description, no Property Type. So I tried it like this:

ObjectCreate(nameVar, OBJ_TEXT, 0, Time[0], someVar);
ObjectSetText(nameVar, textVar, 16, "Times New Roman", EMPTY);
ObjectSet(nameVar, BORDER_RAISED);

That causes an error, wrong number of parameters. I guess it expects three parameters. What would the third parameter be?


TIA

 
//+------------------------------------------------------------------+
//| Creating Text object                                             |
//+------------------------------------------------------------------+
bool TextCreate(const long              chart_ID=0,               // chart's ID
                const string            name="Text",              // object name
                const int               sub_window=0,             // subwindow index
                datetime                time=0,                   // anchor point time
                double                  price=0,                  // anchor point price
                const string            text="Text",              // the text itself
                const string            font_type="Arial",        // font
                const int               fontsize=10,              // font size
                const color             clr=clrRed,               // color
                const double            angle=0.0,                // text slope
                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
                const bool              back=true,                // in the background
                const bool              selection=false,          // highlight to move
                const bool              hidden=true,              // hidden in the object list
                const long              z_order=0)                // priority for mouse click
{
//--- set anchor point coordinates if they are not set
//ChangeTextEmptyPoint(time,price);
//--- reset the error value
   ResetLastError();
//--- create Text object
   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))
      {
         Print(__FUNCTION__,
               ": failed to create \"Text\" object! Error code = ",GetLastError());
         return(false);
      }
//--- set the text
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font_type);
//--- set font size
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,fontsize);
//--- set the slope angle of the text
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the object by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
}
//+----------------------------------------------------------------------------+

The first parameter of ObjectCreate is the Chart ID.

You can use 0 for the current chart.

There is no border around a text object.

Documentation on MQL5: Chart Operations / ChartID
Documentation on MQL5: Chart Operations / ChartID
  • www.mql5.com
Chart Operations / ChartID - Reference on algorithmic/automated trading language for MetaTrader 5