“'idx' – some operator expected” and “undeclared identifier” on while-loop counter inside function, but minimal test compiles fine
The answer is simple—there is no such built-in function for MQL5 called "PositionSelectByIndex", hence the error "undeclared identifier".
The reason ... you have been relying on A.I. for code and answers. Don't! Learn to research and think for yourself.
Instead, read the documentation and use the following function to select a position by index ... Documentation on MQL5: Trade Functions / PositionGetSymbol

Documentation on MQL5: Trade Functions / PositionGetSymbol
- www.mql5.com
Returns the symbol corresponding to the open position and automatically selects the position for further working with it using functions...

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
“'idx' – some operator expected” and “undeclared identifier” on while-loop counter inside function, but minimal test compiles fine
Body
Hi all, I’m stuck on two persistent compile errors in an EA. The errors flag a simple while-loop counter variable inside an otherwise straightforward function. What’s odd is that a minimal test file with the same while pattern compiles fine on the same setup, so the toolchain appears OK.
Environment
Exact Errors
Problem Code (excerpt)
The first error points to the while line where idx is used. Here’s the exact function pattern I’m using (simplified to buys; a similar sell function shows the same issue later in the file):
Notes
Minimal Test That Compiles (same machine)
What I’ve Already Tried
Hypothesis
Based on the error placement and the fact that isolation works, it looks like the compiler is parsing the while as if it’s not inside a valid function scope (hence “some operator expected” and variable appears “undeclared”). That would usually mean there’s a malformed construct earlier in the file (missing } or ;, or an unclosed string/comment), or possibly a stray/invisible character on a previous line. I’ve checked for these, but I may be missing something subtle.
Questions for the community
Thanks in advance for any pointers — especially on techniques to surface a hidden missing brace/semicolon or a scope leak I’m not seeing.