MQL5 - OrderSend returns error 10013, but manually the order is Placed

 

I'm trying to automate placing orders starting from an external file. The parsing of file is working well. The issue is on create order. 

I'll show you the code used to creare a Request for and Order:


      MqlTradeRequest request;
      double point=SymbolInfoDouble(orders[i].symbol, SYMBOL_POINT);
      int digits=SymbolInfoInteger(orders[i].symbol, SYMBOL_DIGITS);
      
      request.action = TRADE_ACTION_PENDING;
      request.symbol = orders[i].symbol;
      if(orders[i].direction == "BUY"){
         request.type = ORDER_TYPE_BUY_LIMIT;
      } else {
         request.type = ORDER_TYPE_SELL_LIMIT;
      }
      request.price = NormalizeDouble(orders[i].entryPrice, digits);
      //request.stoplimit = orders[i].entryPrice;
      request.magic = MAGIC_NUMBER;
      request.tp = NormalizeDouble(orders[i].takeProfit, digits);
      request.sl = NormalizeDouble(orders[i].stopLoss, digits);
      request.comment = orders[i].comment;
      request.volume = 0.01;
      request.deviation = 5;
      request.type_time = ORDER_TIME_GTC;
      
      MqlTradeResult result;
      OrderSend(request, result);
      if(result.retcode == TRADE_RETCODE_DONE || result.retcode == TRADE_RETCODE_PLACED){
         PrintFormat("Order placed %d", result.order);
      } else {
         PrintFormat("Error %d", result.retcode);
         return false;
      }


I can't understand the error. The Journal shows me:

'166137079': failed buy limit 0.01 EURUSD at 1.04956 sl: 1.03495 tp: 1.06950 [Invalid request]

But if I create manually a pending order with the same values it is placed correctly.



Thanks for your supply.


 

try this

int OnInit()
  {
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
  string orders_symbols[]={"EURUSD"};
  double orders_entry[]={1.03432};
  double orders_tp[]={1.06060};
  double orders_sl[]={1.02033};
  string orders_direction[]={"BUY"};
  static bool PLACED=false;
  if(!PLACED){
  PLACED=true;
  ResetLastError();
  double minimum_allowed=_Point*((int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL));
  if(GetLastError()==0){
      MqlTradeRequest request={};
      ResetLastError();
      double point=SymbolInfoDouble(orders_symbols[0], SYMBOL_POINT);
      if(GetLastError()==0){ResetLastError();
      int digits=(int)SymbolInfoInteger(orders_symbols[0], SYMBOL_DIGITS);
      if(GetLastError()==0){ResetLastError();
      
            request.action = TRADE_ACTION_PENDING;
            request.symbol = orders_symbols[0];
            if(orders_direction[0] == "BUY"){
               request.type = ORDER_TYPE_BUY_LIMIT;
            } else {
               request.type = ORDER_TYPE_SELL_LIMIT;
            }
            request.type_filling=GetFillingA(orders_symbols[0],request.type);
            request.price = NormalizeDouble(orders_entry[0], digits);
            //request.stoplimit = orders[i].entryPrice;
            request.magic = 123;
            request.tp = NormalizeDouble(orders_tp[0], digits);
            request.sl = NormalizeDouble(orders_sl[0], digits);
            request.comment = "com";
            request.volume = 0.01;
            request.deviation = 100;
            request.type_time = ORDER_TIME_GTC;
            
            MqlTradeResult result;
            OrderSend(request, result);
            if(result.retcode == TRADE_RETCODE_DONE || result.retcode == TRADE_RETCODE_PLACED){
               PrintFormat("Order placed %d", result.order);
            } else {
               PrintFormat("Error %d", result.retcode);
               return;
            }      
      
      }else{Print("Cannot read symbol digits");}
      }else{Print("Cannot read symbol point");}
  }else{Print("Cannot acquire limit");}
  }
  }

//+------------------------------------------------------------------+
ENUM_ORDER_TYPE_FILLING GetFillingA( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}