Chart Event Router
Every OnChartEvent handler ends up repeating the same three steps for each event it cares about: check the id, check whether sparam belongs to this panel, and cast lparam/dparam into the coordinates or key code they actually represent. Written by hand at every call site, that is where the copy-paste drift creeps in - one branch checks the prefix with StringFind and gets the comparison backwards, another forgets the cast and compares a long against an int silently. This header is not a callback dispatch table on either platform - neither MQL4 nor MQL5 offers function-pointer support consistent enough across the full range of deployed builds this catalogue targets for a registration-based router to be reliable everywhere. Instead it is a set of matcher functions called directly inside your own OnChartEvent, each one doing the id check, the prefix check and the parameter decode in one place, tested once here rather than trusted at every call site. EvIsDuplicate() covers a genuine chart-event quirk present on both platforms: some interaction sequences raise two chart events for what a user experiences as one action - finishing an edit box can raise CHARTEVENT_OBJECT_ENDEDIT and then a CHARTEVENT_CLICK on the same object in the same instant. Debouncing by (id, sparam) within a short window is far more reliable than trying to special-case every object type this happens with. Notes and limitations - A script has no OnChartEvent of its own on either platform, so the demo feeds the matchers synthetic (id, lparam, dparam, sparam) values built the same way the terminal builds them for a real interaction. This proves the decoding logic without needing a live chart click; wiring it into a real OnChartEvent is one line per matcher, as shown above. - EvIsDuplicate() and the mouse-move/key-down matchers keep state in module- level variables, so they track the single most recent event only - correct for a single chart's OnChartEvent, not meant to be shared across multiple independent charts running the same compiled program. - CHART_EVENT_MOUSE_MOVE, CHART_EVENT_OBJECT_CREATE and CHART_EVENT_OBJECT_ DELETE are off by default on every chart on both platforms; nothing arrives at OnChartEvent for them until the matching Ev...Enable...() call is made once, typically from OnInit. - No money-per-point or contract-size calculation appears anywhere in this header on either platform - it decodes chart UI events only, so the tick- value/contract-size scaling this catalogue's house style calls out for money calculations does not apply here.
Script · MT5 · v1.0 · Free
About Chart Event Router
Chart Event Router is a script for MetaTrader 5. It has 1 setting, such as Panel prefix. MQL source code included.
Files in the download: EventRouter.mq5.mqh, EventRouter.txt, EventRouterDemo.ex5, EventRouterDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Panel prefix | DemoPanel_ | Object-name prefix this router should accept |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code