Need help in moving the OBJPROP_TEXT with the OBJ_HLINE

 

Hi all

I have a function in MLQ4 to create a horizontal line and I am adding in a Text to the line. However, I want this to be moved to the right side of the chart (attached). 

I have tried setting up the anchor and alignment but none of these work as the text is being formed with the HLINE Object not seperatly.

//Create a horizontal line on a chart
void drawDashedHorizontalLine(string id, double price, color clr, bool displayLineName=false) {
   ENUM_LINE_STYLE style         = STYLE_DASHDOT;
   
   ObjectCreate(0, id, OBJ_HLINE, 0, 0, price);
   ObjectMove(0, id, 0, 0, price);
   ObjectSetInteger(0, id, OBJPROP_STYLE, style); // Line style
   ObjectSetInteger(0, id, OBJPROP_COLOR, clr); // Line color
   if (displayLineName) { 
      ObjectSetString(0, id, OBJPROP_TEXT, id);
   }
}

Any help would be much appriciated

Cheers

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
chart.png  21 kb
 

OBJPROP_XDISTANCE

102

int

Integer value to set/get anchor X distance object property in pixels (see note)

OBJPROP_YDISTANCE

103

int

Integer value is to set/get anchor Y distance object property in pixels (see note)

OBJPROP_PRICE

Price coordinate

double    modifier=number of anchor point

Use ObjectSetInteger and  ObjectSetDouble to change those values.

https://docs.mql4.com/objects/objectsetinteger

Also you can use chart width property to find the right coordination.

https://docs.mql4.com/chart_operations/chartgetinteger

Object Properties - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
Object Properties - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Object Properties - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
 
Yashar Seyyedin #:

OBJPROP_XDISTANCE

102

int

Integer value to set/get anchor X distance object property in pixels (see note)

OBJPROP_YDISTANCE

103

int

Integer value is to set/get anchor Y distance object property in pixels (see note)

OBJPROP_PRICE

Price coordinate

double    modifier=number of anchor point

Use ObjectSetInteger and  ObjectSetDouble to change those values.

https://docs.mql4.com/objects/objectsetinteger

Also you can use chart width property to find the right coordination.

https://docs.mql4.com/chart_operations/chartgetinteger

Thanks Yashar

I have even tried that and it doesn't work. I modified the code below but the text doesn't move from its position.

//Create a horizontal line on a chart
void drawDashedHorizontalLine(string id, double price, color clr, bool displayLineName=false) {
   ENUM_LINE_STYLE style         = STYLE_DASHDOT;
   
   ObjectCreate(id, OBJ_HLINE, 0, timeCurrent, price);
   ObjectMove(id, 0, timeCurrent, price);
   ObjectSetInteger(0, id, OBJPROP_STYLE, style); // Line style
   ObjectSetInteger(0, id, OBJPROP_COLOR, clr); // Line color
   if (displayLineName) {
      int height  = ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
      int width   = ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);
      ObjectSetString(0, id, OBJPROP_TEXT, id);
      ObjectSetDouble(0, id, OBJPROP_PRICE, price);
      ObjectSetInteger(0, id, OBJPROP_XDISTANCE, width - 1000);
      ObjectSetInteger(0, id, OBJPROP_YDISTANCE, height); 
   }
}
Thanks
 
mehakatharkhichi #:

Thanks Yashar

I have even tried that and it doesn't work. I modified the code below but the text doesn't move from its position.

Thanks
Create a separate object for your text.
 
Yashar Seyyedin #:
X and Y coordinates don't work with OBJ_TEXT. I have also tried with OBJ_LABEL but I am struggling to find the Y coordinate of the price on the chart as the label needs to go where the price is on the chart.
 
   ObjectCreate("HLINE", OBJ_HLINE, 0, TimeCurrent(), Bid);
   ObjectCreate("TEXT1", OBJ_TEXT, 0, TimeCurrent(), Bid);
   double price   = ObjectGetDouble(ChartID(),"HLINE",OBJPROP_PRICE);
   datetime date   = ObjectGetInteger(ChartID(),"HLINE",OBJPROP_TIME);
   ObjectSetInteger(ChartID(), "TEXT1", OBJPROP_TIME, date);
   ObjectSetDouble(ChartID(), "TEXT1", OBJPROP_PRICE, price);
 
Yashar Seyyedin #:

Thanks for sharing.

However my question is to move the text to the extreme right of the chart not with the TimeCurrent() which I am unable to figure out on how can I do it.

Please see attached chart image. Thanks heaps

Files:
chart.png  21 kb
 
   ObjectCreate("HLINE", OBJ_HLINE, 0, TimeCurrent(), Bid );
   ObjectCreate("TEXT1", OBJ_LABEL, 0, 0, 0);
   
   datetime time = ObjectGetInteger(ChartID(),"HLINE",OBJPROP_TIME);
   double price = ObjectGetDouble(ChartID(),"HLINE",OBJPROP_PRICE);
   
   int chart_width = ChartGetInteger(ChartID(), CHART_WIDTH_IN_PIXELS, 0);
   ObjectSetInteger(ChartID(), "TEXT1", OBJPROP_XDISTANCE, chart_width-50);

   int x=0;
   int y=0;
   ChartTimePriceToXY(ChartID(), 0, time, price, x, y);
   ObjectSetInteger(ChartID(), "TEXT1", OBJPROP_YDISTANCE, y);
 
Yashar Seyyedin #:
Excellent! Thanks heaps. That worked. Much appreciated.