Add description to a horizontal line using program code Metatrader 5

 
Hi,

I have a mql5 code/program that creates horizontal lines (OBJ_HLINE).I am able to set other parameters of the horizontal line like name,font size,color among other parameters via code but cannot set the object description name through any function that I am aware of and I have to manually click on the properties of the line and add my desired text on Description on the textbox highlighted in yellow as shown in image attached.

Since I use my program to make many horizontal lines,its tedious having to manually add on each line the description on textbox as explained above and was hoping if one can give my some code syntax that I can add to my program to add descriptions as I create the lines using my code.

Thanks
 

Documentation on MQL5: Object Functions / ObjectSetString

ENUM_OBJECT_PROPERTY_STRING

OBJPROP_TEXT

Description of the object (the text contained in the object)

string

Documentation on MQL5: Object Functions / ObjectSetString
Documentation on MQL5: Object Functions / ObjectSetString
  • www.mql5.com
ObjectSetString - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Documentation on MQL5: Object Functions / ObjectSetString

ENUM_OBJECT_PROPERTY_STRING

OBJPROP_TEXT

Description of the object (the text contained in the object)

string

Thanks alot.It works perfectly


Hi,

I am passing double datatype onto function as below where the price variable represents a price point on a MT5 chart

double price;
ObjectSetString(chart_ID,name,OBJPROP_TEXT,price);

During compilation I get a warning implicit conversion from 'number' to 'string'  and I am ok with that as I expect the function ObjectSetString to convert the number to string and the number is displayed as string on my charts.

However,for easy reading of the converted string I would like to have the converted number into a string with comma separators so that if for instance I pass number 15577.5 onto the function,I would like it to be displayed  with comma separators as 15,577.5 on the chart instead of the 15577.5 displayed currently.

How can I add such comma separators?

Thanks.

 
Joe Trader #: I am passing double datatype onto function as below where the price variable represents a price point on a MT5 chart

As the name of the ObjectSetString function states, it expects the parameter to be a "string", not a "double".

So, an implicit typecast will be applied if you use any other datatype besides a "string".

If you require a specific format, then use a proper conversion function such as DoubleToString or StringFormat.

However, if you require any kind of specialised formatting, then you will have to write your own formatting function.

Also, please use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Joe Trader #:

Thanks alot.It works perfectly


Hi,

I am passing double datatype onto function as below where the price variable represents a price point on a MT5 chart

During compilation I get a warning implicit conversion from 'number' to 'string'  and I am ok with that as I expect the function ObjectSetString to convert the number to string and the number is displayed as string on my charts.

However,for easy reading of the converted string I would like to have the converted number into a string with comma separators so that if for instance I pass number 15577.5 onto the function,I would like it to be displayed  with comma separators as 15,577.5 on the chart instead of the 15577.5 displayed currently.

How can I add such comma separators?

Thanks.

Not sure if there is a simpler way, but you could write a function like this

string getCommaSepNumStr(double num, int decimals)
  {
   string  numStr       = DoubleToString(num, decimals);
   long    numRadix     = (int) floor(num);
   string  numRadixStr  = IntegerToString(numRadix);
   string  numDecimalStr   = StringSubstr(numStr, StringFind(numStr, "."));
   string  numCommaStr  = "";
   int     commasAdded  = 0;
   for(int i = StringLen(numRadixStr); i >= 0; i--)
     {

      if(((StringLen(numCommaStr) - commasAdded) % 3 == 0) && (StringLen(numCommaStr) > 0))
        {
         numCommaStr =  "," + numCommaStr;
         commasAdded++;
        }
      numCommaStr = StringSubstr(numRadixStr, i, 1) + numCommaStr;
     }

   numCommaStr += numDecimalStr;

   return(numCommaStr);
  }
 
R4tna C #:
string getCommaSepNumStr(double num, int decimals)   {    string  numStr       = DoubleToString(num, decimals);    long    numRadix     = (int) floor(num);    string  numRadixStr  = IntegerToString(numRadix);    string  numDecimalStr   = StringSubstr(numStr, StringFind(numStr, "."));    string  numCommaStr  = "";    int     commasAdded  = 0;    for(int i = StringLen(numRadixStr); i >= 0; i--)      {       if(((StringLen(numCommaStr) - commasAdded) % 3 == 0) && (StringLen(numCommaStr) > 0))         {          numCommaStr =  "," + numCommaStr;          commasAdded++;         }       numCommaStr = StringSubstr(numRadixStr, i, 1) + numCommaStr;      }    numCommaStr += numDecimalStr;    return(numCommaStr);   }
WOW Thanks.This function works 100% perfectly.

Thanks a million.I really appreciate it R4tna
 
Joe Trader #:
WOW Thanks.This function works 100% perfectly.

Thanks a million.I really appreciate it R4tna

Small correction 

change the line 

   numCommaStr += numDecimalStr;

to 

   numCommaStr +=  (iv_decimals > 0)? numDecimalStr : "";

There was a error if you use 0 decimal places

Reason: