Complex Number Library
Neither MQL4 nor MQL5 has a built-in complex type, and structs on both platforms cannot have operator overloads the way a class can - there is no way to write z1 + z2 for two Complex values without wrapping the value type in a class purely to fake the syntax, which adds allocation overhead for no real benefit here. Every function in this library therefore takes and returns a plain Complex struct (a `re` and an `im` double) by value: ComplexAdd(a, b) instead of a + b. That is the honest shape of what both languages allow, not a workaround bolted on afterwards, and it is why the MQL5 port keeps the exact same function-call shape rather than reaching for MQL5 class syntax. Beyond the four basic operations, the library covers what most callers actually reach for once they get past addition and multiplication: polar conversion (ComplexFromPolar / ComplexAbs / ComplexArg) for building roots of unity, ComplexExp for Euler's formula, ComplexSqrt and ComplexPow for the principal root and integer powers, and a guarded ComplexDiv that prints a warning and returns 0+0i on a zero or near-zero divisor instead of silently producing inf or NaN the way raw division would - neither platform raises a runtime error on division by zero for doubles, it just poisons everything downstream with a value that looks like a number but is not one. This is a foundation library, not an application on its own. It exists so FastFourierTransform.mqh, filter-design code, or any eigenvalue or polynomial-root routine has a single, self-contained complex type to build on, without any of them re-deriving the same arithmetic slightly differently. Notes and limitations - No operator overloading is possible for structs on either platform - every operation is a named function, by design, not an oversight. - ComplexPow only supports integer exponents; a fractional complex power (a general z^w) is out of scope for this library. - ComplexDiv and ComplexSqrt both guard the zero-input case by printing a warning and returning 0+0i rather than producing inf/NaN - check the Experts log if a downstream result looks unexpectedly like exactly zero. - This library is pure arithmetic - it has no dependency on trade context, symbols or account state, and can be used in an Expert, Script, Indicator or another Library without any adaptation.
Script · MT5 · v1.0 · Free
About Complex Number Library
Complex Number Library is a script for MetaTrader 5. It has 4 settings, such as Real a, Imag a and Real b. MQL source code included.
Files in the download: ComplexNumbers.mq5.mqh, ComplexNumbers.txt, ComplexNumbersDemo.ex5, ComplexNumbersDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Real a | 3.0 | Real part of the first demo value |
| Imag a | 4.0 | Imaginary part of the first demo value |
| Real b | 1.0 | Real part of the second demo value |
| Imag b | -2.0 | Imaginary part of the second demo value |
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