How to get x and y from Hline?

 

Solution by Keith Watford

ObjectCreate("Entry", OBJ_HLINE, 0, 0, Ask);
ObjectSet("Entry", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Entry", OBJPROP_COLOR, clrGreen);

double   DOT_P = ObjectGetDouble( 0,"Entry",OBJPROP_PRICE );
datetime DOT_T =TimeCurrent() ;

int      x,y;
if(!ChartTimePriceToXY(0,0,DOT_T,DOT_P,x,y))
Print("Error ",GetLastError());
else
Print ("X=",x,"   Y=",y);


*Input current date/time into ChartTimePriceToXY

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Positioning Constants
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Positioning Constants
  • www.mql5.com
Constants, Enumerations and Structures / Chart Constants / Positioning Constants - Reference on algorithmic/automated trading language for MetaTrader 5
 

GLYTioNyMUSkINtOldEs: H‌ow do I get get X and Y value to determine chart position for HLine. 

  1. You don't. A Hline has no time. You created it with a zero time (1970.) That is what you read back.

  2. ChartTimePriceToXY(0,0,DOT_T,DOT_P,x,y)
    Since DOT_T will always be zero, why do you think you can ever get an x coordinate?
 
GLYTioNyMUSkINtOldEs:

DOT_T is not 0

DOT_T has a value of 1970 xxxxxxxxx


What is the workaround?

DOT_T is 0.

ObjectCreate("Entry", OBJ_HLINE, 0, 0, Ask);

datetime 0 is 1 January, 1970.

There is no workaround.

Why would you need the x co-ordinate of a horizontal line?

 

I have deleted your new topic as it is related to this one.

Do not open multiple topics with the same subject.

 
GLYTioNyMUSkINtOldEs: DOT_T is not 0, and has a value of 1970.01.01 00:00:00

They are the same, EXACT, value. Online Conversion - Unix time conversion

 
Try this 
ChartRedraw(0);
ObjectGetDouble(0,"Entry",OBJPROP_PRICE,0);
 
Kok Hoong Cheang:

Is it possible just to get the y co-ordinate of HLine? Maybe there's a function I am not aware of.

Kok Hoong Cheang:
I want to create a label that moves with the Hline.

So you only need the Y co-ordinate

ObjectCreate("Entry", OBJ_HLINE, 0, 0, Ask);
ObjectSet("Entry", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Entry", OBJPROP_COLOR, clrGreen);

double   DOT_P = ObjectGetDouble( 0,"Entry",OBJPROP_PRICE );
datetime DOT_T =TimeCurrent() ;

int      x,y;
if(!ChartTimePriceToXY(0,0,DOT_T,DOT_P,x,y))
Print("Error ",GetLastError());
else
Print ("X=",x,"   Y=",y);
 
Kok Hoong Cheang: The label will not move with hline, 

You must move it. In chart event, if the Hline is dragged, get its price and move your label.

 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. I see your code. It uses getXY, which you already know will not work for hlines, because you have no time.
  3. I see your code. It uses getInteger(prop time), which you already know will not work for hlines, because they have no time.
  4. Stop pasting the same code everywhere and start thinking. If you don't have time from hlines, what can you use to get the Y from getXY
 
Keith Watford:

So you only need the Y co-ordinate

Kok Hoong Cheang:

still didn't work, the label doesn't move with the Hline when I drag it, 

The code that I posted was to enable you to get the Y co-ordinate.

 
Kok Hoong Cheang:

Thanks, 

I have input current time into the HLine so that I can get the Y co-ordinate later from ChartTimePriceToXY.

Now, the problem is, if I were right click on the HLine and change the price level, the label will move with the HLine.

If I were to drag the HLine, the x and y obtained from the HLine is 0,0. I don't understand why, because I have cater for OnChartEvent; even clicking on the chart doesn't change a thing (x and y obtained from a dragged-HLine is still 0,0)

I have already given you the solution but you have chosen to ignore it.