Typed Units Safety Layer
Every MQL4 quantity that actually matters for sizing a trade correctly - price, pips, points, lot size, money - is conventionally just a "double" with no distinction between them anywhere in the type system. Passing a pip count where a points value was expected, or adding a distance straight onto a price without converting units first, compiles without complaint and fails only on a live account, often expensively. This library wraps each kind of quantity in its own struct - PriceValue, PipsValue, PointsValue, LotsValue, MoneyValue - and provides named conversion functions between them (PipsToPoints, PointsToPriceDistance, MoneyRiskToLots and so on) instead of implicit conversions. There is no operator overloading between the different struct types anywhere in this header, so a function written to take a PipsValue simply will not accept a PointsValue or a bare double at that argument position without an explicit conversion call first. HONEST LIMITATION, read before relying on this for safety. MQL4 has no real nominal type system. This library cannot, and does not claim to, make the compiler reject reading a bare double back out of one of these structs via its ".value" field, or reject comparing two mismatched values once you have done that. A struct here is genuinely just a double with a name wrapped around it, unlike a language with real unit-of-measure typing (F#'s units of measure, or a Rust newtype with no arithmetic trait implemented). What this library actually buys is discipline made visible: every function that means "pips" takes and returns PipsValue, not double, so a caller doing the wrong thing has to either call the wrong conversion function outright, or reach into ".value" directly - both conspicuous, both easy to catch in a code review, neither invisible the way a bare-double swap is. The demo includes one deliberate misuse example (reading .value off a PipsValue and using it as if it were points) to show exactly what this library cannot stop, and why that line is still the visible red flag a reviewer would catch. Notes and limitations - Not a compile-time unit-safety guarantee - see the honest limitation paragraph above. It is a disciplined convention that makes mistakes visible at the call site and in review, not something the compiler enforces. - MoneyRiskToLots and PointsToMoney both use MODE_TICKVALUE and MODE_TICKSIZE from the current broker feed; both correctly return zero rather than dividing by zero when a symbol reports a degenerate tick size, but a genuinely wrong/stale tick value from the broker feed produces a wrong (not obviously wrong) money or lot figure - this library trusts MarketInfo() the same way any MQL4 code must. - NormalizeLots clamps to MODE_MINLOT/MODE_MAXLOT and rounds to MODE_LOTSTEP with an epsilon guard, but does not re-check margin availability or the account's actual free margin - that is a separate concern outside this library's scope.
Script · MT5 · v1.0 · Free
About Typed Units Safety Layer
Typed Units Safety Layer is a script for MetaTrader 5. It has 2 settings, such as Stop pips and Risk money. MQL source code included.
Files in the download: TypedUnitsSafety.mq5.mqh, TypedUnitsSafety.txt, TypedUnitsSafetyDemo.ex5, TypedUnitsSafetyDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Stop pips | 25.0 | Stop distance in pips |
| Risk money | 100.0 | Money to risk on the trade |
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: Risk & Lot Size · MT5 Script · MQL5 Source Code