Help me fix this code pls

 
// Spot Grid Trading Expert Advisor for MQL5

input double gridStep = 10; // Grid step in points
input int maxOrders = 5; // Maximum number of grid positions
input double stopLoss = 50; // Stop loss in points
input double takeProfit = 100; // Take profit in points
input double lotSize = 0.01; // Lot size for each position

int totalOrders = 0; // Total number of grid positions opened

void OnTick()
{
   // Check if there are any open orders
   int orders = OrdersTotal();
   if (orders == 0)
   {
      // No open orders, initiate the grid
      totalOrders = 0;
      double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      for (int i = 0; i < maxOrders; i++)
      {
         double priceLevel = currentPrice - i * gridStep * _Point;
         int ticket = OrderSend(_Symbol, OP_BUYSTOP, lotSize, priceLevel, 2, 0, 0, "Buy Stop", 0, 0, clrNONE);
         if (ticket > 0)
         {
            totalOrders++;
         }
         else
         {
            Print("Error opening buy stop order: ", GetLastError());
         }
      }
   }
   else
   {
      // Check for grid hits (price crossing grid levels)
      for (int i = orders - 1; i >= 0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if (OrderType() == OP_BUYSTOP && OrderMagicNumber() == 0)
            {
               double priceLevel = OrderOpenPrice();
               double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
               
               // Check if the current price hits the grid level
               if (currentPrice >= priceLevel)
               {
                  // Close the buy stop order and open a new sell stop order
                  bool closeResult = OrderClose(OrderTicket(), OrderLots(), currentPrice, 2, clrNONE);
                  if (closeResult)
                  {
                     int ticket = OrderSend(_Symbol, OP_SELLSTOP, lotSize, currentPrice + gridStep * _Point, 2, 0, 0, "Sell Stop", 0, clrNONE);
                     if (ticket > 0)
                     {
                        totalOrders++;
                     }
                     else
                     {
                        Print("Error opening sell stop order: ", GetLastError());
                     }
                  }
                  else
                  {
                     Print("Error closing buy stop order: ", GetLastError());
                  }
               }
            }
         }
      }
   }
}

void OnDeinit(const int reason)
{
   // Close all open grid positions on deinitialization
   for (int i = OrdersTotal() - 1; i >= 0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderMagicNumber() == 0)
         {
            bool closeResult = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 2, clrNONE);
            if (!closeResult)
            {
               Print("Error closing order: ", GetLastError());
            }
         }
      }
   }
}

 
  1.  int ticket = OrderSend(_Symbol, OP_SELLSTOP, lotSize, currentPrice + gridStep * _Point, 2, 0, 0, "Sell Stop", 0, clrNONE);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  3. THANAKRIT SOEMSRI:// Spot Grid Trading Expert Advisor for MQL5

    Martingale is not a strategy. It's a betting system.

    Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    Martingale, guaranteed to blow your account eventually. If your strategy is not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020)