Can someone please identify the the cause of this? < [invalid request] OrderSend failed with error 4756 >

 


Im writing an EA which will place market order at a specific time, everything seemed to be right, no bug, no warning were found when I compiled it. But the problem is when I run it, the system kept warning that my order is invalid

CTrade::OrderSend: market buy 0.66 EURUSD sl: 1.07209 tp: 2.00000 [invalid request]

OrderSend failed with error 4756

I tried to figure out what is that error, and found this, but I can't stand why the request cannot be sent

ERROR CODE LIST (fromMQL5)

ERR_TRADE_SEND_FAILED

4756

Trade request sending failed


Below is the full code, I'm new to this so it may not be optimized. You can just skip the input part. I guess the problem comes from the order send function, but can't figure out what was wrong

And because I need to open 2 positions at the same time, but I still want to keep my positions without taking profit in the next open time, so I cannot use PositionsTotal() to limit positions, so may be Ctrade.Buy or Ctrade.Sell cannot work here

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

#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Object.mqh>

CTrade trade;

bool ordersSent=false;
bool open_buy = true;
bool open_sell = true;
MqlDateTime timeo, timel;
datetime timeClose, timeOpen, timeReset, timeLimit;
input bool bt = true;
//+------------------------------------------------------------------+
//| Input                                                            |
//+------------------------------------------------------------------+
double input Mult = 1;              // Stoploss multiplier
input double Percentt = 1;       // Risk/position (number)
double Risk = Percentt/10;      // Risk/position (%)
input double Total_risk = 0.7;   // Total risk (%)
input int length=14; //ATR handle
//--------

input bool trailing = true;   // Trailing allowed? true = trailing on
input string text1=""; //.
input string text2=""; // INPUT TIME //=======================================
input string text3="";//.
input int t_trailing1 = 10;  // 0. Time to start trailing stop

input int openhour=11;       // 1.1. 1st order - Open new position time (hour) 
input int openmin = 05;       // 1.2. 1st order - Open new position time (min)

//--------

input bool o_again = true;    // Open order again? 
double vol_buy1, vol_sell1, sl_buy1, sl_sell1, vbuy2, vsell2, sbuy2, ssell2, sbtest, sstest;
double a, b; 
double AccBalance, open, high, low, close,atr_value, sp;
string timecomment;


int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
 

void OnDeinit(const int reason)
  {   
  }
  
//+------------------------------------------------------------------+
//| ON TICK                                                          |
//+------------------------------------------------------------------+

void OnTick()
{
   /* Account info */ AccBalance = AccountInfoDouble(ACCOUNT_BALANCE);
   /* ATR INFO */ atr_value = MathRound(GetATRValue(14, 0)*100000)/100000;

   /* PRICE INFO */ open  = iOpen(Symbol(),PERIOD_CURRENT,0);
   /* PRICE INFO */ high  = iHigh(Symbol(),PERIOD_H1,1);
   /* PRICE INFO */ low   = iLow(Symbol(),PERIOD_H1,1);
   /* PRICE INFO */ close = iClose(Symbol(),PERIOD_CURRENT,0);
   /* PRICE INFO */ sp    = iSpread(Symbol(),PERIOD_CURRENT,0)/100000;

   /* Stoploss Buy   */    sl_buy1   =  low - Mult * atr_value - (bt==false?sp:0);
   /* Stoploss Sell  */    sl_sell1  =  high + Mult * atr_value + (bt==false?sp:0);            
   /* Volume Buy     */    vol_buy1   =  MathFloor((AccBalance * Risk) / (MathAbs(sl_buy1 - close) * 100003 * 100003 * SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE)+3) * 10) / 100;
   /* Volume Sell    */    vol_sell1  =  MathFloor((AccBalance * Risk) / (MathAbs(sl_sell1 - close) * 100003 * 100003 * SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE)+3) * 10) / 100;

//+------------------------------------------------------------------+
//| Set time                                                         |
//+------------------------------------------------------------------+

   TimeCurrent(timeo);
   timeo.hour = openhour;
   timeo.min = openmin;
   timeOpen  = StructToTime(timeo);
   timecomment=TimeToString(timeOpen);
   
//+------------------------------------------------------------------+
//| Send order                                                       |
//+------------------------------------------------------------------+

   if (TimeCurrent()!=timeOpen && !ordersSent)
            {
               SendBuy();//=======================================================
               SendSell();                                                      //
            }                                                                   //
   comment();                                                                   //
}                                                                               //
//+------------------------------------------------------------------+          //
//| Send order                                                       |          //
//+------------------------------------------------------------------+          // 
                                                                                //
                                                                                //
