Problem with setting OBJ_HLINE coordinates

 

Hi guys,

I have a problem with setting the x and y coordinates of an HLINE. I want to do it like a Label, that the Line is constantly placed for example 80 pixel on the x-axis and y-axis in a subwindow. Like when you set the parameters of a TextLabel on the mt4 platform.  

Here is my code:

 ObjectCreate("Line",OBJ_HLINE,1,0,5000);  //5000 is just a random number because I dont know what to type in else
 ObjectSet("Line",OBJPROP_XDISTANCE,80);
 ObjectSet("Line",OBJPROP_YDISTANCE,80);

Does anybody see the mistake?

Thanks for your help;) 

 

Some small exercising tasks here:

  • A line is described by a starting point and an ending point.
  • A point is described by a position in an X/Y/Z coordinate system (the example shows a 3d cartesian coordinate system). For your charting window you proably only need X/Y.
  • The charting window is build up on an X axis and an Y axis. The X axis describes the time and the Y axis describes the prize. Something similar applies for the sub windows such as Stochastics indicator etc.

Conclusion:

Now that we know that an OBJ_HLINE requires a starting and an eding point, something like this might come handy:

datetime time1 = TimeCurrent();
datetime time2 = time1 + (60 * 60);
double price1 = 1.10000;
double price2 = 1.13000;

ObjectCreate("Line", OBJ_TREND, 0, time1, price1, time2, price2);
ObjectSetInteger(0, "Line", OBJPROP_RAY_RIGHT, false);

The code above is not tested. Just written out of my mind. Now for your OBJ_HLINE. The HLINE object only needs the price because it spans the entire time of your chart. Therefore you only need the Y axis. Depending where your chart is (the main chart or the sub charts) you need to chose a value, that fits the price (e.g. to make the thing visible). So 5000 might be a far too high value. Look at the right of the Y axis of the chart window (sub window) and fill a value that fits there. E.g. for currency pairs (EURUSD) you can choose a value from the code above. If you run this in a sub window like a Stochastics indicator choose something between 0 and 100.

 

Hi aakcaagac,

thanks for your quick reply. I´m using the Volumes indicator so when I create a Line at the price level of 5000, it is created and i can see it too. My problem is, that i want a Line, which is no based on the price, but on pixel. So if i scroll back and the scale changes (because it is a flexible scale) the position (not the price) of the Line changes too. And exactly thats what I want to prevent.

I hope for your help.

Timerider 

 

This might come handy (ChartXYToTimePrice) ?

 
I will try. But another thought:this would also be possible if i would know the current scale price. So the "highest" scale, which is currently available.
 
But i dont know if the ChartXYToTimePrice function is helpful.
 

Well it's not really clear what your use case exactly is. What I understand now is, that you want your HLINE to stay where it is even if you scale the chart window up and down. So the price in the HLINE can change but the X/Y coordinates should stay e.g. if you want to place the HLINE 5 pixel from top then it has to stay there 5 pixels from top no matter if you scale up and down.

int x = 10;
int y = 10;
int window = 0;
datetime time = 0;
double price = 0;

ChartXYToTimePrice(0, x, y, window, time, price);

ObjectSet("Line", OBJPROP_PRICE1, price);

This is why I came up with the idea of using XYTo ... If you put this into the chart window event handler of your indicator, then whenever something happens, the event makes sure that your object is always set to x=10 / y=10. In this case you only use the y value which represents the price (which the XYTo ... automagically converts).

 
 

The ChartXYToTimePrice function works perfect. Thanks aakcaagac for your help. But I don't want the price of the main chart. Instead, I want the price of the Volumes chart. So I thought that I could just type in 1 at "window" instead of 0(for subwindow). But i doesn't work. Do you have an idea for solving this problem.

Best

Timerider 

 
int x = 10;           ---> user input
int y = 10;           ---> user input
int window = 0;       <--- function output
datetime time = 0;    <--- function output
double price = 0;     <--- function output

function output <-----------------------------.
function output <----------------------.      |
function output <----------------.     |      |
                                 |     |      |
user input --------------.       |     |      |
user input -----------.  |       |     |      |
user input --------.  |  |       |     |      |
                   |  |  |       |     |      |
                   v  v  v       |     |      |
ChartXYToTimePrice(0, x, y, window, time, price);

So you can't use 1 for the window variable. You might expect the desired functionality by changing the "0" to "1" ... "2" ... "3" ... "x" ...

Parameters
chart_id
[in]  Chart ID. 0 means the current chart.

x
[in]  The X coordinate. The origin is in the upper left corner of the main chart window.

y
[in]  The Y coordinate. The origin is in the upper left corner of the main chart window.

sub_window
[out]  The variable, into which the chart subwindow number will be written. 0 means the main chart window.

time
[out]  The time value on the chart, for which the value in pixels along the X axis will be received.

price
[out]  The price value on the chart, for which the value in pixels along the Y axis will be received.

 [1] ChartXYToTimePrice

 
I think I´ve got it. Thank you so much!
Reason: