“'idx' – some operator expected” and “undeclared identifier” on while-loop counter inside function, but minimal test compiles fine

 

“'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

  • Platform: MetaTrader 5
  • OS: [Windows 10/11 x64]
  • File encoding: [UTF-8 (no BOM) or ANSI — tried both]
  • #property strict: enabled

Exact Errors

  • 'idx' - some operator expected    
  • undeclared identifier             

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):


bool FindBuyPosition(const string sym, long magic, ulong &ticket, double &open_price, double &volume, double &tp, double &sl)
{
   int total = PositionsTotal();
   int idx   = 0;

   while(idx < total)
   {
      if(PositionSelectByIndex(idx)) <------------ Both error occur on the line 
      {
         if(PositionGetString(POSITION_SYMBOL) == sym &&
            (long)PositionGetInteger(POSITION_MAGIC) == magic &&
            (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            ticket     = (ulong)PositionGetInteger(POSITION_TICKET);
            open_price = PositionGetDouble(POSITION_PRICE_OPEN);
            volume     = PositionGetDouble(POSITION_VOLUME);
            tp         = PositionGetDouble(POSITION_TP);
            sl         = PositionGetDouble(POSITION_SL);
            return true;
         }
      }

      idx = idx + 1;
   }

   return false;
}


Notes

  • The error specifically lands on the while(idx < total) line (column ~32), saying "'idx' - some operator expected", followed by another “undeclared identifier” error later in the file (line ~81).
  • The same loop-counter pattern compiles in isolation (see “Minimal Test” below), so I suspect a scope/syntax leak above the function in the full file, but I can’t spot it.

Minimal Test That Compiles (same machine)

void OnTick()
{
   int test_var = 0;
   while(test_var < 5)
   {
      test_var++;
   }
}

What I’ve Already Tried

  • Declared counters well before loops; no inline declarations in control statements.
  • Renamed counters (idx → i/counter/pos_counter; collect_counter in other functions → j/k).
  • Inserted a trivial “probe” loop immediately before the failing while:
    • int probe = 0; while(probe < 1) { probe = probe + 1; }
  • Verified line immediately above while ends with a semicolon.
  • Checked for unbalanced braces { }, parentheses ( ), and missing semicolons using MetaEditor brace matching.
  • Searched for unclosed strings "…" and unclosed block comments /* … */.
  • Retyped the lines (and the few lines above them) directly in MetaEditor to strip potential hidden characters.
  • Saved the file as UTF-8 (no BOM) and as ANSI, reopened, and recompiled.
  • Confirmed #property strict is present.
  • Built a separate .mq5 with only this function and a tiny OnTick harness — compiled cleanly.
  • Restarted MetaEditor and machine for good measure.

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

  • Are there known cases where a prior subtle syntax issue (or invisible character/BOM) causes the parser to treat later while statements as being out of scope and trigger these exact errors?
  • Any recommended tricks to pinpoint the first offending location? For example:
    • Temporarily inserting return(false); } just before the failing while to see if the error jumps earlier, then locating the missing } above.
    • Declaring idx as a temporary global at the very top to see if errors change (to confirm scope leakage).
    • Copying only the code from the start of the file up to this function into a new file to see where the break occurs.
  • Anything specific to MQL5 preprocessing or includes that could cause this in an EA file?

Thanks in advance for any pointers — especially on techniques to surface a hidden missing brace/semicolon or a scope leak I’m not seeing.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Execution of trade operations results in the opening of a position, changing of its volume and/or direction, or its disappearance. Trade operations...
 

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
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...