Edgoro
Compiler warning 94 · MQL5

MQL5 warning: implicit conversion from number to string

What MetaEditor's "implicit conversion from 'number' to 'string'" warning means, and how to format prices with DoubleToString() and StringFormat() in MQL5.

MetaEditor · Errors tab · warning 94 · line 5
implicit conversion from 'double' to 'string'

What it means

A number is being turned into text without you saying how, usually because it's joined to a string with +. The program still compiles: it's a warning. Depending on the MetaEditor build, the message names the type, as in implicit conversion from 'double' to 'string' (build 6275, warning 94), or says 'number'.

Usual causes

  • Concatenation. "Bid: " + bid or Comment("Spread " + spread): the + with a string turns the number into text.
  • Passing a number where a string parameter is expected, such as an object's text.
  • MQL4 code. MQL4 accepted these conversions silently in many cases; MQL5 checks types more strictly and warns.

The warning matters because you don't choose the format: how many decimals a price gets, or how a bool is shown.

How to detect it

  • MetaEditor's Errors tab lists it as a warning, with the line and column of the conversion. Warnings don't stop the build, so they're easy to miss in the build log.

Broken code

This script compiles with three warnings: implicit conversion from 'double' to 'string' and from 'int' to 'string' on line 5, and from 'int' to 'string' on line 6.

MQL5
void OnStart()
  {
   double bid    = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   int    spread = (int)SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
   string msg    = "Bid: " + bid + ", spread: " + spread;
   Comment("Spread " + spread);
   Print(msg);
  }

Fixed code

The same program, corrected. It compiles in MetaEditor with 0 errors and 0 warnings.

MQL5
void OnStart()
  {
   double bid    = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   int    spread = (int)SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
   // The price with the symbol's own number of decimals
   string msg    = "Bid: " + DoubleToString(bid, _Digits) + ", spread: " + IntegerToString(spread);
   Comment(StringFormat("Spread %d points", spread));
   Print(msg);
  }

Why the fix works

Each number is converted explicitly: DoubleToString(bid, _Digits) prints the price with exactly the symbol's number of decimals, IntegerToString() converts the integer, and StringFormat() (like PrintFormat()) builds the whole text from a format string. The compiler has nothing left to guess, so the warnings go away, and the output looks the way you decided.

Questions

Can I ignore implicit conversion warnings in MQL5?

The program runs, so it's not urgent. But you don't control the format of the number, and a long list of warnings hides the ones that matter. Converting explicitly takes one function call per number.

What's the difference between (string)price and DoubleToString(price, _Digits)?

Both convert explicitly and silence the warning. DoubleToString lets you choose the number of decimals, which is what you want for prices; (string) uses a default format.

Why does MQL4 code show this warning in MetaEditor 5?

MQL5 checks types more strictly than MQL4, which converted numbers to text silently in many places. Converted MQL4 code is full of "text" + number, hence the warnings.

The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Rather not write the error handling yourself?

Edgoro's MT5 EA generator writes complete MQL5 Expert Advisors from plain English and compiles each one in MetaEditor before you get it. A clean build proves the code compiles, not that a strategy works: test it on a demo account.