Loading…

Chart Coordinate Mapper

MQL4 already ships ChartTimePriceToXY and ChartXYToTimePrice, but they only answer a single question each - one point, one conversion. The moment a drawing tool needs "which bar is under this pixel", "what pixel is this bar at", "what price range is visible in this subwindow right now" or "which bars are currently on screen", that logic gets derived by hand, and it is easy to get subtly wrong: bar width in pixels is not constant once a chart has been scrolled or zoomed, and a subwindow's visible price range is not exposed as a plain ChartGetDouble property. This library builds those answers on top of the platform's own two functions rather than re-deriving pixel geometry independently - the zoom level, scroll position and per-subwindow price scale are all things the terminal already tracks internally, and reusing ChartTimePriceToXY / ChartXYToTimePrice for every derived query means this stays correct as the chart moves instead of drifting out of sync with what is actually on screen. ChartXPixelToBarIndex and ChartBarIndexToXPixel translate between a pixel column and a bar index (0 = current bar, matching Close[]/Time[] indexing) by going through time, not through a fixed pixels-per-bar constant. ChartVisibleBarRange reads the two chart edges to report which bar indices are actually on screen right now. ChartSubWindowPriceRange sums the pixel heights of every subwindow above the target one to find its top edge, then probes two pixels just inside its top and bottom borders to read the visible price range for that specific subwindow - the step a custom indicator drawing tool needs before it can place an object at a screen- relative position rather than a price-relative one. Notes and limitations - All functions read the chart as it is at the moment of the call - none of them cache anything, so results reflect scrolling and zooming done since the last call automatically, but also mean repeated calls in a tight loop re-query the terminal each time. - ChartSubWindowPriceRange probes pixels two rows in from each border rather than the exact edge, because the very edge pixel can fail to resolve to a price on some terminal builds. This means the reported range is a close approximation of the true visible range, not pixel-exact. - ChartXPixelToBarIndex/ChartBarIndexToXPixel assume the main price window (subwindow 0). Bars are not meaningfully addressed the same way inside an indicator subwindow, so these two are not usable there. - All functions operate on chartId as given - pass 0 (or ChartID()) for the current chart; a different chart ID only makes sense from a script or EA that is deliberately inspecting another open chart window.

Script · MT4 · v1.0 · Free

About Chart Coordinate Mapper

Chart Coordinate Mapper is a script for MetaTrader 4. MQL source code included.

Files in the download: ChartCoordinateMapper.mqh, ChartCoordinateMapper.txt, ChartCoordinateMapperDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria