Loading…

Fixed Point Money Arithmetic

double cannot represent most decimal fractions exactly, because it stores binary fractions, not decimal ones. 0.1 in a double is actually 0.1000000000000000055511151231257827021181583404541015625. A single addition never shows it. Ten thousand additions of 0.1 to a running total measurably do not equal 1000.0 - the demo prints the exact error alongside the exact result so the drift is not just asserted, it is shown. This matters for anything that accumulates over a long run rather than computing once: realised profit tallied across months of closed trades, a running equity total updated on every tick, a fixed-ratio or optimal-f tracker that recomputes its base from cumulative results. Each individual addition's error is tiny. The point is that they do not cancel out - they compound, in the same direction, for as long as the program runs. CDecimal fixes this by never storing the value as a double at all. Internally it is a 64-bit integer scaled by one hundred million - eight decimal places, comfortably more precision than any broker's price or lot step - so addition and subtraction are exact integer operations with no rounding step whatsoever. Two CDecimal values added a million times produce a result with zero accumulated error, not just a smaller one. Multiplication and division are the honest exception. Scaling a decimal by an arbitrary factor - lots, a percentage, anything that is not itself a fixed- point value - genuinely requires a floating point step, because the factor is not fixed-point. MultiplyBy and DivideBy do this openly, in one place, for one call. That is a fundamentally different situation from thousands of additions each losing a fraction of a bit: one rounding step does not compound the way a running total does. Division by zero has no correct answer, and neither MQL4 nor MQL5 has exceptions to signal one with. Rather than return an infinity or a silently wrapped garbage integer, DivideBy returns the original value unchanged when the divisor is effectively zero - a visible, debuggable no-op instead of a value that quietly stopped meaning anything. The class body is pure integer and double arithmetic - MathRound, MathAbs and DoubleToString all behave identically on both platforms, so the MQL5 header is the same class, unchanged, with only the platform-neutral #property lines adjusted. Notes and limitations - Eight decimal places of precision. That comfortably covers five-digit forex quotes and fractional lot sizes, but this is not an arbitrary-precision decimal type - do not reach for it for anything needing more than eight places after the point. - The raw scaled value is a 64-bit long, giving headroom for any realistic account balance or cumulative total, but it is not unbounded - extremely large intermediate values could in principle overflow, the same as any fixed-size integer type. - MultiplyBy and DivideBy use a double internally by necessity, since the factor is not itself fixed-point. That single step does not compound the way repeated addition would, but it is not magic either - do not chain many MultiplyBy calls expecting the same exactness addition gives you. - DivideBy(0) is a silent no-op returning the unchanged value, not an error. Callers that need to detect a zero divisor should check for it themselves before calling.

Script · MT4 · v1.0 · Free

About Fixed Point Money Arithmetic

Fixed Point Money Arithmetic is a script for MetaTrader 4. It has 1 setting, such as Addition count. MQL source code included.

Files in the download: DecimalArithmetic.mqh, DecimalArithmetic.txt, DecimalArithmeticDemo.mq4.

Settings

SettingDefaultWhat it does
Addition count100000Number of times 0.1 is added, in both representations

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Binary Options · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria