Loading…

Interval Arithmetic

The usual way to carry uncertainty through a calculation is a fixed "+/- x%" margin tacked onto the final answer - which silently ignores how errors actually combine across a chain of operations: cancellation can shrink the true uncertainty, and multiplication or division can grow it non-linearly. Interval arithmetic instead attaches a [lo, hi] bound to every value from the start and defines every operation (+, -, *, /, integer powers, sqrt) so the RESULT'S bound is mathematically guaranteed to contain the true answer, whatever the true point values inside each input's bound turn out to be. The two traps that make a naive implementation wrong are handled explicitly: multiplication takes the min/max of all four corner products rather than assuming a fixed sign, since an interval that has already passed through a subtraction commonly straddles zero; and squaring (or any even power) of an interval that straddles zero correctly reports a minimum of exactly 0.0 - not the smaller endpoint's square, which would understate how tight the true minimum actually is. Division by an interval that itself straddles zero has no single-interval answer at all (the true result is two disjoint unbounded rays) and is flagged as invalid rather than silently returned as some finite-looking but meaningless bound. Useful anywhere an input genuinely carries uncertainty rather than being a clean known number - a quoted spread with a variable component, an expected- move estimate from a backtest's own confidence range, a slippage assumption - and the question that matters is the worst case the calculation can produce, not just its point estimate. Notes and limitations Only addition, subtraction, multiplication, division, non-negative integer powers and square root are implemented - no trigonometric, exponential or logarithmic interval functions. Adding one for a specific need is straightforward (map the function's monotonicity over the bound the same way IaSqrt does) but is left to the caller rather than guessed at speculatively. Interval bounds widen monotonically as more operations chain together (this is inherent to interval arithmetic, not a bug in this implementation) - a long calculation chain with the same variable appearing more than once ("dependency problem") produces a wider, more pessimistic bound than the true achievable range, since the method has no way to know two occurrences of the same interval are perfectly correlated rather than independent. Rearranging a formula to use each uncertain input only once keeps the bound tighter. IaDiv and IaSqrt return a success flag rather than raising a runtime error on an invalid domain (division by a zero-straddling interval, square root of a negative-only interval) - a caller that ignores the flag silently gets a placeholder result ([-DBL_MAX, DBL_MAX] or [0,0] respectively), not a crash.

Script · MT5 · v1.0 · Free

About Interval Arithmetic

Interval Arithmetic is a script for MetaTrader 5. MQL source code included.

Files in the download: IntervalArithmetic.mq5.mqh, IntervalArithmetic.txt, IntervalArithmeticDemo.ex5, IntervalArithmeticDemo.mq5.

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

MQL4 & MQL5 scripts · PolyMaria