Trade Request Returns Invalid Request

 

Hi, can someone correct me what is the mistake below?

I got error code 10013 which mean invalid request.

FYI, I am using MetaQuotes-Demo account.


Thank you very much.


My code

bool Trade_Buy()
 {
   ZeroMemory(my_request);
   ZeroMemory(my_result);
   ZeroMemory(my_check_result);
  
   my_request.action       = TRADE_ACTION_PENDING;         // Setting a pending order
   my_request.magic        = magic_number;                         // ORDER_MAGIC
   my_request.symbol       = _Symbol;                                 // Symbol
   my_request.volume       = 1;                                           // Volume in 0.1 lots
   my_request.price        = 0.79;                                         // Price, reaching which the order must be executed
   my_request.stoplimit    = 0.89;                                        // Pending order is not placed if reach stop limit
   my_request.sl           = 0.86;                                           // Stop Loss
   my_request.tp           = 0.9;                                            // Take Profit
   my_request.type         = ORDER_TYPE_BUY;                    // order type
   my_request.type_filling = ORDER_FILLING_FOK;
   my_request.type_time    = ORDER_TIME_DAY;

   if(OrderCheck(my_request, my_check_result) == false)
   {
      Alert("ORDER_TYPE_BUY check failed\r\n");
      return false; 
   }
  if(OrderSend(my_request, my_result) == false)
   {
      Alert("ORDER_TYPE_BUY failed\r\n");
      return false;
   } // if the order is successfully accepted by the trade server, the OrderSend() function returns true.

   return true;
}

Watch

MqlTradeRequest{  action:TRADE_ACTION_PENDING magic:55555 order:0 symbol:"NZDCAD" volume:1 price:0.79 stoplimit:0.89 sl:0.86 tp:0.9 deviation:0 type:ORDER_TYPE_BUY type_filling:ORDER_FILLING_FOK type_time:ORDER_TIME_DAY expiration:D'1970.01.01 00:00:00… }


MqlTradeResult{  retcode:10013 deal:0 order:0 volume:0 price:0 bid:0 ask:0 comment:"Invalid request" request_id:2 reserved:[128] }

 
You are using TRADE_ACTION_PENDING and ORDER_TYPE_BUY, a BUY is not a pending. Check the documentation please.
 

Hello MQL5 Community.

I am getting this error when I ran my code in demo account. The error is "invalid request",. what is wrong with my code?

void GenerateTradeSignal(double ema5Buffer_0, double ema20Buffer_0, double ema5Buffer_1, double ema20Buffer_1)
  {
   if(ema5Buffer_0 > ema20Buffer_0 && ema5Buffer_1 < ema20Buffer_1 && ema5Buffer_0 - ema20Buffer_0 > confirmationThreshold && ema5Buffer_0 - ema20Buffer_0 > priceMovementThreshold)
     {
      Print("Buy signal generated: confirmationThreshold = ", confirmationThreshold, ", priceMovementThreshold = ", priceMovementThreshold);

      // Place buy order at the crossover price
      double crossoverPriceBuy = SymbolInfoDouble(Symbol(), SYMBOL_ASK); // Get the current ask price
      MqlTradeRequest request;
      MqlTradeResult result;

      request.action = TRADE_ACTION_DEAL;
      request.symbol = Symbol();
      request.volume = 0.01;
      request.price = crossoverPriceBuy;
      request.deviation = 1;
      request.type = ORDER_TYPE_BUY;
      request.type_filling = ORDER_FILLING_FOK;
      request.magic = 123456;
      request.comment = "Buy Order";

      double stopLossPercentage = 9.0;
      double stopLossLevel = crossoverPriceBuy - (crossoverPriceBuy * stopLossPercentage / 100.0);
      request.sl = stopLossLevel;

      double takeProfitPercentage = 13.0;
      double takeProfitLevel = crossoverPriceBuy + (crossoverPriceBuy * takeProfitPercentage / 100.0);
      request.tp = takeProfitLevel;

      if(OrderSend(request, result))
        {
         if(result.retcode == TRADE_RETCODE_DONE)
           {
            Print("Buy order placed successfully");
            ApplyTrailingStop(stopLossPercentage);
           }
         else
           {
            Print("Failed to place buy order. Error:", result.retcode);
           }
        }
     }
   else
      if(ema5Buffer_0 < ema20Buffer_0 && ema5Buffer_1 >= ema20Buffer_1 && ema20Buffer_0 - ema5Buffer_0 > confirmationThreshold && ema20Buffer_0 - ema5Buffer_0 > priceMovementThreshold)
        {
         Print("Sell signal generated: confirmationThreshold = ", confirmationThreshold, ", priceMovementThreshold = ", priceMovementThreshold);

         // Place sell order at the crossover price
         double crossoverPriceSell = iClose(Symbol(), 0, 1); // Get the closing price of the previous candle
         MqlTradeRequest request;
         MqlTradeResult result;

         request.action = TRADE_ACTION_DEAL;
         request.symbol = Symbol();
         request.volume = 0.01;
         request.price = crossoverPriceSell;
         request.deviation = 1;
         request.type = ORDER_TYPE_SELL;
         request.type_filling = ORDER_FILLING_FOK;
         request.magic = 123456;
         request.comment = "Sell Order";

         double stopLossPercentage = 9.0;
         double stopLossLevel = crossoverPriceSell + (crossoverPriceSell * stopLossPercentage / 100.0);
         request.sl = stopLossLevel;

         double takeProfitPercentage = 13.0;
         double takeProfitLevel = crossoverPriceSell - (crossoverPriceSell * takeProfitPercentage / 100.0);
         request.tp = takeProfitLevel;

         if(OrderSend(request, result))
           {
            if(result.retcode == TRADE_RETCODE_DONE)
              {
               Print("Sell order placed successfully");
               ApplyTrailingStop(stopLossPercentage);
              }
            else
              {
               Print("Failed to place sell order. Error:", result.retcode);
              }
           }
        }
      else
        {
         // No valid crossover or false signal
         Print("No valid crossover detected or false signal");
        }
  }

Any assistance will be appreciated. Thank you

Francis