Help To Delete Pending Order Code

 
Hi all, i need help. I want to delete a pending order at a specified time, example: when i set to 01/09/2024 18:00:05, then the pending order will close. Here is the error and the code:

Error on
Delete all pending orders at the specified time
'SELECT_BY_POS' - undeclared identifier
'OrderSelect' - wrong parameters count
'OrderGetTicket' - wrong parameters count

// Input parameters
input double Range = 200.0;           // Range in points
input double TakeProfit = 400.0;      // Take Profit in points
input double StopLoss = 200.0;        // Stop Loss in points
input double LotSize = 0.1;           // Lot size
input int TrailingStop = 200;         // Trailing Stop in points
input string OrderComment = "MySetup"; // Order comment
input int TimerOption = 0;            // Timer option (0: Off, 1: On)
input datetime TimerTargetTime = D'2024.08.29 18:00:00'; // Target time
input int DeletePOTimerOption = 0;    // Delete PO Timer option (0: Off, 1: On)
input datetime DeletePOTargetTime = D'2024.08.29 18:00:05'; // Time to delete pending orders

// Global variables
double BuyStopPrice, SellStopPrice;
double StopLossBuy, TakeProfitBuy, StopLossSell, TakeProfitSell;
CTrade trade;
bool tradeOpened = false;  // Flag to track if a trade has been opened

// Function declarations
void SetOrders();
void ManageTrailingStop();
void DeletePendingOrders();

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Print a message on the log
   Print("MyEA has started.");
   
   // Return initialization result
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Print a message on the log
   Print("MyEA has stopped.");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Check if a trade has already been opened
   if (!tradeOpened)
     {
      // If the timer is on and the current time is greater or equal to the target time
      if (TimerOption == 1)
        {
         if (TimeCurrent() >= TimerTargetTime)
           {
            // Calculate the prices and set the orders
            SetOrders();
           }
        }
      else
        {
         // If timer is off, immediately set the orders
         SetOrders();
        }
     }
   else
     {
      // Manage trailing stop for open orders
      ManageTrailingStop();
     }
     
   // Check if the Delete PO Timer is on and the current time is greater or equal to the target time
   if (DeletePOTimerOption == 1 && TimeCurrent() >= DeletePOTargetTime)
     {
      DeletePendingOrders();  // Delete all pending orders at the specified time
     }
  }
//+------------------------------------------------------------------+
//| Calculate the prices and set the orders                          |
//+------------------------------------------------------------------+
void SetOrders()
  {
   double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   
   // Calculate the buy stop and sell stop prices
   BuyStopPrice = price + Range * _Point;
   SellStopPrice = price - Range * _Point;
   
   // Calculate the stop loss and take profit for buy stop
   StopLossBuy = BuyStopPrice - StopLoss * _Point;
   TakeProfitBuy = BuyStopPrice + TakeProfit * _Point;
   
   // Calculate the stop loss and take profit for sell stop
   StopLossSell = SellStopPrice + StopLoss * _Point;
   TakeProfitSell = SellStopPrice - TakeProfit * _Point;
   
   // Place buy stop order
   if (!trade.BuyStop(LotSize, BuyStopPrice, _Symbol, StopLossBuy, TakeProfitBuy, ORDER_TIME_GTC, 0, OrderComment))
     {
      Print("Error placing buy stop order: ", GetLastError());
     }
   else
     {
      tradeOpened = true;
      Print("Buy stop order placed successfully.");
     }
   
   // Place sell stop order
   if (!trade.SellStop(LotSize, SellStopPrice, _Symbol, StopLossSell, TakeProfitSell, ORDER_TIME_GTC, 0, OrderComment))
     {
      Print("Error placing sell stop order: ", GetLastError());
     }
   else
     {
      tradeOpened = true;
      Print("Sell stop order placed successfully.");
     }
  }
