Help Needed: Code fails to compile with 'unexpected token' on a correct line.

 

I have this EA code that seems syntactically correct, but it always fails to compile with an "unexpected token" error. The error appears on different lines each time (like line 75 in the attached screenshot), even though the lines look correct.

I have already tried cleaning the code by pasting it into Notepad and saving it with ANSI encoding, but the problem still persists. Can anyone see what might be wrong with my file or the code itself? I have attached the .mq5 file for you to check.

Thank you for your help.

//+------------------------------------------------------------------+
//| Universal Scalper EA - Final ANSI-Ready Version (v1.45)           |
//+------------------------------------------------------------------+
#property copyright "Finalized by World's AI Collective"
#property version   "1.45"
#property strict

#include <Trade\Trade.mqh>
CTrade trade;

//--- Inputs
input ulong  InpMagicNumber = 654321;
input double InpLotSize = 0.01;
input ulong  InpSlippage = 5;
input double InpBasketProfitTarget = 1.0;
input double InpStopLossMultiplierOfCost = 3.0;
input bool   InpUseBasketTrailing = true;
input double InpTrailStartProfit = 2.0;
input double InpTrailStopDistance = 1.0;
input int    InpMA_Fast_Period = 10;
input int    InpMA_Slow_Period = 25;
input int    Inp_RSI_Period = 14;
input double Inp_RSI_Buy_Level = 55.0;
input double Inp_RSI_Sell_Level = 45.0;
input double InpMaxSpreadPips = 3.0;
input double InpMaxDailyDrawdown = 5.0;
input bool   InpUseTimeFilter = false;
input bool   TradeMonday = true;
input bool   TradeTuesday = true;
input bool   TradeWednesday = true;
input bool   TradeThursday = true;
input bool   TradeFriday = true;
input int    InpStartHour = 0;
input int    InpEndHour = 23;

//--- Global Variables
int hMA_Fast, hMA_Slow, hRSI;
datetime dayStart;
double startOfDayEquity;
bool tradingHaltedByDD = false;
double maxProfitForTrailing = 0;

//--- Helper Functions (Defined before Main Event Handlers)
void CloseAllPositionsByMagic()
{
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      if(ticket > 0 && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
      {
         trade.PositionClose(ticket);
      }
   }
}

double GetBasketProfit()
{
   double totalProfit = 0;
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if(PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
      {
         totalProfit += PositionGetDouble(POSITION_PROFIT);
      }
   }
   return totalProfit;
}

void ExecuteTrade(ENUM_ORDER_TYPE orderType)
{
   MqlTick tick;
   SymbolInfoTick(_Symbol, tick);
   double spread_cost = (tick.ask - tick.bid) * SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) * (InpLotSize / SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP));
   double commission_cost = 0;
   OrderCalcCommission(_Symbol, ORDER_TYPE_BUY, InpLotSize, tick.ask, commission_cost);
   double cost = spread_cost + commission_cost;

   if(cost <= 0)
      return;
      
   double price = (orderType == ORDER_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double slCurrency = cost * InpStopLossMultiplierOfCost;
   double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) * (InpLotSize / SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP));
   
   if(tickValue <= 0)
      return;
      
   double slPoints = slCurrency / tickValue;
   double slPrice = (orderType == ORDER_TYPE_BUY) ? price - slPoints * _Point : price + slPoints * _Point;
   
   bool sent = false;
   if(orderType == ORDER_TYPE_BUY)
   {
      sent = trade.Buy(InpLotSize, _Symbol, price, slPrice, 0, NULL);
   }
   else
   {
      sent = trade.Sell(InpLotSize, _Symbol, price, slPrice, 0, NULL);
   }
   
   if(!sent)
   {
      Print("Trade execution failed: ", trade.ResultComment());
   }
}

void CheckBasketProfitAndClose()
{
   if(PositionsTotal() > 0 && GetBasketProfit() >= InpBasketProfitTarget)
   {
      CloseAllPositionsByMagic();
   }
}

void ManageBasketTrailingStop()
{
   double currentProfit = GetBasketProfit();
   if(currentProfit > maxProfitForTrailing)
      maxProfitForTrailing = currentProfit;
   if(maxProfitForTrailing >= InpTrailStartProfit && currentProfit <= (maxProfitForTrailing - InpTrailStopDistance))
   {
      CloseAllPositionsByMagic();
      maxProfitForTrailing = 0;
   }
}

int CountPositionsByMagic(string symbol)
{
   int count = 0;
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if(PositionGetInteger(POSITION_MAGIC) == InpMagicNumber && PositionGetString(POSITION_SYMBOL) == symbol)
         count++;
   }
   return count;
}

bool IsSpreadOK()
{
   if(InpMaxSpreadPips <= 0)
      return true;
   return ((SymbolInfoDouble(_Symbol, SYMBOL_ASK) - SymbolInfoDouble(_Symbol, SYMBOL_BID)) / SymbolInfoDouble(_Symbol, SYMBOL_POINT) <= InpMaxSpreadPips);
}

bool CheckDailyDrawdown()
{
   if(InpMaxDailyDrawdown <= 0)
      return false;
   double equity = AccountInfoDouble(ACCOUNT_EQUITY);
   if(startOfDayEquity > 0)
   {
      return ((startOfDayEquity - equity) / startOfDayEquity * 100.0 >= InpMaxDailyDrawdown);
   }
   return false;
}

bool IsTradingTime()
{
   if(!InpUseTimeFilter)
      return true;
   MqlDateTime time;
   TimeCurrent(time);
   bool isDayAllowed = false;
   switch(time.day_of_week)
   {
      case 1: isDayAllowed = TradeMonday; break;
      case 2: isDayAllowed = TradeTuesday; break;
      case 3: isDayAllowed = TradeWednesday; break;
      case 4: isDayAllowed = TradeThursday; break;
      case 5: isDayAllowed = TradeFriday; break;
   }
   return (isDayAllowed && time.hour >= InpStartHour && time.hour <= InpEndHour);
}

//+------------------------------------------------------------------+
//| Main Event Handlers                                              |
//+------------------------------------------------------------------+
int OnInit()
{
   hMA_Fast = iMA(_Symbol, _Period, InpMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE);
   hMA_Slow = iMA(_Symbol, _Period, InpMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE);
   hRSI = iRSI(_Symbol, _Period, Inp_RSI_Period, PRICE_CLOSE);
   if(hMA_Fast == INVALID_HANDLE || hMA_Slow == INVALID_HANDLE || hRSI == INVALID_HANDLE)
      return(INIT_FAILED);
   trade.SetExpertMagicNumber(InpMagicNumber);
   trade.SetDeviationInPoints(InpSlippage);
   dayStart = iTime(_Symbol, PERIOD_D1, 0);
   startOfDayEquity = AccountInfoDouble(ACCOUNT_EQUITY);
   EventSetTimer(1);
   Print("EA Initialized. Version 1.45 - ANSI Final Build.");
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   EventKillTimer();
   IndicatorRelease(hMA_Fast);
   IndicatorRelease(hMA_Slow);
   IndicatorRelease(hRSI);
}

void OnTimer()
{
   if(iTime(_Symbol, PERIOD_D1, 0) > dayStart)
   {
      dayStart = iTime(_Symbol, PERIOD_D1, 0);
      startOfDayEquity = AccountInfoDouble(ACCOUNT_EQUITY);
      tradingHaltedByDD = false;
      maxProfitForTrailing = 0;
   }
   if(CheckDailyDrawdown())
   {
      if(!tradingHaltedByDD)
      {
         CloseAllPositionsByMagic();
         tradingHaltedByDD = true;
      }
      return;
   }
   if(!IsTradingTime())
   {
      return;
   }
   if(PositionsTotal() > 0)
   {
      if(InpUseBasketTrailing)
         ManageBasketTrailingStop();
      else
         CheckBasketProfitAndClose();
   }
   if(tradingHaltedByDD || CountPositionsByMagic(_Symbol) > 0)
      return;
   if(!IsSpreadOK())
   {
      return;
   }
   double maFast[2], maSlow[2], rsi[1];
   if(CopyBuffer(hMA_Fast, 0, 1, 2, maFast) < 2 || CopyBuffer(hMA_Slow, 0, 1, 2, maSlow) < 2 || CopyBuffer(hRSI, 0, 1, 1, rsi) < 1)
      return;
   bool buySignal = maFast[0] > maSlow[0] && maFast[1] <= maSlow[1] && rsi[0] > Inp_RSI_Buy_Level;
   bool sellSignal = maFast[0] < maSlow[0] && maFast[1] >= maSlow[1] && rsi[0] < Inp_RSI_Sell_Level;
   if(buySignal)
      ExecuteTrade(ORDER_TYPE_BUY);
   if(sellSignal)
      ExecuteTrade(ORDER_TYPE_SELL);
}

void OnTick()
{
   // Logic is now in OnTimer() for performance. This is left empty.
}
 
No1. No.1#property copyright "Finalized by World's AI Collective"

This forum is a space for developers who are genuinely learning and working with MQL5. If you're using an AI tool (like ChatGPT or similar) to generate code, it's your responsibility to also use that tool to debug or refine the output it creates.

Posting AI-generated code here (especially without understanding it) and expecting others to fix it for you is not acceptable. It’s not fair to ask human developers to clean up or troubleshoot code that was mass-produced by a machine. That’s not collaboration, that’s outsourcing without consent.

If you want to learn, you're absolutely welcome here, and we'll help you. But if your intent is to let an AI generate everything and offload the work of fixing it to others, this is not the right place.

Please take this into account before posting further.

 
Miguel Angel Vico Alba #:

This forum is a space for developers who are genuinely learning and working with MQL5. If you're using an AI tool (like ChatGPT or similar) to generate code, it's your responsibility to also use that tool to debug or refine the output it creates.

Posting AI-generated code here (especially without understanding it) and expecting others to fix it for you is not acceptable. It’s not fair to ask human developers to clean up or troubleshoot code that was mass-produced by a machine. That’s not collaboration, that’s outsourcing without consent.

If you want to learn, you're absolutely welcome here, and we'll help you. But if your intent is to let an AI generate everything and offload the work of fixing it to others, this is not the right place.

Please take this into account before posting further.

Thank you for the guidance, and I apologize for the misunderstanding. I understand and respect the forum's rules.

The reason I posted the code is that I'm genuinely in a learning phase. I did try to fix it myself first but was still stuck, and seeing the correct approach from experienced developers helps me a lot.

I'll be more careful in the future and will be sure to explain my own attempts more clearly. Thanks again.