Quick Answer
If your code calls PositionSelectByIndex(i), it will not compile, because this function does not exist in MQL5. The compiler reports undeclared identifier, and the parser can add confusing lines such as 'idx' - some operator expected on the lines nearby. A forum thread on exactly this problem shows it: the error pointed at a while loop and the loop counter, and the real cause was the call to the function that is not there.
The correct way to go through open positions is a loop from 0 to PositionsTotal() - 1 that selects each position with PositionGetSymbol(index) (it returns the symbol and selects the position) or PositionGetTicket(index) (it returns the ticket and selects the position). After that, PositionGetInteger, PositionGetDouble and PositionGetString read the selected position. The sections below show the loop, a version that filters by magic number, and a script that prints the open positions.
From the author: I build and convert Expert Advisors. Names like
PositionSelectByIndexlook natural: there isOrderSelect, there isPositionSelect, so one more "select by index" seems logical. Code written from memory, or by a tool that guessed the name, uses it often. The compiler is right: look the function up in the reference first.
Key Facts
| What you wrote | What happens | Use instead |
|---|---|---|
| PositionSelectByIndex(i) | Undeclared identifier | PositionGetSymbol(i) or PositionGetTicket(i) |
| PositionSelect(i) | It takes a symbol, not an index | A symbol name, or the index loop above |
| PositionSelectByTicket(t) | Valid: it takes a ticket | Use it when you already have a ticket |
| 'idx' - some operator expected | A cascade from the first error | Fix the undeclared name first |
| PositionGetSymbol(i) returns an empty string | The index is wrong, or there is no such position | Loop to PositionsTotal() - 1 |
| Positions are skipped while you close them | You loop upward and the list shrinks | Loop downward |
What the Error Looks Like
'PositionSelectByIndex' - undeclared identifier
'idx' - some operator expected
The second line is a side effect. The compiler cannot understand the call, so it also complains about the tokens around it. Fix the first message, compile again, and the second one is gone. The cause of a plain "undeclared identifier" is described in MQL5 undeclared identifier.
The Broken Code
bool FindBuyPosition(const string sym, const long magic)
{
int total = PositionsTotal();
int idx = 0;
while(idx < total)
{
PositionSelectByIndex(idx); // undeclared identifier
if(PositionGetString(POSITION_SYMBOL) == sym &&
PositionGetInteger(POSITION_MAGIC) == magic)
return true;
idx++;
}
return false;
}
The Fix With PositionGetSymbol
According to the MQL5 documentation, PositionGetSymbol(index) returns the symbol of the position and automatically selects the position for the functions that read it. It returns an empty string when it fails. So the call does two jobs at once:
bool FindBuyPosition(const string sym, const long magic)
{
int total = PositionsTotal();
for(int idx = 0; idx < total; idx++)
{
string posSymbol = PositionGetSymbol(idx); // selects the position idx
if(posSymbol == "")
continue; // could not select it
if(posSymbol == sym &&
PositionGetInteger(POSITION_MAGIC) == magic &&
(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
return true;
}
return false;
}
Note the cast: PositionGetInteger returns a long, and the position type is an enumeration. The cast avoids a type warning (see possible loss of data).
The Fix With PositionGetTicket
If you need the ticket of the position (to close or modify it), PositionGetTicket(index) returns the ticket and selects the position in the same way. A ticket of 0 means that it failed:
for(int idx = PositionsTotal() - 1; idx >= 0; idx--)
{
ulong ticket = PositionGetTicket(idx);
if(ticket == 0)
continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol)
continue;
if(PositionGetInteger(POSITION_MAGIC) != 123456)
continue;
// here: read, modify or close this position
}
The loop runs downward. If you close a position inside the loop, the list gets shorter, and an upward loop would skip a position or read an index that is no longer there (see array out of range for the same kind of mistake). The magic number filter is explained in filter positions by magic number.
Which Function Selects What
| Function | Takes | Use it when |
|---|---|---|
| PositionGetSymbol(index) | An index | You loop over all positions and want the symbol |
| PositionGetTicket(index) | An index | You loop and want the ticket |
| PositionSelect(symbol) | A symbol | You want the position of one symbol (netting accounts: one position per symbol) |
| PositionSelectByTicket(ticket) | A ticket | You already have the ticket |
On a hedging account, one symbol can have many positions, so PositionSelect(symbol) is not enough, and the index loop is the right tool (see hedging vs netting).
A Full Script That Prints the Open Positions
This script does not trade. It prints every open position of the account, in the order of the list:
void OnStart()
{
int total = PositionsTotal();
PrintFormat("Open positions: %d", total);
for(int idx = 0; idx < total; idx++)
{
ulong ticket = PositionGetTicket(idx);
if(ticket == 0)
continue;
PrintFormat("#%d ticket=%I64u symbol=%s volume=%.2f magic=%I64d",
idx,
ticket,
PositionGetString(POSITION_SYMBOL),
PositionGetDouble(POSITION_VOLUME),
PositionGetInteger(POSITION_MAGIC));
}
}
On a demo account with no open positions, the output should be Open positions: 0. Open one manual position and run it again: you should see one line with the symbol and the volume of that position.
Why Other Names Fail the Same Way
The same error appears for other names that look right and are not in the language: PositionSelectByIndex, OrderSelectByIndex, PositionGetByIndex. When you see "undeclared identifier" for a function, check the name in the reference, not the code around it. For an MT4 background, OrderSelect(i, SELECT_BY_POS) is the old way, and the MT5 way is the pair above. See wrong parameters count for the other common MT4 to MT5 mistake.
Checklist: What to Check When a Position Function Does Not Compile
- Read the first message, not the cascade after it.
- Look the function name up in the MQL5 reference.
- Use
PositionGetSymbol(index)orPositionGetTicket(index)in an index loop. - Check the return: an empty symbol or a ticket of 0 means that it failed.
- Filter by symbol and magic number before you act.
- Loop downward when you close positions inside the loop.
- Cast the position type and the magic number to the right types.
- Test on a demo account with one manual position.
Common Mistakes
- Inventing a "select by index" function.
- Passing an index to
PositionSelect. - Ignoring the return value of
PositionGetSymbol. - Looping upward while closing positions.
- Reading the position without selecting it first.
- Not filtering by magic number, so that the EA touches positions of other EAs.
FAQ
Related Guides
- MQL5 filter positions by magic number: find your own positions.
- MQL5 undeclared identifier: a name the compiler does not know.
- MQL5 wrong parameters count: MT4 calls that fail in MT5.
- MQL5 array out of range: an index outside the list.
- MQL5 possible loss of data: a type conversion warning.
- MT5 hedging vs netting: why one symbol can have many positions.
- MQL5 trailing stop EA: modify the stop of an open position.
- Pine Script strategy.close to MQL5: close the position that your rule selects.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
- MQL5 variable already defined: a name declared twice in the same scope.
- MQL5 implicit conversion from number to string: the PositionSelect index warning.
Fix Compile Errors Without Guessing
If an Expert Advisor does not compile, paste the code and the error message 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. 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.