//+------------------------------------------------------------------+
//| Manage trailing stop for open orders                             |
//+------------------------------------------------------------------+
void ManageTrailingStop()
  {
   for (int i = 0; i < PositionsTotal(); i++)
     {
      if (PositionSelect(i))
        {
         ulong ticket = PositionGetTicket(i);
         string symbol = PositionGetString(POSITION_SYMBOL);
         if (symbol != _Symbol) continue;

         double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double stopLoss = PositionGetDouble(POSITION_SL);
         double newStopLoss;

         if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            newStopLoss = currentPrice - TrailingStop * _Point;
            if (newStopLoss > stopLoss)
              {
               if (!trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
                 {
                  Print("Error modifying position: ", GetLastError());
                 }
              }
           }
         else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
           {
            newStopLoss = currentPrice + TrailingStop * _Point;
            if (newStopLoss < stopLoss)
              {
               if (!trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
                 {
                  Print("Error modifying position: ", GetLastError());
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Delete all pending orders at the specified time                  |
//+------------------------------------------------------------------+
void DeletePendingOrders()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if (OrderSelect(i,SELECT_BY_POS))
        {
         int orderType = (int)OrderGetInteger(ORDER_TYPE);
         if (orderType == ORDER_TYPE_BUY_STOP || orderType == ORDER_TYPE_SELL_STOP)
           {
            ulong ticket = OrderGetTicket();
            if (!trade.OrderDelete(ticket))
              {
               Print("Error deleting pending order: ", GetLastError());
              }
            else
              {
               Print("Pending order deleted: ", ticket);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
PTIxSTC:
'SELECT_BY_POS' - undeclared identifier
'OrderSelect' - wrong parameters count
'OrderGetTicket' - wrong parameters count

Hi, Check this code:

// Input parameters
input double Range = 200.0;           // Range in points
input double TakeProfit = 400.0;      // Take Profit in points
input double StopLoss = 200.0;        // Stop Loss in points
input double LotSize = 0.1;           // Lot size
input int TrailingStop = 200;         // Trailing Stop in points
input string OrderComment = "MySetup"; // Order comment
input int TimerOption = 0;            // Timer option (0: Off, 1: On)
input datetime TimerTargetTime = D'2024.08.29 18:00:00'; // Target time
input int DeletePOTimerOption = 0;    // Delete PO Timer option (0: Off, 1: On)
input datetime DeletePOTargetTime = D'2024.08.29 18:00:05'; // Time to delete pending orders

// Global variables
double BuyStopPrice, SellStopPrice;
double StopLossBuy, TakeProfitBuy, StopLossSell, TakeProfitSell;
CTrade trade;
bool tradeOpened = false;  // Flag to track if a trade has been opened

// Function declarations
void SetOrders();
void ManageTrailingStop();
void DeletePendingOrders();

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Print a message on the log
   Print("MyEA has started.");
   
   // Return initialization result
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Print a message on the log
   Print("MyEA has stopped.");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Check if a trade has already been opened
   if (!tradeOpened)
     {
      // If the timer is on and the current time is greater or equal to the target time
      if (TimerOption == 1)
        {
         if (TimeCurrent() >= TimerTargetTime)
           {
            // Calculate the prices and set the orders
            SetOrders();
           }
        }
      else
        {
         // If timer is off, immediately set the orders
         SetOrders();
        }
     }
   else
     {
      // Manage trailing stop for open orders
      ManageTrailingStop();
     }
     
   // Check if the Delete PO Timer is on and the current time is greater or equal to the target time
   if (DeletePOTimerOption == 1 && TimeCurrent() >= DeletePOTargetTime)
     {
      DeletePendingOrders();  // Delete all pending orders at the specified time
     }
  }
//+------------------------------------------------------------------+
//| Calculate the prices and set the orders                          |
//+------------------------------------------------------------------+
void SetOrders()
  {
   double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   
   // Calculate the buy stop and sell stop prices
   BuyStopPrice = price + Range * _Point;
   SellStopPrice = price - Range * _Point;
   
   // Calculate the stop loss and take profit for buy stop
   StopLossBuy = BuyStopPrice - StopLoss * _Point;
   TakeProfitBuy = BuyStopPrice + TakeProfit * _Point;
   
   // Calculate the stop loss and take profit for sell stop
   StopLossSell = SellStopPrice + StopLoss * _Point;
   TakeProfitSell = SellStopPrice - TakeProfit * _Point;
   
   // Place buy stop order
   if (!trade.BuyStop(LotSize, BuyStopPrice, _Symbol, StopLossBuy, TakeProfitBuy, ORDER_TIME_GTC, 0, OrderComment))
     {
      Print("Error placing buy stop order: ", GetLastError());
     }
   else
     {
      tradeOpened = true;
      Print("Buy stop order placed successfully.");
     }
   
   // Place sell stop order
   if (!trade.SellStop(LotSize, SellStopPrice, _Symbol, StopLossSell, TakeProfitSell, ORDER_TIME_GTC, 0, OrderComment))
     {
      Print("Error placing sell stop order: ", GetLastError());
     }
   else
     {
      tradeOpened = true;
      Print("Sell stop order placed successfully.");
     }
  }
//+------------------------------------------------------------------+
//| Manage trailing stop for open orders                             |
//+------------------------------------------------------------------+
void ManageTrailingStop()
  {
   for (int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if (PositionSelectByIndex(i))
        {
         ulong ticket = PositionGetTicket(i);
         string symbol = PositionGetString(POSITION_SYMBOL);
         if (symbol != _Symbol) continue;

         double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double stopLoss = PositionGetDouble(POSITION_SL);
         double newStopLoss;

         if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            newStopLoss = currentPrice - TrailingStop * _Point;
            if (newStopLoss > stopLoss)
              {
               if (!trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
                 {
                  Print("Error modifying position: ", GetLastError());
                 }
              }
           }
         else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
           {
            newStopLoss = currentPrice + TrailingStop * _Point;
            if (newStopLoss < stopLoss)
              {
               if (!trade.PositionModify(ticket, newStopLoss, PositionGetDouble(POSITION_TP)))
                 {
                  Print("Error modifying position: ", GetLastError());
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Delete all pending orders at the specified time                  |
//+------------------------------------------------------------------+
void DeletePendingOrders()
  {
   for (int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if (OrderSelect(i, SELECT_BY_POS))
        {
         int orderType = (int)OrderGetInteger(ORDER_TYPE);
         if (orderType == ORDER_TYPE_BUY_STOP || orderType == ORDER_TYPE_SELL_STOP)
           {
            ulong ticket = OrderGetTicket();
            if (!trade.OrderDelete(ticket))
              {
               Print("Error deleting pending order: ", GetLastError());
              }
            else
              {
               Print("Pending order deleted: ", ticket);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+