Loading…

Draggable Line Handler

A "drag this to set your stop" line looks trivial and has two traps that show up the first time someone tests it on a real account. The first is the event model. CHARTEVENT_OBJECT_DRAG fires once, when the drag finishes, not continuously while the mouse button is held down. Code written against a stream of drag events, or against polling the price every tick, either never reacts or reacts on every tick regardless of whether the line moved. Dragged() is written against the single event the terminal actually delivers, and returns false - not true - when a drag starts and ends on the same pixel, so a caller reacting to "the level changed" does not treat a no-op as a real change. The second trap is precision. A price read straight off a dragged object is wherever the pixel the mouse released over maps to, not a value on the symbol's tick grid - it can be off by a fraction of a point. Every price this header hands back, from Create() through Price(), Dragged() and SetPrice(), is passed through NormalizeDouble against the symbol's own digit count, so a dragged stop is never rejected by the server for having too many decimal places. SetLocked() clears OBJPROP_SELECTABLE rather than simply ignoring drag events for a locked line. The difference matters: an ignored-but-still-selectable line can still be picked up and visually dragged before the discard happens, which looks like a bug even when the logic is correct. A genuinely unselectable line cannot be picked up at all. The header is self-contained - one class, no dependency on ChartObjects or PanelFramework from this collection. Notes and limitations - Only OBJ_HLINE is wrapped. A trend line has two price/time pairs instead of one and needs a different handler; this one intentionally stays narrow. - Dragged() compares against the last price it itself recorded, whether that came from a real drag or from SetPrice(). Calling SetPrice() and then immediately dragging back to the exact same spot correctly reports no change, which is the intended behaviour, not a bug. - The demo cannot generate a real mouse drag, since a script's OnStart runs once with no live event loop, on either platform. It moves the object with ObjectSetDouble the way a finished drag would leave it, then calls Dragged() directly, which exercises the identical change-detection path a genuine CHARTEVENT_OBJECT_DRAG would use. - No support for constraining the drag to a price range while it is in progress - neither platform delivers a mid-drag event to intervene on; range enforcement has to happen after the fact, in the handler that reacts to Dragged().

Script · MT5 · v1.0 · Free

About Draggable Line Handler

Draggable Line Handler is a script for MetaTrader 5. It has 1 setting, such as Prefix. MQL source code included.

Files in the download: DraggableLine.mq5.mqh, DraggableLine.txt, DraggableLineDemo.ex5, DraggableLineDemo.mq5.

Settings

SettingDefaultWhat it does
PrefixDraggableLineDemo_Object-name prefix, avoids collisions on re-run

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: Grid · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria