Time/Price to window coords and vice versa

 

Hi MQLers,

please advise: I am looking for a way to put a label (or even a text) above a specified bar so that it is positioned a specified number of pixel above the bar's high and moves along the window as new bars form. I guess the key to this funcionality is through being able to transform pixel coords to time/price coords and/or vice versa, and what I have been able to find throughout the web was fairly contradicting.. Could anybody help?

Mac 

 

I wouldn't advise using Labels and positions in pixels for your application  . . .   instead find the height of the window in pixels and convert your pixel offset into a price,  then place your text based on time and price . . .  it will then move correctly if the Y axis is resized.

 

To get the window dimensions in pixels use something like this . . .

 

#import "user32.dll"
int GetWindowRect(int hWnd, int rect[4]); 
#import


int WindowDims[4];         // used to store window coordinates from "user32.dll"

int SizeX = 800;          // x axis is width  (pixels measured from left to right)
int SizeY = 600;          // y axis is height (pixels measured from top to bottom)


   if (IsDllsAllowed()) 
   {
   GetWindowRect(WindowHandle(Symbol(), Period()), WindowDims);
   SizeX = WindowDims[2] - WindowDims[0];
   SizeY = WindowDims[3] - WindowDims[1];
   }
 
Well, this is exactly what I meant, only the most interesting part is missing ..:-) Of course it is easy to GetWindowsRect, but how to get the pixel offset of a price, that is where I am hoping to get soon ... :-)
 
Martinigue:
Well, this is exactly what I meant, only the most interesting part is missing ..:-) Of course it is easy to GetWindowsRect, but how to get the pixel offset of a price, that is where I am hoping to get soon ... :-)
Divide the number of pixels of the height by the max-min price.  That gives you price per pixel.
 
Oh ...I think I am getting there ( at last ..:-D), so please bear with me for a couple of moments more. So it seems that what I have been missing is that the max-min price duo, these have nothing to do with the price displayed by the bars, but rather the 'price window' being shown by the 'y-axis' ..is this correct? And if that one is cleared, I still do not get it how I could pick an arbitrary price to put my text on ..? While having just the OHLC, how could I state 'put this text like 10 pixels above High[15]?
 
Martinigue:
Oh ...I think I am getting there ( at last ..:-D), so please bear with me for a couple of moments more. So it seems that what I have been missing is that the max-min price duo, these have nothing to do with the price displayed by the bars, but rather the 'price window' being shown by the 'y-axis' ..is this correct? And if that one is cleared, I still do not get it how I could pick an arbitrary price to put my text on ..? While having just the OHLC, how could I state 'put this text like 10 pixels above High[15]?

"max-min price duo, these have nothing to do with the price displayed by the bars, but rather the 'price window' being shown by the 'y-axis' ..is this correct?"  -  correct.

If there are 600 pixels from the top of the window to the bottom and the price max to min is 103.788 to 103.120  (EURJPY) then 10 pixels as a price will be ( (103.788 - 103.120) / 600 ) * 10  add this to your bar high. 

 

So getting on with your calculation:

 yCoordBuffer = ( (103.788 - 103.120) / 600 ) * 10, then into CreateObject goes

High[15] + yCoordBuffer? 

 
Martinigue:

So getting on with your calculation:

 yCoordBuffer = ( (103.788 - 103.120) / 600 ) * 10, then into CreateObject goes

High[15] + yCoordBuffer? 

Yes,  ObjectCreate()  or ObjectSet() if you are re-using objects.  Your variable would be better named:  yPriceBuffer,  or even yPriceOffset . . . buffer means something else in mql4
 
Excellent! Now in closing - I would like to ask you about the reasons behind the very first line of yours here: "I wouldn't advise using ...". Why wouldn't you and why do you consider this method a better one for this achievement?
 
Martinigue:
Excellent! Now in closing - I would like to ask you about the reasons behind the very first line of yours here: "I wouldn't advise using ...". Why wouldn't you and why do you consider this method a better one for this achievement?
If you use a pixel position using a Label object then you have to use pixels for the X and Y dimensions,  as your chart moves when new bars are added you would need to re-calculate the position for each new bar . . .  also if the chart is scrolled the label will not move and would be out of position . . .  also if the Y axis scale is adjusted your label would not move and could end up being a long way from the bar it is meant to accompany.

You should try both methods and see which is better for you.
 
Thank you very much, RaptorUK, your help is greatly appreciated!
Reason: