OrderSend() Error 10013 - Invalid request

 
Hi All, I am having this error when calling OrderSend function. I hope someone can help me.

This is error log :

2021.11.11 04:10:45.254 TEST (EURUSD,M5) Alert: Open market order: Error 10013 - Invalid request
2021.11.11 04:10:45.254 TEST (EURUSD,M5) MqlTradeRequest - action:1, comment:, deviation:20, expiration:2021.11.11 23:10:41, magic:132781905614498815, order:0, position:0, position_by:0, price:1.14799, ls:0.0, stoplimit:0.0, symbol:EURUSD, tp:0.0, type:0, type_filling:-370154256, type_time:0, volume:0.01
2021.11.11 04:10:45.254 TEST (EURUSD,M5) MqlTradeResult - ask:0.0, bid:0.0, comment:Invalid request, deal:0, order:0, price:0.0, request_id:0, retcode:10013, retcode_external:0, volume:0.0
2021.11.11 04:10:45.254 TEST (EURUSD,M5) Open buy order #0: 10013 - Invalid request, Volume: 0.0, Price: 0.0, Bid: 0.0, Ask: 0.0


Here's the code : I've got it from the Book : Expert Advisor for Metatrader 5 by Andrew Young.

// Reset Request Result
void CTrade::ResetRR()
{
   ZeroMemory(request);
   ZeroMemory(result);
}

// Open Position
bool CTrade::OpenPosition(string pSymbol,ENUM_ORDER_TYPE pType,double pVolume,double pStop=0.000000,double pProfit=0.000000,string pComment = NULL)
{
   ResetRR(); 
   
   request.action = TRADE_ACTION_DEAL;
        request.symbol = pSymbol;
        request.type = pType;
        request.sl = pStop;
        request.tp = pProfit;
        request.comment = pComment;
        request.deviation = deviation;
        request.type_filling = fillType;
        request.magic = magicNumber;
        request.expiration = TimeCurrent()+86400;
        
        // Calculate lot size
        double positionVol = 0;
        long positionType = WRONG_VALUE;
        
        if(PositionSelect(pSymbol) == true)
        {
                positionVol = PositionGetDouble(POSITION_VOLUME);
                positionType = PositionGetInteger(POSITION_TYPE);
        }
                
        if((pType == ORDER_TYPE_BUY && positionType == POSITION_TYPE_SELL) || (pType == ORDER_TYPE_SELL && positionType == POSITION_TYPE_BUY)) 
        {
                request.volume = pVolume + positionVol;
        }
        else request.volume = pVolume;

        // Order loop
        int retryCount = 0;
        int checkCode = 0;
        
        do 
        {
                if(pType == ORDER_TYPE_BUY) request.price = SymbolInfoDouble(pSymbol,SYMBOL_ASK);
                else if(pType == ORDER_TYPE_SELL) request.price = SymbolInfoDouble(pSymbol,SYMBOL_BID);
                
                bool sent = OrderSend(request,result);
                
                checkCode = CheckReturnCode(result.retcode);
                
                if(checkCode == CHECK_RETCODE_OK) break;
                else if(checkCode == CHECK_RETCODE_ERROR)
                {
                        string errDesc = TradeServerReturnCodeDescription(result.retcode);
                        Alert("Open market order: Error ",result.retcode," - ",errDesc);
                        LogTradeRequest();
                        break;
                }
                else
                {
                        Print("Server error detected, retrying...");
                        Sleep(RETRY_DELAY);
                        retryCount++;
                }
        }
        while(retryCount < MAX_RETRIES);
        
        if(retryCount >= MAX_RETRIES)
        {
                string errDesc = TradeServerReturnCodeDescription(result.retcode);
                Alert("Max retries exceeded: Error ",result.retcode," - ",errDesc);
        }
        
        string orderType = CheckOrderType(pType);
        
        string errDesc = TradeServerReturnCodeDescription(result.retcode);
        Print("Open ",orderType," order #",result.order,": ",result.retcode," - ",errDesc,", Volume: ",result.volume,", Price: ",result.price,", Bid: ",result.bid,", Ask: ",result.ask);
        
        if(checkCode == CHECK_RETCODE_OK) 
        {
                Comment(orderType," position opened at ",result.price," on ",pSymbol);
                return(true);
        }
        else return(false);   

}




Calling code in OnInit:

   CTrade pp;
   
   string symbol= "EURUSD"; 
   pp.Buy(_Symbol,0.01);
Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
OrderSend - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

These two fields raise questions:

        request.type_filling = fillType;
        ***
        request.expiration = TimeCurrent()+86400;

Are you sure you are filling them in correctly? To check - use the code based on the Standard Library.

 
Vladimir Karputov #:

These two fields raise questions:

Are you sure you are filling them in correctly? To check - use the code based on the Standard Library.

Hi Vladimir,

  Thanks for the reply. You are right. After I set the filltype properties to ORDER_FILLTYPE_IOC it send the market order
successfully. Thanks.

 

I have Error is given . It says "Order Modify Failed. Error code: 10013" .



void OnTick()
  {
   MqlTick tick;
   if(SymbolInfoTick(Symbol(), tick))
     {
      double entry = tick.ask;
      double tick_size = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);

      MqlTradeRequest request = {};
      MqlTradeResult result = {};

      // Open a new position with zero stop loss and take profit
      request.action = TRADE_ACTION_DEAL;
      request.symbol = Symbol();
      request.volume = 0.1; // Modify this to your desired volume.
      request.type = ORDER_TYPE_BUY; // Modify this to ORDER_TYPE_SELL for a sell order.
      request.price = entry;
      request.sl = entry - 100 * tick_size; // Zero stop loss.
      request.tp = entry + 100 * tick_size; // Zero take profit.

      if(!OrderSend(request, result))
        {
         Print("Order Failed = ", result.retcode);
         return;
        }
      else
        {
         Print("Order placed. Ticket: ", result.order);
        }

      Sleep(5000); // 5 seconds delay (modify this as needed).

      ulong Pos_ticket = PositionGetInteger(POSITION_TICKET);
      ENUM_POSITION_TYPE Pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      double Pos_entry=PositionGetDouble(POSITION_PRICE_OPEN);
      string Pos_Symbol=PositionGetString(POSITION_SYMBOL);
      double Pos_Volume=PositionGetDouble(POSITION_VOLUME);

      MqlTradeRequest modifyRequest = {};
      MqlTradeResult modifyResult = {};

      modifyRequest.symbol = Pos_Symbol;
      modifyRequest.action = TRADE_ACTION_SLTP;
      modifyRequest.position = Pos_ticket;
      modifyRequest.volume = Pos_Volume;
      modifyRequest.type_filling = ORDER_FILLING_FOK;

      if(Pos_type == POSITION_TYPE_BUY)
        {
         modifyRequest.sl = Pos_entry - 500 * tick_size;
         modifyRequest.tp = Pos_entry + 500 * tick_size;
        }
      else
         if(Pos_type == POSITION_TYPE_SELL)
           {
            modifyRequest.sl = Pos_entry + 500 * tick_size;
            modifyRequest.tp = Pos_entry - 500 * tick_size;
           }

      if(OrderSend(modifyRequest, modifyResult))
        {
         Print("Order Modified. New SL: ", modifyRequest.sl, ", New TP: ", modifyRequest.tp);
        }
      else
        {
         Print("Order Modify Failed. Error code: ", modifyResult.retcode);
        }
     }
  }

Please Solve this 🙏..

 
Check bid and ask (you buy the ask and sell the bid!!) and your prices and use  MqlTradeCheckResult the order before sending.
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Request Check Result Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Request Check Result Structure
  • www.mql5.com
Request Check Result Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Carl Schreiber #:
Check bid and ask (you buy the ask and sell the bid!!) and your prices and use  MqlTradeCheckResult the order before sending.


