How to Add Text above Horizontal Line

 

I cannot figure out how to add text just above the horizontal line drawn on the chart and centered in the chart window.  The following is the code I have written so far:

///////////////////////////////////////////////////////////////////////////

 ObjectCreate(0,"PrevDayHigh",OBJ_HLINE,0,TimeCurrent(),iHigh(Symbol(),1440,1));

      ObjectSet("PrevDayHigh",OBJPROP_COLOR,Blue);

      ObjectSet("PrevDayHigh",OBJPROP_WIDTH,1);

      ObjectSet("PrevDayHigh",OBJPROP_STYLE,STYLE_SOLID);

      ObjectSet("PrevDayHigh",OBJPROP_SELECTABLE,false);

      ObjectSet("PrevDayHigh",OBJPROP_HIDDEN,true);

      ObjectSet("PrevDayHigh",OBJPROP_BACK,true);

   //ObjectDelete(0,"PrevDayHigh");    

   

   

  // Create Text just above horizonal line and centered in window

  ObjectCreate("ObjName", OBJ_LABEL, 0, 0, 0);

   ObjectSetText("ObjName","Testing Text just above Horizontal Line and centered in window",7, "Verdana", Red);

   ObjectSet("ObjName",OBJPROP_YDISTANCE,50);  // I don't know how to do this with price

///////////////////////////////////////////////////////////////////////////

 
Alan Ray Northam:

The function you're looking for is ChartTimePriceToXY.

bool  ChartTimePriceToXY(
   long           chart_id,     // Chart ID
   int            sub_window,   // The number of the subwindow
   datetime       time,         // Time on the chart
   double         price,        // Price on the chart
   int&           x,            // The X coordinate for the time on the chart
   int&           y             // The Y coordinates for the price on the chart
   );

If you need help with the function, just ask, but basically you plug in the date and price on the chart, along with two variables (int x, y), and when it's done, the x and y variables will contain the x and y coordinates on the chart of the date and price you plugged in.

Another solution would be to enable "Show Object Description" for the chart and set the text to the object's description, but that can get messy when other objects have a description. Still, worth knowing and mentioning.

Side note: consider learning how to use the Standard Library, more specifically in your case, CChartObjectLabel.

All you need to be aware of is to call the Attach function if the object exists (it will return a bool value of true if it does) and Detach on DeInit so the object doesn't get deleted (usual reasons to detach are REASON_CHARTCHANGE, REASON_ACCOUNT, REASON_CLOSE, and maybe REASON_RECOMPILE and REASON_PARAMETERS, depending on your situation).

ChartTimePriceToXY - Chart Operations - MQL4 Reference
ChartTimePriceToXY - Chart Operations - MQL4 Reference
  • docs.mql4.com
ChartTimePriceToXY - Chart Operations - MQL4 Reference
 
Alexander Martinez #:

The function you're looking for is ChartTimePriceToXY

Another solution would be to enable "Show Object Description" for the chart and set the text to the object's description, but that can get messy when other objects have a description. Still, worth knowing and mentioning.

Side note: consider learning how to use the Standard Library, more specifically in your case, CChartObjectLabel.

All you need to be aware of is to call the Attach function if the object exists (it will return a bool value of true if it does) and Detach on DeInit so the object doesn't get deleted (usual reasons to detach are REASON_CHARTCHANGE, REASON_ACCOUNT, REASON_CLOSE, and maybe REASON_RECOMPILE and REASON_PARAMETERS, depending on your situation).

Thanks for leading me in the right direction!  I have been searching the library but could not figure out which function to use.

 
Alan Ray Northam #:

Thanks for leading me in the right direction!  I have been searching the library but could not figure out which function to use.

You're welcome. :)

Take care and good luck!

 
First you create a HLine and then a Label. HLine moves with the chart, a Label does not. You want a text object.
 
Alexander Martinez #:

The function you're looking for is ChartTimePriceToXY.

If you need help with the function, just ask, but basically you plug in the date and price on the chart, along with two variables (int x, y), and when it's done, the x and y variables will contain the x and y coordinates on the chart of the date and price you plugged in.

Another solution would be to enable "Show Object Description" for the chart and set the text to the object's description, but that can get messy when other objects have a description. Still, worth knowing and mentioning.

Side note: consider learning how to use the Standard Library, more specifically in your case, CChartObjectLabel.

All you need to be aware of is to call the Attach function if the object exists (it will return a bool value of true if it does) and Detach on DeInit so the object doesn't get deleted (usual reasons to detach are REASON_CHARTCHANGE, REASON_ACCOUNT, REASON_CLOSE, and maybe REASON_RECOMPILE and REASON_PARAMETERS, depending on your situation).

Hi.

I try to update x,y values in the Ontick function, but the x,y values are not updating. If I change the trendline, the text does not follow :(

Can you help me?

 
erdos.jozsef95 #: I try to update x,y values in the Ontick function, but the x,y values are not updating. If I change the trendline, the text does not follow :(
Show your code! We can't help you otherwise.
 
erdos.jozsef95 #: If I change the trendline, the text does not follow :(

Of course, the text doesn't follow. They are two separate objects. If you move one, you move the other.

Reason: