invalid request 10013 every time the EA try to open a trade

 

I stuck with this error, I'm looking where the mistake is for the last 24h, and I'm completely blind.

The error appear in the strategy tester log like this : "invalid request" and with the retcode 10013.

Below is the full code. It's a training code where I try different and basic trading logic, I don't except it to be profitable, I just want to see it work correctly without error...


//+------------------------------------------------------------------+
//|                                                          EMA.mq5 |
//|                                                       CrokCrypto |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "CrokCrypto"
#property link      "https://www.mql5.com"
#property version   "1.00"


#include <Trade\Trade.mqh>
CTrade myTradingControlPanel; 
MqlRates PriceDataTableM5[];   
double leviermax = 3;
double input dailylossmax = 1000;
double capital;
bool safemode, ultrasafemode;
double EMA100M5DataTable[], EMA200M5DataTable[], EMA300M5DataTable[];
double currentBid, currentAsk, StopLoseUSD, StopLosePer, TakeProfitUSD, Size, SizeMax, SizeReel;
int numberofEMA100M5DataPoints, numberofEMA200M5DataPoints, numberofEMA300M5DataPoints, EMA100M5ControlPanel, EMA200M5ControlPanel, EMA300M5ControlPanel, numberOfPriceDataPointsM5;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   ArraySetAsSeries(PriceDataTableM5,true);
   
   ArraySetAsSeries(EMA100M5DataTable,true);
   EMA100M5ControlPanel = iMA(_Symbol,PERIOD_M5,100,0,MODE_EMA,PRICE_CLOSE);
   
   ArraySetAsSeries(EMA200M5DataTable,true);
   EMA200M5ControlPanel = iMA(_Symbol,PERIOD_M5,200,0,MODE_EMA,PRICE_CLOSE);
   
   ArraySetAsSeries(EMA300M5DataTable,true);
   EMA300M5ControlPanel = iMA(_Symbol,PERIOD_M5,300,0,MODE_EMA,PRICE_CLOSE);

   return(INIT_SUCCEEDED);
   
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

   
  }
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//+---------------------------Setup Data-----------------------------+
//+------------------------------------------------------------------+

   capital = AccountInfoDouble(ACCOUNT_BALANCE); // Get $ balance
   
   if(AccountInfoDouble(ACCOUNT_BALANCE) <= 9000) // See if in a drawdown
      {
      safemode = true;
      }
   else
      {
      safemode = false;
      }
      
   if(AccountInfoDouble(ACCOUNT_BALANCE) <= 8500) // See if in a big drawdown
      {
      ultrasafemode = true;
      }
   else
      {
      ultrasafemode = false;
      }
   
   currentBid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get latest Bid Price
   currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price
   
   numberOfPriceDataPointsM5 = CopyRates(_Symbol,PERIOD_M5,0,10,PriceDataTableM5); // Get OHLC 5min TF
   
   numberofEMA100M5DataPoints = CopyBuffer(EMA100M5ControlPanel,0,0,10,EMA100M5DataTable); // Get EMA100 5min TF
   numberofEMA200M5DataPoints = CopyBuffer(EMA200M5ControlPanel,0,0,10,EMA200M5DataTable); // Get EMA200 5min TF
   numberofEMA300M5DataPoints = CopyBuffer(EMA300M5ControlPanel,0,0,10,EMA300M5DataTable); // Get EMA300 5min TF


if(Closed_Profit_Today() > dailylossmax*-0.475) // If more than 475$ loss, stop trade for the day
   {
   
//+---------------------------5min TF--------------------------------+
//+------------------------------------------------------------------+

   if(EMA100M5DataTable[0] > EMA200M5DataTable[0] && EMA200M5DataTable[0] > EMA300M5DataTable[0]) // Uptrend
      {
      if((EMA100M5DataTable[0]-EMA200M5DataTable[0])/EMA200M5DataTable[0] > 0.0025 && (EMA200M5DataTable[0]-EMA300M5DataTable[0])/EMA200M5DataTable[0] > 0.0025 && (EMA100M5DataTable[0]-EMA200M5DataTable[0])/EMA200M5DataTable[0] > (EMA200M5DataTable[0]-EMA300M5DataTable[0])/EMA200M5DataTable[0]) // No range
         {
         if(currentAsk < EMA200M5DataTable[0] && currentAsk > EMA200M5DataTable[0]*0.9995) // Price hit EMA200 in a uptrend
            {
            if(PositionSelect(_Symbol) == false) // Don't have an open position
               {
               StopLoseUSD = Round(EMA300M5DataTable[0]); // Stop Lose price
               StopLosePer = (currentAsk-EMA300M5DataTable[0])/currentAsk; // Stop Lose %
               TakeProfitUSD = Round(EMA100M5DataTable[0]); // Take Profit price
               Size = capital*leviermax/currentAsk; // Size of the position with full leverage
               SizeMax = (dailylossmax*0.475)/StopLosePer/currentAsk; // Size max of the position (risk management)
               if(SizeMax <= Size)
                  {
                  SizeReel = NormalizeDouble(SizeMax,2); // Size of the position cut to 2 decimals
                  }
                  else
                  {
                  SizeReel = NormalizeDouble(Size,2); // Size of the position cut to 2 decimals
                  }
               if(safemode == true) // capital go under 9000$
                  {
                  SizeReel = NormalizeDouble(SizeReel/2,2);
                  }
               if(ultrasafemode == true) // capital go under 8500$
                  {
                  SizeReel = NormalizeDouble(SizeReel/4,2);
                  }
               MqlTradeRequest request;
               request.action = TRADE_ACTION_DEAL;
               request.symbol = _Symbol;
               request.volume = SizeReel;
               request.type = ORDER_TYPE_BUY;
               request.type_filling = ORDER_FILLING_FOK; 
               request.sl = StopLoseUSD;
               request.tp = TakeProfitUSD;
               MqlTradeResult result;
                  
               myTradingControlPanel.OrderSend(request,result);
               Print(result.retcode);
                }
             }
          }
       }
   
   if(EMA100M5DataTable[0] < EMA200M5DataTable[0] && EMA200M5DataTable[0] < EMA300M5DataTable[0]) // Downtrend
      {
      if((EMA200M5DataTable[0]-EMA100M5DataTable[0])/EMA200M5DataTable[0] > 0.0025 && (EMA300M5DataTable[0]-EMA200M5DataTable[0])/EMA200M5DataTable[0] > 0.0025 && (EMA200M5DataTable[0]-EMA100M5DataTable[0])/EMA200M5DataTable[0] > (EMA300M5DataTable[0]-EMA200M5DataTable[0])/EMA200M5DataTable[0]) // No range
         {
         if(currentBid > EMA200M5DataTable[0] && currentBid < EMA200M5DataTable[0]*1.0005) // Price hit EMA200 in a downtrend
            {
            if(PositionSelect(_Symbol) == false) // Don't have an open position
               {
               StopLoseUSD = Round(EMA300M5DataTable[0]); // Stop Lose price
               StopLosePer = -(currentBid-EMA300M5DataTable[0])/currentBid; // Stop Lose %
               TakeProfitUSD = Round(EMA100M5DataTable[0]); // Take Profit price
               Size = capital*leviermax/currentBid; // Size of the position with full leverage
               SizeMax = (dailylossmax*0.475)/StopLosePer/currentBid; // Size max of the position (risk management)
               if(SizeMax <= Size)
                  {
                  SizeReel = NormalizeDouble(SizeMax,2); // Size of the position cut to 2 decimals
                  }
                  else
                  {
                  SizeReel = NormalizeDouble(Size,2); // Size of the position cut to 2 decimals
                  }
               if(safemode == true) // capital go under 9000$
                  {
                  SizeReel = NormalizeDouble(SizeReel/2,2);
                  }
               if(ultrasafemode == true) // capital go under 8500$
                  {
                  SizeReel = NormalizeDouble(SizeReel/4,2);
                  }
               MqlTradeRequest request;
               request.action = TRADE_ACTION_DEAL;
               request.symbol = _Symbol;
               request.volume = SizeReel;
               request.type = ORDER_TYPE_SELL;
               request.type_filling = ORDER_FILLING_FOK; 
               request.sl = StopLoseUSD;
               request.tp = TakeProfitUSD;
               MqlTradeResult result;
                  
               myTradingControlPanel.OrderSend(request,result);   
               Print(result.retcode);
                }
             }
          }
       }
//+------------------------------------------------------------------+

   }
  }

//+------------------------------------------------------------------+
//| Building Function                                             |
//+------------------------------------------------------------------+

double Closed_Profit_Today()
{
   MqlDateTime SDateTime;
   TimeToStruct(TimeCurrent(),SDateTime);

   SDateTime.hour=0;
   SDateTime.min=0;
   SDateTime.sec=0;
   datetime from_date=StructToTime(SDateTime);     // From date

   SDateTime.hour=23;
   SDateTime.min=59;
   SDateTime.sec=59;
   datetime to_date=StructToTime(SDateTime);     // To date
   to_date+=60*60*24;

   HistorySelect(from_date,to_date);
   int    trades_of_day=0;
   double wining_trade=0.0;
   double losing_trade=0.0;
   double total_profit=0.0;
   uint   total=HistoryDealsTotal();
   ulong  ticket=0;
//--- for all deals
   for(uint i=0; i<total; i++)
     {
      //--- try to get deals ticket
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         ///////////----
         //--- get deals properties
         trades_of_day++;
         double deal_commission=HistoryDealGetDouble(ticket,DEAL_COMMISSION);
         double deal_swap=HistoryDealGetDouble(ticket,DEAL_SWAP);
         double deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         double profit=deal_commission+deal_swap+deal_profit;
         if(profit>0.0)
            wining_trade+=profit;
         if(profit<0.0)
            losing_trade+=profit;
         total_profit+=profit;
        }
     }

return(total_profit);
}  

//+------------------------------------------------------------------+

double Round(double price)
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
   return( round( price / tick_size ) * tick_size );
}

//+------------------------------------------------------------------+
 
Your code
   if(AccountInfoDouble(ACCOUNT_BALANCE) <= 9000) // See if in a drawdown
      {
      safemode = true;
      }
   else
      {
      safemode = false;
      }
Simplified
safemode = AccountInfoDouble(ACCOUNT_BALANCE) <= 9000; // See if in a drawdown
          Increase Order after stoploss - MQL4 programming forum #1.3 (2017)
 
William Roeder #:
Your code
Simplified
          Increase Order after stoploss - MQL4 programming forum #1.3 (2017)


Thank you. I will update my code that way.


It doesn't change the error code problem, tho.

 

What a trade request needs is written here: https://www.mql5.com/en/docs/constants/structures/mqltraderequest

If your broker uses Request or Instant Execution you need to set the price value. The current Bid-value for Sells and the Ask-value for Buys.

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Benjamin Fotteler #:

What a trade request needs is written here: https://www.mql5.com/en/docs/constants/structures/mqltraderequest

If your broker uses Request or Instant Execution you need to set the price value. The current Bid-value for Sells and the Ask-value for Buys.


Yes, I read that page several times and I still not find what wrong with my request. I try to add the price in the request, doesn't change a thing. I check the value of SL/TP/Size, all correct.

I still don't see what I miss…

 
CrokCrypto #:


Yes, I read that page several times and I still not find what wrong with my request. I try to add the price in the request, doesn't change a thing. I check the value of SL/TP/Size, all correct.

I still don't see what I miss…


Change-

MqlTradeRequest request;
MqlTradeResult result;

To-

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