void OnTick()
  {
   ulong Pos_ticket = PositionGetInteger(POSITION_TICKET);
   ENUM_POSITION_TYPE Pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
   double Pos_entry = PositionGetDouble(POSITION_PRICE_OPEN);
   double Pos_Volume = PositionGetDouble(POSITION_VOLUME);
   string Pos_Symbol = PositionGetString(POSITION_SYMBOL);

   MqlTick tick;
   if(SymbolInfoTick(Symbol(),tick))
     {
      MqlTradeRequest request = {};
      MqlTradeResult result = {};
      MqlTradeCheckResult check = {};

      double entry = tick.ask;
      double tick_size = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);

      request.symbol = Symbol();
      request.volume = 0.1;
      request.action = TRADE_ACTION_DEAL;
      request.price = entry;
      request.type = ORDER_TYPE_BUY;

      request.magic = 123456;
      request.type_filling = ORDER_FILLING_FOK;
      request.sl = entry - 500 * tick_size;
      request.tp = entry + 500 * tick_size;

      if(OrderCheck(request,check))
        {
         if(OrderSend(request,result))
           {
            Print("Order Send Placed = ",result.order);
           }
         else
           {
            Print("Order Send Failed = ",result.retcode);
           }
        }
      else
        {
         Print("Order Checked to Send Failed = ",check.retcode);
        }

      MqlTradeRequest Modifyrequest={};
      MqlTradeResult Modifyresult={};
      MqlTradeCheckResult Modifycheck ={};

      double sl =PositionGetDouble(POSITION_SL);
      double tp =PositionGetDouble(POSITION_TP);
      
      Modifyrequest.symbol = Pos_Symbol;
      Modifyrequest.action = TRADE_ACTION_SLTP;
      Modifyrequest.position = Pos_ticket;
      Modifyrequest.sl = sl;
      Modifyrequest.tp = tp;

      if(Pos_type == POSITION_TYPE_BUY)
        {
         sl = Pos_entry - 500 * tick_size;
         tp = Pos_entry + 500 * tick_size;
        }
      else
         if(Pos_type == POSITION_TYPE_SELL)
           {
            sl = Pos_entry + 500 * tick_size;
            tp = Pos_entry - 500 * tick_size;
           }
      if(OrderCheck(Modifyrequest,Modifycheck))
        {
         if(OrderSend(Modifyrequest,Modifyresult))
           {
            Print("Order Modified = ",Modifyresult.order);
           }
         else
           {
            Print("Order Modify Failed = ",Modifyresult.retcode);
           }
        }
      else
        {
         Print("Order Checked to Modify Failed = ",Modifycheck.retcode);
        }

     }
  }
It says Error "Order Checked to Modify Failed = 10013" . Sir, Please solve it🙏🙏
 
 
i have same error. please help me 
Invalid request reply code 10013

im trying to set takeprofit and stoploss for every  trade.


this is the code : 

//+------------------------------------------------------------------+
//|                                                    AutoSLTP2.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    Print("0N init");
    return(INIT_SUCCEEDED);
}



//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    //EventKillTimer(); // destroy timer
}

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,const MqlTradeRequest& request,const MqlTradeResult& result)

{
Print("0N TRANSACTON");
    if (trans.type == TRADE_TRANSACTION_ORDER_ADD)
    {
    Print("insidee type checl");
      MqlTradeResult result2 = result;
        SetStopLossAndTakeProfit(request, result2);
    }
}
void OnTick()
  {
   //Print("0N Tick");
  }


//+------------------------------------------------------------------+
//| Set Stop Loss and Take Profit                                    |
//+------------------------------------------------------------------+
void SetStopLossAndTakeProfit(const MqlTradeRequest& request,
                              MqlTradeResult& result)
{
Print("Symbol: ", request.symbol);
    double stopLoss = CalculateStopLoss(request.symbol);
    double takeProfit = CalculateTakeProfit(request.symbol);
    
        Print("Stop Loss: ", stopLoss); // Add this line to print out stop loss
    Print("Take Profit: ", takeProfit); // Add this line to print out take profit
    
    MqlTradeRequest modRequest = request;
    modRequest.sl = stopLoss;
    modRequest.tp = takeProfit;

    Print("Attention Sending Order ");
    Print(modRequest.sl);
    Print(modRequest.tp);
       
    ulong ticket = OrderSend(modRequest, result);
    Print(__FUNCTION__,": ",result.comment," reply code ",result.retcode);
    if(ticket < 0)
    {
        Print("OrderSend failed, Error code: ", GetLastError());
        Print("OrderSend failed, Reason: ", result.comment);
    }
    else
    {
        Print("Order sent successfully with ticket: ", ticket);
    }
}


//+------------------------------------------------------------------+
//| Calculate Stop Loss                                             |
//+------------------------------------------------------------------+
double CalculateStopLoss(const string symbol)
{
    double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
    double stopLoss = NormalizeDouble(point * 300, Digits());
    Print("SLLLL IS ");
    Print(stopLoss);
    return stopLoss;
}

//+------------------------------------------------------------------+
//| Calculate Take Profit                                           |
//+------------------------------------------------------------------+
double CalculateTakeProfit(const string symbol)
{
    double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
    double takeProfit = NormalizeDouble(point * 900, Digits());
    Print("TPPP IS ");
    Print(takeProfit);
    return takeProfit;
}



Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.05.14
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes

You are sending invalid request. Use Crade class with standalone metaquotes library. I never seen a similar issue.
Reason: