MQL5 For-Loop: Compiler Says ‘i’ Undeclared — What’s Wrong?

 
Hi everyone, I need help. MetaEditor keeps showing this error on several lines:
undeclared identifier 486 9
'i' - some operator expected 486 31
undeclared identifier 118 9
'i' - some operator expected 118 31
undeclared identifier 209 9
'i' - some operator expected 209 31
undeclared identifier 251 9
'i' - some operator expected 251 31

Line 118
int CountEAForSymbol(string sym, int magic)
{
  int cnt = 0;
  for(int i = PositionsTotal() - 1; i >= 0; i--)
  {
    if(!PositionSelectByIndex(i)) continue;
    if(PositionGetString(POSITION_SYMBOL) != sym) continue;
    if((int)PositionGetInteger(POSITION_MAGIC) != magic) continue;
    cnt++;
  }
  return cnt;
}

Line 209
// Partial close by opposite market order
bool PartialClose(string sym, double vol, int magic)
{
  for(int i = PositionsTotal() - 1; i >= 0; i--)
  {
    if(!PositionSelectByIndex(i)) continue;
    if(PositionGetString(POSITION_SYMBOL) != sym) continue;
    if((int)PositionGetInteger(POSITION_MAGIC) != magic) continue;
    int ptype = (int)PositionGetInteger(POSITION_TYPE);
    ENUM_ORDER_TYPE otype = (ptype == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
    return SendMarket(sym, otype, vol, 0.0, magic);
  }
  return false;
}


Line 486

void OnTick()
{
  SymbolSelect(SYMBOL_BTC, true);

  ManageForSymbol(SYMBOL_BTC, magicBTC);

  int totalEA = 0;
  for(int i = PositionsTotal() - 1; i >= 0; i--)
  {
    if(!PositionSelectByIndex(i)) continue;
    int pm = (int)PositionGetInteger(POSITION_MAGIC);
    if(pm == magicBTC) totalEA++;
  }
  if(totalEA >= 1) return;

  TryEntry(SYMBOL_BTC, magicBTC, handleATR_BTC, handleMA_BTC, handleRSI_BTC);
}


Line 251

void ManageForSymbol(string sym, int magic)

{

  double hh, ll;

  if(!GetHHLL(sym, hh, ll)) return;



  int h_atr = iATR(sym, TF, ATRPeriod);

  if(h_atr == INVALID_HANDLE) return;

  double atr = FetchLastBuffer(h_atr);

  IndicatorRelease(h_atr);

  if(atr <= 0.0) return;



  double point = SymbolInfoDouble(sym, SYMBOL_POINT);



  for(int i = PositionsTotal() - 1; i >= 0; i--)

  {

    if(!PositionSelectByIndex(i)) continue;

    if(PositionGetString(POSITION_SYMBOL) != sym) continue;

    if((int)PositionGetInteger(POSITION_MAGIC) != magic) continue;



    ulong ticket = (ulong)PositionGetInteger(POSITION_TICKET);

    double vol = PositionGetDouble(POSITION_VOLUME);

    double openp = PositionGetDouble(POSITION_PRICE_OPEN);

    int ptype = (int)PositionGetInteger(POSITION_TYPE);

    double curp = (ptype==POSITION_TYPE_BUY) ? SymbolInfoDouble(sym,SYMBOL_BID) : SymbolInfoDouble(sym,SYMBOL_ASK);

    double sl = PositionGetDouble(POSITION_SL);

    int flags = LoadFlag(ticket);



    double sl_price_val = MathAbs(openp - sl);

    if(sl_price_val <= 0.0) sl_price_val = atr * 2.0;

    double sl_points = sl_price_val / point;

    double profit_points = MathAbs(curp - openp) / point;

    double profitR = (sl_points > 0.0) ? (profit_points / sl_points) : 0.0;



    // 0.5R close 30%

    if(profitR >= 0.5 && (flags & FLAG_05R) == 0)

    {

      double close_vol = vol * 0.30;

      if(close_vol >= SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN))

      {

        if(PartialClose(sym, NormalizeLotSafe(sym, close_vol), magic))

        {

          flags |= FLAG_05R; SaveFlag(ticket, flags);

        }

      }

    }

    // 1.0R close 30%

    if(profitR >= 1.0 && (flags & FLAG_1R) == 0)

    {

      double close_vol = vol * 0.30;

      if(close_vol >= SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN))

      {

        if(PartialClose(sym, NormalizeLotSafe(sym, close_vol), magic))

        {

          flags |= FLAG_1R; SaveFlag(ticket, flags);

        }

      }

    }

    // 1.5R -> BE + 1 pip

    if(profitR >= 1.5 && (flags & FLAG_15R) == 0)

    {

      double newsl = (ptype==POSITION_TYPE_BUY) ? openp + point*1 : openp - point*1;

      if(ModifySL(ticket, newsl))

      {

        flags |= FLAG_15R; SaveFlag(ticket, flags);

      }

    }

    // 2.0R -> close 40% + SL to +0.5R

    if(profitR >= 2.0 && (flags & FLAG_2R) == 0)

    {

      double close_vol = vol * 0.40;

      if(close_vol >= SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN))

      {

        if(PartialClose(sym, NormalizeLotSafe(sym, close_vol), magic))

        {

          double newsl;

          if(ptype==POSITION_TYPE_BUY) newsl = openp + (sl_points * point) * 0.5;

          else newsl = openp - (sl_points * point) * 0.5;

          if(ModifySL(ticket, newsl))

          {

            flags |= FLAG_2R; SaveFlag(ticket, flags);

          }

        }

      }

    }



    // Band touch final close

    if((flags & FLAG_FINAL) == 0)

    {

      double atrbuf = atr * ConfidenceFactor;

      if(ptype == POSITION_TYPE_BUY)

      {

        if(curp <= ll + atrbuf)

        {

          double remaining = PositionGetDouble(POSITION_VOLUME);

          if(remaining >= SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN))

          {

            if(PartialClose(sym, NormalizeLotSafe(sym, remaining), magic))

            {

              flags |= FLAG_FINAL; SaveFlag(ticket, flags); ClearFlag(ticket);

            }

          }

        }

      }

      else

      {

        if(curp >= hh - atrbuf)

        {

          double remaining = PositionGetDouble(POSITION_VOLUME);

          if(remaining >= SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN))

          {

            if(PartialClose(sym, NormalizeLotSafe(sym, remaining), magic))

            {

              flags |= FLAG_FINAL; SaveFlag(ticket, flags); ClearFlag(ticket);

            }

          }

        }

      }

    }

  } // end positions loop

}
 

?(

  1. You show several lines but where are they precisely ?
  2. Why don't you try int i; for (i = PositionsTotal() - 1 ... ?
  3. Have you loaded your EA in N++ to count [ and ] and { and } ?
 
Adi Surya T A:
int CountEAForSymbol(string sym, int magic) {   int cnt = 0;   for(int i = PositionsTotal() - 1; i >= 0; i--)   {     if(!PositionSelectByIndex(i)) continue;     if(PositionGetString(POSITION_SYMBOL) != sym) continue;     if((int)PositionGetInteger(POSITION_MAGIC) != magic) continue;     cnt++;   }   return cnt; }

PositionSelectByIndex() doesn't exist. 

=> PositionSelectByTicket(PositionGetTicket(i) )

cf. https://www.mql5.com/en/docs/trading/positionselect

Documentation on MQL5: PositionSelect / Trade Functions
Documentation on MQL5: PositionSelect / Trade Functions
  • www.mql5.com
Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. To...
 
  int h_atr = iATR(sym, TF, ATRPeriod);

  if(h_atr == INVALID_HANDLE) return;

  double atr = FetchLastBuffer(h_atr);

  IndicatorRelease(h_atr);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate/OnStart (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
It’s crazy how many people run into problems with PositionSelectByIndex. This thing is basically an AI hallucination created by ChatGPT or other LLMs. People here are really patient helping users who rely too much on AI.