Errors Expert Advisor

 

Hello everyone!

                                I need help, I have made an Expert Advisor, and in my opinion it should not have problems but when I go through Backtesting, it gives me some errors that I am not aware of and that I cannot find in Metacuotes to solve it either. Could someone help me and tell me where I have made the mistake of the code?

errors

//+------------------------------------------------------------------+
//|                                          EA_MultiZigZag_V2.3.mq4 |
//|                                          © ® Code Nr.: 202211107 |
//+------------------------------------------------------------------+
#property description   "Robot utilizando MultiZigZag_V4."
#property version       " "
#property strict

// === Variables globales ===.
extern double  MagicNumber     = 202211107;                 // MagicNumber .:
extern double  TakeProfit      = 0;                      // Take Profit (0 = sin Take Profit) .:
extern double  StopLoss        = 10;                     // Stop Loss .:
extern int     Ratio           = 44;                     // Ratio .:
extern double  Porcentaje      = 0.25;                    // Porcentaje(%) tamaño operación/balance .:
extern double  Lotes           = 0.05;                   // Lotaje en pips .:
extern double  LotesMin        = 0.03;                   // Lotaje mínimo .:
extern string  Comentario      = "EA_MultiZigZag_V2.3_"; // Comentario operación abierta.:
extern int     vela            = 2;                 // Número de vala anterior .:


// === Variables globales ===
double   pips,SL,TP,PriceHigh1,PriceLow1,PriceHigh2,PriceLow2,PriceHigh3,
         PriceLow3,ticksize,HighSL,LowSL,Gastos,PipsGastos,SLTemp;
int bars,ticket = 0;

bool OpenBuy,W,OpenSell = false;
datetime TimeDn, TimeUp;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Lotaje();
   ResetLastError();
   PriceLow1 = iCustom(NULL, PERIOD_CURRENT, "..\\Indicators\\MultiZigZag_V4", "12,12,12", "5,5,5", "8,8,8", "225000,45000,1000", "1,5,60", 3, 0, vela);
   PriceHigh1 = iCustom(NULL, PERIOD_CURRENT, "..\\Indicators\\MultiZigZag_V4", "12,12,12", "5,5,5", "8,8,8", "225000,45000,1000", "1,5,60", 3, 0, vela);
   PriceLow2 = iCustom(NULL, PERIOD_CURRENT, "..\\Indicators\\MultiZigZag_V4", "12,12,12", "5,5,5", "8,8,8", "225000,45000,1000", "1,5,60", 3, 0, vela);
   PriceHigh2 = iCustom(NULL, PERIOD_CURRENT, "..\\Indicators\\MultiZigZag_V4", "12,12,12", "5,5,5", "8,8,8", "225000,45000,1000", "1,5,60", 3, 0, vela);
   Print(GetLastError());
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

//PriceLow1 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",0,vela);
//PriceHigh1 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",1,vela);
//PriceLow2 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",2,vela);
//PriceHigh2 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",3,vela);
//---
   for(int i=0; i>=10; i++)
     {
      long TicketEnUso = Ticket();
      if(TicketEnUso > 0)
        {
         if(OrderSelect(TicketEnUso,SELECT_BY_TICKET,MODE_TRADES))
           {
            if(OrderType() == OP_BUY)     // Apertura de operación de Compra o Alcista.
              {
               if(PriceHigh1 > 0 && PriceHigh2 > 0)
                  CloseTrade(TicketEnUso);
              }
            if(OrderType() == OP_SELL)    // Apertura de operación de Venta o Bajista.
              {
               if(PriceLow1 > 0 && PriceLow2> 0)
                  CloseTrade(TicketEnUso);
              }
           }
        }
      if(TicketEnUso < 1)
        {
         if(PriceLow1 > 0 && PriceLow2)
            OpenTrade(OP_BUY);
         if(PriceHigh1 > 0 && PriceHigh2 > 0)
            OpenTrade(OP_SELL);
        }

      //PriceLow1 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",0,i);
      //PriceHigh1 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",1,i);
      //PriceLow2 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",2,i);
      //PriceHigh2 = iCustom(NULL,0,"..\\Indicators\\MultiZigZag_V4.mq4",3,i);
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//===================================================================//
//+------------------------------------------------------------------+
//|                       FUNCIONES PROPIAS                          |
//+------------------------------------------------------------------+
//===================================================================//
//+------------------------------------------------------------------+
//|  Inicio Función "OpenTrade"                                      |
//+------------------------------------------------------------------+
//---
void  OpenTrade(int Type)
  {
//---

   long TicketEnUso = Ticket();
   string Tipo;
   Lotes = Lotaje();

   if(Lotes < LotesMin)
      Lotes = LotesMin;

   if(AccountFreeMargin()<(1000*Lotes))         //--- Filtro apertura por falta de dinero en Margin.
     {
      Print("No hay suficiente dinero disponible en Free Margin");
      return;
     }
//---
   if(Digits == 5 || Digits == 3)
     {
      pips = Point*10;
     }
   else
     {
      pips = Point;
     }

//---
   double SLMin = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if(StopLoss <= SLMin)
      StopLoss = SLMin + 1;

   if(Ratio == 0)
      TakeProfit = TakeProfit;
   else
      TakeProfit = StopLoss * Ratio;

   if(TakeProfit == 0)
      TakeProfit = TakeProfit;
   else
     {
      if(Ratio == 0)
         TakeProfit = TakeProfit;
      else
         TakeProfit = StopLoss * Ratio;
     }
//---

   double SL = NormalizeDouble(StopLoss*Point,Digits);
   double TP = NormalizeDouble(TakeProfit*Point,Digits);

   double SLBuy = NormalizeDouble(OrderOpenPrice()- pips,Digits);
   double SLSell = NormalizeDouble(OrderOpenPrice()+ pips,Digits);

   double TPBuy=NormalizeDouble(OrderOpenPrice()+ pips,Digits);
   double TPSell=NormalizeDouble(OrderOpenPrice()- pips,Digits);

   SLBuy = NormalizeDouble(Bid - SL,Digits);
   SLSell = NormalizeDouble(Ask + SL,Digits);

   TPBuy = NormalizeDouble(Ask + TP,Digits);
   TPSell = NormalizeDouble(Bid - TP,Digits);

//--- selección del precio por tipo de operación.
   double Precio = 0;

   if(Type == OP_BUY)
     {
      Precio = Ask;
      Tipo = "_BUY";
      SL = SLBuy;
     }
   else
     {
      Precio = Bid;
      Tipo = "_SELL";
      SL = SLSell;
     }

   ResetLastError();
   if(OrderSend(NULL,Type,Lotes,Precio,10,SL,0,Comentario + _Symbol + Tipo,MagicNumber,0,clrNONE) < 1)
      Print(GetLastError());
  }
//+------------------------------------------------------------------+
//===================================================================//
//+------------------------------------------------------------------+
//|  Inicio Función "CloseTrade"                                      |
//+------------------------------------------------------------------+
//---
void  CloseTrade(long NoTicket)
  {
   double Precio = 0;
   if(OrderSelect(NoTicket,SELECT_BY_TICKET,MODE_TRADES))
     {
      if(OrderType() == OP_BUY)
         Precio = Bid;
      else
         Precio = Ask;
      ResetLastError();
      if(OrderClose(NoTicket,Lotes,Precio,10,clrNONE));
      Print(GetLastError());
     }
  }
//+------------------------------------------------------------------+
//===================================================================//
//+------------------------------------------------------------------+
//|  Inicio Función "Ticket"                                         |
//+------------------------------------------------------------------+
//---
long Ticket()
  {
//---
   for(int Oper = 0; Oper < OrdersTotal(); Oper++)
     {
      if(OrderSelect(Oper,SELECT_BY_POS, MODE_TRADES))
        {
         if(MagicNumber == OrderMagicNumber())
            return(OrderTicket());
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
//===================================================================//
//+------------------------------------------------------------------+
//|  Inicio Función "Lotaje"                                         |
//+------------------------------------------------------------------+
//---
double Lotaje()
  {
//---
   if(Porcentaje > 0.05)
     {
      Porcentaje = 0.05;
     }

   double  Balance = NormalizeDouble(AccountBalance(),0);
   double  Lotaje = ((Balance * Porcentaje) /10) / (StopLoss*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE))/100;
   Lotaje = NormalizeDouble(Lotaje,2);

   return(Lotaje);
  }
//+------------------------------------------------------------------+
//===================================================================//
//+------------------------------------------------------------------+




I attach the code and a photo of the errors it gives me.

 
Manuel De Los Heros Soler: I attach the code and a photo of the errors it gives me.

The code has nothing to do with your tester errors. You have not downloaded history for all lower timeframes.

Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
          General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

 
William Roeder #:

The code has nothing to do with your tester errors. You have not downloaded history for all lower timeframes.

Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
          General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

Hi Williams!
                         I have updated the History of the pair and when performing the backtesting no operation has been opened for me and this is the backtestig report, why does it not open operations for me? Where is the error?
Many thanks in advance.

error_2

 
   if(OrderSend(NULL,Type,Lotes,Precio,10,SL,0,Comentario + _Symbol + Tipo,MagicNumber,0,clrNONE) < 1)

Be careful with NULL.

  1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
  2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
  3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
  4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
  5. Cloud Protector Bug? - MQL4 programming forum (2020)
 
William Roeder #:

Be careful with NULL.

  1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
  2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
  3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
  4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
  5. Cloud Protector Bug? - MQL4 programming forum (2020)
Thank you very much for the information, the application of the _Symbol, and the NULL and it is gratifying that people like you clarify them since MQL5's documentation aids are very diffuse and often lead to error, even more so when the compiler gives you the OK and Then it doesn't work, I think the same thing happens to many people as me, even more so if you're not an expert.

Thanks a lot.
All the best.
 
int OnInit(){
   PriceLow1 = iCustom(NULL, PERIOD_CURRENT, "..\\Indicators\\MultiZigZag_V4", "12,12,12", "5,5,5", "8,8,8", "225000,45000,1000", "1,5,60", 3, 0, vela);
don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.