Zero Divide Hunt Kit
A division by zero on a double in MQL4 is a critical runtime error that terminates the running program immediately - there is no try/catch to recover inside the program itself once it happens. That means the only workable defence is checking the denominator before dividing, never dividing first and reacting afterward, and this script is built around that constraint throughout: it never performs a genuinely unguarded division anywhere in its own code, only a guarded one it can prove works using a synthetic zero it controls itself. It checks four classic sources of a zero denominator that show up repeatedly across trading tools: Point (used converting a price distance into pips), MODE_TICKSIZE (used converting a price distance into ticks for a tick-value calculation), MODE_LOTSTEP (used normalising a requested lot size to the broker's lot grid), and the number of bars currently loaded for the chart's symbol and timeframe (used in any per-bar average computed before history has finished synchronising - a real risk for a freshly attached multi-symbol tool). For each one it reports the broker's live value and states plainly whether it is currently safe to divide by. It then runs each of those four denominators through one shared SafeDivide() guard against a realistic example calculation, so a reader can see the guard in normal operation, not just in the abstract. Finally it proves the guard itself actually triggers correctly, by calling it with a deliberate zero the script constructs on the spot rather than sourced from the broker - this is the one place a zero denominator appears in the source, and it never reaches an actual division, only the guard's own check. Notes and limitations - This checks only the four denominators named above. Other classic zero-divide risks exist (MODE_STOPLEVEL-based spacing checks, tick value itself, account-currency conversion rates) and are not covered here - the scope is deliberately the four named in the brief, not an exhaustive audit. - A symbol reported "safe" at the moment the script runs can still become zero later (e.g. a symbol delisted mid-session, or history that has not finished loading yet on first attach) - this is a point-in-time check, not a permanent guarantee, and any real tool should still guard every division itself rather than relying on having run this once. - The guard's "effectively zero" threshold is a small fixed epsilon (1e-10), not exactly 0.0, since floating-point broker values are rarely exactly zero even when practically unusable.
Script · MT5 · v1.0 · Free
About Zero Divide Hunt Kit
Zero Divide Hunt Kit is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 1 setting, such as Symbol. MQL source code included.
Files in the download: ZeroDivideHuntKit.ex5, ZeroDivideHuntKit.mq5, ZeroDivideHuntKit.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol | Symbol to check (blank = current chart symbol) |
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: Alerts · MT5 Script · MQL5 Source Code