Loading…

Price String Rounding Demo

A double cannot represent most decimal fractions exactly - a price that looks like a clean value in decimal, such as 1.10005 or 2.675, is actually stored as something a tiny fraction above or below that value in binary. Which way that tiny fraction falls decides which way the displayed digit rounds once the digit count is reduced, and it is not always the direction a trader would expect from the decimal value alone. This is invisible until a displayed price is one point off from what was actually stored, or two different formatting calls on the same value disagree. This script runs a small set of known-tricky prices, plus one of your own choosing, through three independent formatting paths at every digit count from InpDigitsMin to InpDigitsMax: DoubleToString directly, NormalizeDouble followed by DoubleToString, and a manually built "%.<n>f" format string. All three usually agree, but printing them side by side is what actually shows the rare cases where they do not, rather than leaving it as a warning to take on faith. It also prints the same prices formatted at the digit count MarketInfo actually reports for a real symbol, read live rather than assumed, since a hardcoded digit count breaks the moment the same code runs against a 3-digit JPY cross instead of a 5-digit major. Notes and limitations - This is a read-only, display-only demonstration. It does not touch any order, position or account value, and the tricky prices are fixed test constants, not live market prices. - "NO - differs" rows are the expected, useful output of this tool, not a bug - they show exactly where DoubleToString, a normalize-then- format path, and manual %f formatting round the same stored double differently. - The built-in tricky-price list is a small, fixed set chosen for known binary-representation edge cases, not an exhaustive scan of every possible price.

Script · MT4 · v1.0 · Free

About Price String Rounding Demo

Price String Rounding Demo is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Symbol, Custom price and Digits min. MQL source code included.

Files in the download: PriceStringRoundingDemo.mq4, PriceStringRoundingDemo.txt.

Settings

SettingDefaultWhat it does
SymbolSymbol whose real digit count is read for the "broker digits" row (empty = current chart symbol)
Custom price1.234565An extra price of your own to run through the same sweep
Digits min1Lowest digit count in the sweep
Digits max6Highest digit count in the sweep

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 · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria