How to plot a label on top of a Horizontal line

 

Hi everyone, I would like to plot a label on the same price value of an horizontal line and on the extreme right of the chart(that's the point where I need help), ideally the result would look like this: 

Example

As you can see, the yellow line represent prev. High and Low of H1 while white ones H4. If the previous H1 bar was bullish, then a label "UP" will appear next to the H1 prev. High hline, vice versa for low... and of course the same apply to H4. 

   string H1_H = "H1 prev. High";
   double H1_H_pr = iHigh(NULL,60,1);
   string H1_L = "H1 prev. Low";
   double H1_L_pr = iLow(NULL,60,1);
   string H4_H = "H4 prev. High";
   double H4_H_pr = iHigh(NULL,240,1);
   string H4_L = "H4 prev. Low";
   double H4_L_pr = iLow(NULL,240,1);
   
   ObjectCreate(0,H1_H,OBJ_HLINE,0,TimeCurrent(),0); //This is a simple custom function that apply desired input with ObjectCreate
   ObjectCreate(0,H1_L,OBJ_HLINE,0,TimeCurrent(),0);

   PreviousHL(H1_H,iHigh(NULL,60,1),line_style,H1_color);
   PreviousHL(H1_L,iLow(NULL,60,1),line_style,H1_color);
  
   ObjectCreate(0,H4_H,OBJ_HLINE,0,TimeCurrent(),0);
   ObjectCreate(0,H4_L,OBJ_HLINE,0,TimeCurrent(),0);

   PreviousHL(H4_H,iHigh(NULL,240,1),line_style,H4_color); 
   PreviousHL(H4_L,iLow(NULL,240,1),line_style,H4_color);



   ObjectCreate(0,"H1_label", OBJ_TEXT, 0, 0,0); //Here's where the problem dtar
   if (iOpen(NULL,60,1) < iClose(NULL,60,1) )
   {
      ObjectSet("H1_label",OBJPROP_PRICE1,H1_H_pr);
      ObjectSetText("H1_label","UP",10,"Arial",H1_color);
   } 
   
   if (iOpen(NULL,60,1) > iClose(NULL,60,1)) 
   {
      ObjectSet("H1_label",OBJPROP_PRICE1,H1_L_pr);
      ObjectSetText("H1_label","DW",10,"Arial",H1_color);
   }   
   
    
   
   ObjectCreate(0,"H4_label", OBJ_TEXT, 0, 0,0);

   if (iOpen(NULL,240,1) < iClose(NULL,240,1) )
   {
      ObjectSet("H4_label",OBJPROP_PRICE1,H4_H_pr);
      ObjectSetText("H4_label","UP",10,"Arial",H4_color);
   } 
   
   if (iOpen(NULL,240,1) > iClose(NULL,240,1)) 
   {
      ObjectSet("H4_label",OBJPROP_PRICE1,H4_L_pr);
      ObjectSetText("H4_label","DW",10,"Arial",H4_color);
   }

Can you help me? Thank's. 

 

I resolved by using this: 

   datetime text_placement = iTime(NULL,0,0)+12*100;
   ObjectCreate(0,"H1_label", OBJ_TEXT, 0, text_placement,0);
   ObjectSet("H1_label",OBJPROP_TIME1,text_placement);
   
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.06.10
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

Why do you keep calling it a label when it is a text object?
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

A label is different. It does not move as price shifts. The text will move, and that is not what you want.

Convert the price/time[0] coordinate to XY and position the label there.

 
William Roeder #:

Why do you keep calling it a label when it is a text object?
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

A label is different. It does not move as price shifts. The text will move, and that is not what you want.

Convert the price/time[0] coordinate to XY and position the label there.

Hi, sorry I’m a noob and found the difference only after submitting the question.

I also tried using a label and converting price value to x-axis value but with no success, I searched around forums but found nothing. I see there is function ChartPriceTimetoXY but it return a boolean result and I don’t understand how to use it to my purpose. Perhaps if you have some time to illustrate how to do I would be very grateful to you.

Thanks for your reply by the way ✌️

 
ironhak #: but with no success, … and I don’t understand how to use it
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

    There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  2. #define CURRENT_WINDOW 0
    #define MAIN_WINDOW    0
    datetime t=…; double p=…; // Price of the HLine
    int      x,y; ChartTimePriceToXY(CURRENT_WINDOW, MAIN_WINDOW, t, p, x, y);
    MoveLabel(name, x, y);

 

Hey man, thanks now I understand how that function works. Here's what I did: 

   ObjectCreate(0,"H1_label",OBJ_LABEL,0,0,0);
   int x,y; ChartTimePriceToXY(0,0,TimeCurrent(),iHigh(NULL,60,1),x,y);
   ObjectSet("H1_label",OBJPROP_XDISTANCE,10);
   ObjectSet("H1_label",OBJPROP_YDISTANCE,y);
   ObjectSetString(0,"H1_label",OBJPROP_TEXT,"Thank's man  :)");
   ObjectSet("H1_label", OBJPROP_CORNER, 1);

Here’s the desired result:

Result successfully achieved

Sorry if my questions are dumb. Anyway, I have one more of them (sorry I’m still learning), if instead of “TimeCurrent()” I put 0 inside the function, like this:

ChartTimePriceToXY(0,0,0,iHigh(NULL,60,1),x,y);

Then the label does not place correctly, it goes not to the desired price level, see here:

Problem

As you can see it’s messed up. Why this happen? On the object itself I didn’t put the value of x, I just typed “10” on the ObjectSet because it’s the value that better fit my screen… why the value x influence the position of the label even dough it’s not inserted into the object?

Hope my question is clear enough. Thanks.

 
ironhak #: if instead of “TimeCurrent()” I put 0 inside the function,Then the label does not place correctly, i

Wrong. The label has been correctly placed. Zero means 1970. Your label is off-screen as you requested.

 
William Roeder #:

Wrong. The label has been correctly placed. Zero means 1970. Your label is off-screen as you requested.

Yes I get that 0 means 1970, but:

   ObjectCreate(0,"H1_label",OBJ_LABEL,0,0,0);
   int x,y; ChartTimePriceToXY(0,0,0 ,
© ,x,y);
	//Now x=1970 and y= prev H1 high. At this moment the label have no coordinates, they are stiill 0,0
   ObjectSet("H1_label",OBJPROP_XDISTANCE,10); //With this I apply a value of 10 to x-axis 
   ObjectSet("H1_label",OBJPROP_YDISTANCE,y); //With this I insert the y value
	//As you can see only y has been used, x is not used and still it mess up the location of label
   ObjectSetString(0,"H1_label",OBJPROP_TEXT,"Thank's man  :)");
   ObjectSet("H1_label", OBJPROP_CORNER, 1);
Edit: ok now I think I get it.... The ChartTimePricetoXY function apply "iHigh(NULL,60,1)" on the point of time I requested, hence it goes to search the previous High on H1 of the first bar in 1970... Now I get it, thank's for your time. 
 
   ObjectSet("H1_label",OBJPROP_XDISTANCE,10); //With this I apply a value of 10 to x-axis 

Now you move the label to 1970+10 seconds.