Quick Answer
The MetaEditor warning "implicit conversion from 'number' to 'string'" means that you passed a number to a place where the function wants text. In forum threads, the most common cause is a position loop that calls PositionSelect(i). PositionSelect() expects a symbol name (a string), not a position index, so the compiler turns the integer i into a string and warns. The code still compiles, but it does not do what you meant.
The fix is to select positions by index with PositionGetTicket(i), which returns the ticket of the position at that index and selects it for the PositionGet... functions. Keep PositionSelect(symbol) only for the case where you want the position of one symbol. For other numbers that really should become text, convert them yourself with IntegerToString() or DoubleToString(). This post shows the broken loop, the fixed loop, a script that prints a result you can check, and the related pitfalls.
From the author: I convert MT4 and Pine Script code to MQL5. The MT4 habit of selecting orders by index (
OrderSelect(i, ...)) is the most common source of this warning. In MQL5 the same code compiles and then selects nothing useful. A warning that you ignore here is a logic bug that has not shown up yet.
Key Facts
| What you wrote | What happens | Fix |
|---|---|---|
| PositionSelect(i) in a for loop | The integer is converted to a string and used as a symbol name | PositionGetTicket(i) |
| PositionSelect(_Symbol) | Correct: one symbol | Nothing to change |
| A number passed to a string parameter | Same warning | IntegerToString(n) or DoubleToString(x, digits) |
| A number assigned to a string variable | Same warning | Convert with the functions above |
| Warning ignored | The code compiles, but the selection can fail | Treat the warning as a bug |
What the Warning Looks Like
In MetaEditor, the Errors tab shows a line such as implicit conversion from 'number' to 'string' with a file name, a line and a column. In two forum threads the compiler pointed at a line with PositionSelect(...). In one of them the poster had an EA converted from MQL4 and the warning appeared on two lines. A reply in that thread gave the cause: PositionSelect() takes a symbol name, and the code had a logic flaw.
The Broken Loop
double TotalVolume()
{
double total = 0.0;
for(int i = 0; i < PositionsTotal(); i++)
{
if(PositionSelect(i)) // warning: implicit conversion from 'number' to 'string'
total += PositionGetDouble(POSITION_VOLUME);
}
return total;
}
Here i is 0, 1, 2 and so on. The function turns it into the text "0", "1", "2", and looks for a position on a symbol with that name. There is no such symbol, so the call fails, and the function returns 0 while positions are open.
The Fixed Loop
A reply in a forum thread on the same warning suggested selecting by index with PositionGetTicket. A second thread said that the ticket function returns the ticket and selects the position. Use that, and read the properties after it:
double TotalVolume()
{
double total = 0.0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0)
continue; // the position is gone, skip it
total += PositionGetDouble(POSITION_VOLUME);
}
return total;
}
The loop runs from the end to the start. Other posts on this error family explain why: if a position is closed while you loop, the indexes you have not visited yet do not move (see MQL5 array out of range).
When PositionSelect Is the Right Call
PositionSelect(symbol) is meant for the position of one symbol, mostly on netting accounts, where there is one position per symbol:
if(PositionSelect(_Symbol))
{
double volume = PositionGetDouble(POSITION_VOLUME);
Print(_Symbol, " volume: ", volume);
}
On a hedging account there can be many positions on one symbol, and the index loop with PositionGetTicket is the way to see all of them. To find only your own positions, filter by magic number (see filter positions by magic number).
A Related Error: PositionSelectByIndex
Some code that was converted from another language calls a function named PositionSelectByIndex. It is not an MQL5 function and gives a different message, an undeclared identifier. See PositionSelectByIndex undeclared identifier. The fix is the same idea: PositionGetTicket(i).
Other Numbers That Become Text
The warning is not only about positions. Any call that expects a string and gets a number can produce it. When you want text on purpose, convert it:
int count = 3;
double price = 1.10520;
string a = IntegerToString(count); // "3"
string b = DoubleToString(price, 5); // "1.10520"
Print("count=", a, " price=", b);
Print accepts numbers directly and does the conversion for you. The warning is for places where a function or variable needs a string, for example a symbol name, a comment text or a global variable name.
A Script That Prints a Result You Can Check
This script does not trade. It prints the number of open positions and the ticket and symbol of each one, with the safe loop. On an account with no positions it prints only the count:
#property strict
void OnStart()
{
int total = PositionsTotal();
PrintFormat("Open positions: %d", total);
for(int i = total - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0)
continue;
string symbol = PositionGetString(POSITION_SYMBOL);
PrintFormat("index %d ticket %I64u symbol %s", i, ticket, symbol);
}
}
Open one position on a demo account and run the script: it should print Open positions: 1 and one line with that position's ticket and symbol. With two positions you get two lines.
The Type Warnings Are a Family
| Warning or error | What it means | Where to look | |---|---|---| | Implicit conversion from 'number' to 'string' | A number is used as text | This post | | Possible loss of data due to type conversion | A larger type is stored in a smaller one | Possible loss of data | | Undeclared identifier | A name the compiler does not know | Undeclared identifier | | Wrong parameters count | A function call has too few or too many arguments | Wrong parameters count | | System function is not allowed to call (4014) | A function is used in a program type that cannot call it | Error 4014 |
Checklist: What to Check When You See This Warning
- Read the line the compiler points to.
- Look for
PositionSelect(with a loop index. - Replace it with
PositionGetTicket(i)and checkticket == 0. - Loop from the end if positions can close during the loop.
- Use
PositionSelect(symbol)only for one symbol. - Convert numbers with
IntegerToStringorDoubleToStringwhere text is needed. - Compile again and look for new warnings.
- Test on a demo account with one and with two open positions.
Common Mistakes
- Ignoring the warning because the EA compiles.
- Using the MT4 index pattern in MQL5.
- Passing an index to a function that wants a symbol.
- Forgetting to check the ticket for
0. - Looping upward while positions close.
- Building text from numbers by hand instead of
DoubleToString.
FAQ
Related Guides
- MQL5 possible loss of data due to type conversion: the other common type warning.
- MQL5 PositionSelectByIndex undeclared identifier: the position by index function that does not exist.
- MQL5 array out of range: why position loops should run from the end.
- Filter positions by magic number in MQL5: find only your own positions.
- MQL5 undeclared identifier: a name the compiler does not know.
- MQL5 wrong parameters count: a call with the wrong number of arguments.
- MQL5 error 4014: system function is not allowed to call: a function used in the wrong program type.
- MQL5 trailing stop EA: a position loop in a full EA.
- What to check in a trading bot before going live: a pre-live checklist.
Fix Compile Errors Without Guessing
If an Expert Advisor does not compile, or you want a second look at its position loop, paste the code into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. The Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.