void SendBuy()//    <<<===========================================================
   {
      if (open_buy = true)
         {
            MqlTradeRequest request1;
            MqlTradeResult result1;
         
            request1.action = TRADE_ACTION_DEAL;
            request1.symbol = _Symbol;
            request1.volume = vol_buy1;
            request1.price = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
            request1.sl = sl_buy1;
            request1.tp = 2;
            request1.deviation = 10000;
            request1.type = ORDER_TYPE_BUY;
            request1.type_filling = ORDER_FILLING_RETURN;
            request1.magic = 123456;
            request1.comment = "Buy order"+TimeToString(TimeCurrent());
         
            if (!trade.OrderSend(request1, result1)) {Print("OrderSend failed with error ", GetLastError());}
            else {open_buy = false;}
         }
   }
   
void SendSell()
   {
      if (open_sell = true)
         {
            MqlTradeRequest request2;
            MqlTradeResult result2;
         
            request2.action = TRADE_ACTION_DEAL;
            request2.symbol = _Symbol;
            request2.volume = vol_sell1;
            request2.price = SymbolInfoDouble(Symbol(),SYMBOL_BID);
            request2.sl = sl_sell1;
            request2.tp = 1;
            request2.deviation = 10000;
            request2.type = ORDER_TYPE_SELL;
            request2.type_filling = ORDER_FILLING_RETURN;
            request2.magic = 654312;
            request2.comment = "Sell order"+TimeToString(TimeCurrent());
         
            if (!trade.OrderSend(request2, result2)) {Print("OrderSend failed with error ", GetLastError());}
            else {open_sell = false;}
         }
   }

//+------------------------------------------------------------------+
//| ATR                                                              |
//+------------------------------------------------------------------+

double GetATRValue(int atr_period, int shift)
   {
      int atr_handle = iATR(_Symbol, PERIOD_H1, atr_period);
      if (atr_handle == INVALID_HANDLE)
         {
            Print("Error getting ATR handle: ", GetLastError());
            return (0);
         }
      double atr_array[];
      int copied = CopyBuffer(atr_handle, 0, shift, 1, atr_array);
      if (copied <= 0)
         {
            Print("Error copying ATR values: ", GetLastError());
            return (0);
         }
       return (atr_array[0]);   // Return the ATR value
   }
   
//+------------------------------------------------------------------+
//| Comment                                                          |
//+------------------------------------------------------------------+

void comment()
   {
      Comment(StringFormat("Info"
                      "\n"
                      "\n Buy"
                      "\n   Vol  =  %G"
                      "\n   SL   =  %G"
                      "\n Sell"
                      "\n   Vol  =  %G"
                      "\n   SL   =  %G" 
                      "\n   open_buy   =  %G" 
                      "\n   ordersSent   =  %G" 
                      "\n   time set   =  %s" 
                      "\n   time now =  %s" 
                      ,vol_buy1,sl_buy1, vol_sell1,sl_sell1, open_buy,ordersSent,timecomment, TimeToString(TimeCurrent())));
   }
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors
  • www.mql5.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Viet Tung Nguyen:


Im writing an EA which will place market order at a specific time, everything seemed to be right, no bug, no warning were found when I compiled it. But the problem is when I run it, the system kept warning that my order is invalid

CTrade::OrderSend: market buy 0.66 EURUSD sl: 1.07209 tp: 2.00000 [invalid request]

OrderSend failed with error 4756

I tried to figure out what is that error, and found this, but I can't stand why the request cannot be sent

ERROR CODE LIST (fromMQL5)

ERR_TRADE_SEND_FAILED

4756

Trade request sending failed


Below is the full code, I'm new to this so it may not be optimized. You can just skip the input part. I guess the problem comes from the order send function, but can't figure out what was wrong


Your OrderSend command is indeed wrong, if you are using the CTrade class then you are using the instructions in the library which you can see in the following link:

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade


Documentation on MQL5: Standard Library / Trade Classes / CTrade
Documentation on MQL5: Standard Library / Trade Classes / CTrade
  • www.mql5.com
CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

The problem could be too close entry price for the order.

if(entry>Bid+SYMBOL_TRADE_FREEZE_LEVEL*_Point)
        //send sell limit order
Reason: