Loading…

Price Axis Label Collision Manager

Any tool that draws more than one price label near the chart's right edge - multiple take-profit levels, pivot points, session highs and lows, a row per pending order - breaks the same way the moment two of those levels sit closer together than one text row. The labels overlap into an unreadable smear, and the problem gets worse the more zoomed in the chart is, because more price levels end up compressed into the same pixel band. This library fixes it once. PalArrange converts each label's price to its current pixel Y using the chart's own ChartTimePriceToXY mapping - so it always matches the live price-per-pixel scale, including after a zoom or a window resize, rather than a hand-computed scale that goes stale. It then sorts the labels top to bottom and runs a two-pass greedy declutter: a forward pass pushes any label that is too close to the one above it further down, then a backward pass pulls the whole block back up so it stays centred on the original price cluster instead of drifting toward the bottom of the chart. This is the same technique used for map and chart label placement generally - not globally optimal, but it produces the same "spread the cluster outward" layout a human would draw by hand, with no external dependency and no meaningful performance cost for the handful of labels a typical trading tool needs to place. PalArrangeAndDraw wraps the whole pipeline into one call that also creates or updates the OBJ_LABEL chart objects, named prefix + "_" + index so a matching PalRemoveLabels(prefix) call can clean up exactly that set later. Notes and limitations - The declutter pass is a greedy two-pass heuristic, not a globally optimal solver. If there are genuinely more labels than fit the visible window without overlap even fully spread out, some overlap can remain - it minimises it rather than guaranteeing zero in every case. - PalPriceToY calls ChartTimePriceToXY with the chart's current time as the X anchor; the returned Y depends only on price and subwindow, not on which on-screen time was used, so this is safe for any visible chart. - If a price genuinely cannot be converted (e.g. a subwindow index that does not exist), that label's yPixel is left at -1 and it is skipped by PalArrangeAndDraw rather than drawn at a wrong position. - Labels are plain OBJ_LABEL chart objects anchored by corner and pixel distance, not tied to the price scale object MetaTrader itself draws - they will not automatically track a change of chart price scale or window height between one call and the next; call PalArrangeAndDraw again after any such change (e.g. on every OnCalculate/OnTick refresh). - Always prefix your label names and call PalRemoveLabels(prefix) on deinit - the objects are not automatically cleaned up.

Script · MT5 · v1.0 · Free

About Price Axis Label Collision Manager

Price Axis Label Collision Manager is a script for MetaTrader 5. It has 3 settings, such as Min gap pixels, Cluster count and Cluster spread points. MQL source code included.

Files in the download: PriceAxisLabelManager.mq5.mqh, PriceAxisLabelManager.txt, PriceAxisLabelManagerDemo.ex5, PriceAxisLabelManagerDemo.mq5.

Settings

SettingDefaultWhat it does
Min gap pixels14minimum vertical gap enforced between labels
Cluster count5how many demo labels to pack close together
Cluster spread points15how many points apart the raw levels are

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Pivot Points · Trading Sessions · Pending Orders · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria