Implicit conversion from 'double' to 'string' in MQL5

Short answer

Warning 94 means a number was joined to text or assigned to a string without a format. Convert explicitly with DoubleToString, IntegerToString or StringFormat, which is what Pine's str.tostring and str.format did for you.

MetaEditor prints implicit conversion from 'double' to 'string' (and the same with 'int') as warning 94 when a number is joined to text with + or assigned to a string variable. The program compiles and runs, but the number is printed in a default format you did not choose: a price can lose its trailing zero and an RSI can print fifteen decimals. The fix is to convert explicitly with DoubleToString, IntegerToString or StringFormat, which is what Pine's str.tostring and str.format were doing for you.

Why it happens when converting Pine Script

Pine is strict about this. "RSI " + rsi does not compile on TradingView; you have to write "RSI " + str.tostring(rsi, "#.##"). So a Pine script that works always has an explicit conversion with an explicit format at every place a number meets a string: in label.new text, alert() messages, table.cell contents and the indicator's title.

The warning shows up when that conversion is dropped during a hand port. str.tostring has no MQL5 function of the same name, so it is tempting to delete it and let + do the work, because MQL5 does accept "RSI " + rsi. It only warns. And the format argument, the part that mattered, is lost with it. Three lines from a ported indicator:

   string s = "Close: " + close[rates_total - 1];   // Pine: "Close: " + str.tostring(close, format.mintick)
   string t = rates_total;                           // Pine: str.tostring(bar_index + 1)
   Print("bars " + rates_total);                     // Pine: log.info("bars " + str.tostring(...))

MetaEditor 5 (build 6140) reports, one warning per line:

implicit conversion from 'double' to 'string'
implicit conversion from 'int' to 'string'
implicit conversion from 'int' to 'string'

What the implicit conversion prints is worth seeing. A test indicator that builds the same strings both ways printed this in the Experts journal of MetaTrader 5 on a five-digit symbol:

implicit:   r=33.33333333333333 px=1.1129 n=600 e=1.7976931348623157e+308
explicit:   r=33.33 px=1.11290
StringFormat("%s closed at %.5f, rsi %.2f, bars %d", ...)  ->  ... closed at 1.11290, rsi 33.33, bars 600

Three things went wrong with the implicit version: the RSI printed all its digits, the price 1.11290 printed as 1.1129 (the shortest form, not the symbol's five digits), and a value that Pine would have shown as NaN printed as 1.7976931348623157e+308, because MQL5's stand-in for na, EMPTY_VALUE, is just the largest double.

Fix

Map each Pine conversion to its MQL5 counterpart and keep the format:

Pine ScriptMQL5
str.tostring(x) with an intIntegerToString(x)
str.tostring(x, "#.##")DoubleToString(x, 2) (count the digits after the point)
str.tostring(x, format.mintick)DoubleToString(x, _Digits)
str.tostring(x, format.percent)DoubleToString(x * 100.0, 2) + "%"
str.tostring(b) with a bool(b ? "true" : "false")
str.format("{0} at {1}", a, b)StringFormat("%s at %.5f", a, b)
str.tostring(time) style datesTimeToString(t, TIME_DATE | TIME_MINUTES)

A complete indicator using all three forms: the short name with the length, a chart comment with the last RSI value and bar count, and a formatted journal line. It compiles in MetaEditor 5 with no warnings:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_label1  "RSI"

input int Length = 14;   // Pine: len = input.int(14)

double Rsi[];
int    rsiHandle = INVALID_HANDLE;

int OnInit()
{
   SetIndexBuffer(0, Rsi, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   rsiHandle = iRSI(_Symbol, _Period, Length, PRICE_CLOSE);
   if(rsiHandle == INVALID_HANDLE)
      return INIT_FAILED;
   // Pine: indicator("RSI " + str.tostring(len))
   IndicatorSetString(INDICATOR_SHORTNAME, "RSI " + IntegerToString(Length));
   return INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
                const double &open[], const double &high[], const double &low[], const double &close[],
                const long &tick_volume[], const long &volume[], const int &spread[])
{
   int toCopy = prev_calculated > 0 ? rates_total - prev_calculated + 1 : rates_total;
   if(CopyBuffer(rsiHandle, 0, 0, toCopy, Rsi) <= 0)
      return 0;

   int last = rates_total - 1;
   // Pine: label text = "RSI: " + str.tostring(rsi, "#.##") + " bars: " + str.tostring(bar_index)
   string text = "RSI: " + DoubleToString(Rsi[last], 2) + "  bars: " + IntegerToString(rates_total);
   Comment(text);
   // Pine: str.format("{0} closed at {1}", syminfo.ticker, close)
   if(prev_calculated == 0)
      Print(StringFormat("%s closed at %s", _Symbol, DoubleToString(close[last], _Digits)));
   return rates_total;
}

StringFormat uses printf-style specifiers, not Pine's numbered placeholders: %s for strings, %d for int and long, %.2f for a double with two decimals, %d also for a long. The order of arguments follows the order of specifiers; Pine's {1} before {0} has to be reordered by hand. Wherever a value can be EMPTY_VALUE, test it before formatting and print "n/a" yourself.

Other causes of the same message

  • Assigning a number to a string directly. string t = rates_total; warns from 'int' to 'string'. Same fix.
  • Print and Comment with +. Print("bars " + n) warns because the + builds a string first. Print("bars ", n) with a comma does not warn: Print, Comment and Alert take a list of arguments and convert each one themselves. Use DoubleToString for any argument whose digits matter.
  • datetime in text. Joining a datetime to a string warns from 'datetime' to 'string' and prints the date in the terminal's format. Use TimeToString and say which parts you want.
  • color in text. Same pattern; ColorToString is the explicit form.
  • Converted code. The Stratlate converter emits an explicit conversion for every str.tostring: IntegerToString for ints, DoubleToString(x, n) with n counted from a "#.##" or "0.00" pattern, DoubleToString(x, _Digits) for format.mintick, a percent form for format.percent, and "true"/"false" for bools, so the warning does not appear in generated files. Two differences to check by eye: a float converted without a format argument is emitted as DoubleToString(x, 2), which is two decimals where TradingView shows more; and str.format is currently emitted as StringFormat with Pine's {0} pattern left as it was, which MQL5 prints literally (the test above printed {0} at {1}). Rewrite that pattern with %s/%.nf specifiers after converting.

Pitfalls

  • Using (string)x to silence the warning. It does silence it, and it prints exactly the same default form as the implicit conversion. The warning was about the format, not the syntax.
  • DoubleToString(price, 4) on a five-digit symbol. Hard-coded digit counts round prices on another symbol. Use _Digits for prices and a fixed count only for oscillators and ratios.
  • Passing an int to %f or a double to %d in StringFormat. MQL5 does not convert the argument to match the specifier; the output is garbage. Match the specifier to the type or cast the argument.
  • Printing EMPTY_VALUE in alert text. An alert that says stop at 1.7976931348623157e+308 is what an unguarded na looks like. Guard it the way Pine's na() check would.
  • Building long strings on every tick. String building in OnCalculate for every bar of history is slow on large charts. Build label and comment text only for the last bar, which is what Pine's barstate.islast guard was doing.

FAQ

Is implicit conversion from double to string an error?

No, it is a warning and the program runs. The number is printed in MQL5's default short form, which drops trailing zeros and keeps all significant digits, so 1.11290 prints as 1.1129 and an RSI of one third of 100 prints as 33.33333333333333. Convert explicitly to control the output.

What is the MQL5 equivalent of str.tostring?

IntegerToString for ints, DoubleToString(value, digits) for floats, with digits taken from the Pine format string or _Digits for format.mintick, and TimeToString for times. There is no single function that accepts Pine's "#.##" patterns; count the decimals and pass the number.

Why does StringFormat print {0} instead of my value?

StringFormat uses printf specifiers such as %s, %d and %.2f. Pine's numbered placeholders mean nothing to it, so the text is printed unchanged and the arguments are ignored. Replace each {n} with the specifier for that argument's type, in argument order.

Let the converter do the mechanical part

Paste the original script and get a conversion that compiles, with every construct that could not be carried over reported instead of hidden. Review, compile and backtest the result before trading.

Open the converter

Related