New MT4 validation error, NO TRADING OPERATIONS

 

Hello everyone, I can't validate this very simple ea in mt4, the error is in the attached .jpg file. Can anyone help me? the code


//************************************************************************************************/
//*                                                                                   test.mq4   */
//************************************************************************************************/
#property copyright   ""
#property link        ""
#property version     ""
#property description " "
#property strict
enum ENUM_ST
  {
   Awerage   = 0, // Awerage
   PartClose = 1  // Part Close
  };
//************************************************************************************************/
//*                                                                                              */
//************************************************************************************************/
input int          iTakeProfit         = 300;      // Take Profit (in pips)
input double       iStartLots          = 0.01;     // Start lot
input double       iMaximalLots        = 0.01;     // Maximal Lots
input ENUM_ST      iCloseOrder         = Awerage;  // Type close orders
input int          iPointOrderStep     = 50;       // Point order step (in pips)
input int          iMinimalProfit      = 70;       // Minimal profit for close grid (in pips)
input int          iMagicNumber        = 227;      // Magic Number (in number)
input int          iSlippage           = 30;       // Slippage (in pips)
//---
//************************************************************************************************/
//*                                                                                              */
//************************************************************************************************/
int OnInit()
  {
   Comment("");
   return(INIT_SUCCEEDED);
  }
//************************************************************************************************/
//*                                                                                              */
//************************************************************************************************/
void OnTick()
  {
   double
   BuyPriceMax = 0, BuyPriceMin = 0, BuyPriceMaxLot = 0, BuyPriceMinLot = 0,
   SelPriceMin = 0, SelPriceMax = 0, SelPriceMinLot = 0, SelPriceMaxLot = 0;

   int
   BuyPriceMaxTic = 0, BuyPriceMinTic = 0, SelPriceMaxTic = 0, SelPriceMinTic = 0;

   double
   op = 0, lt = 0, tp = 0;

   int
   tk = 0, b = 0, s = 0;

   for(int i = OrdersTotal() - 1; i >= 0; i--)
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber() == iMagicNumber)
            if(OrderSymbol() == Symbol())
              {
               op = NormalizeDouble(OrderOpenPrice(), Digits());
               lt = NormalizeDouble(OrderLots(), 2);
               tk = OrderTicket();
               if(OrderType() == OP_BUY)
                 {
                  b++;
                  if(op > BuyPriceMax || BuyPriceMax == 0)
                    {
                     BuyPriceMax    = op;
                     BuyPriceMaxLot = lt;
                     BuyPriceMaxTic = tk;
                    }
                  if(op < BuyPriceMin || BuyPriceMin == 0)
                    {
                     BuyPriceMin    = op;
                     BuyPriceMinLot = lt;
                     BuyPriceMinTic = tk;
                    }
                 }
               // ===
               if(OrderType() == OP_SELL)
                 {
                  s++;
                  if(op > SelPriceMax || SelPriceMax == 0)
                    {
                     SelPriceMax    = op;
                     SelPriceMaxLot = lt;
                     SelPriceMaxTic = tk;
                    }
                  if(op < SelPriceMin || SelPriceMin == 0)
                    {
                     SelPriceMin    = op;
                     SelPriceMinLot = lt;
                     SelPriceMinTic = tk;
                    }
                 }
              }
//*************************************************************//
   double   AwerageBuyPrice = 0, AwerageSelPrice = 0;

   if(iCloseOrder == Awerage)
     {
      if(b >= 2)
         AwerageBuyPrice = NormalizeDouble((BuyPriceMax * BuyPriceMaxLot + BuyPriceMin * BuyPriceMinLot) / (BuyPriceMaxLot + BuyPriceMinLot) + iMinimalProfit * Point(), Digits());
      if(s >= 2)
         AwerageSelPrice = NormalizeDouble((SelPriceMax * SelPriceMaxLot + SelPriceMin * SelPriceMinLot) / (SelPriceMaxLot + SelPriceMinLot) - iMinimalProfit * Point(), Digits());
     }

   if(iCloseOrder == PartClose)
     {
      if(b >= 2)
         AwerageBuyPrice = NormalizeDouble((BuyPriceMax * iStartLots + BuyPriceMin * BuyPriceMinLot) / (iStartLots + BuyPriceMinLot) + iMinimalProfit * Point(), Digits());
      if(s >= 2)
         AwerageSelPrice = NormalizeDouble((SelPriceMax * SelPriceMaxLot + SelPriceMin * iStartLots) / (SelPriceMaxLot + iStartLots) - iMinimalProfit * Point(), Digits());
     }
//*************************************************************//
   double BuyLot = 0, SelLot = 0;
   if(BuyPriceMinLot == 0)
      BuyLot = iStartLots;
   else
      BuyLot = BuyPriceMinLot * 2;
   if(SelPriceMaxLot == 0)
      SelLot = iStartLots;
   else
      SelLot = SelPriceMaxLot * 2;
//*************************************************************//
   if(iMaximalLots >0)
     {
      if(BuyLot > iMaximalLots)
         BuyLot = iMaximalLots;
      if(SelLot > iMaximalLots)
         SelLot = iMaximalLots;
     }
   if(!CheckVolumeValue(BuyLot) || !CheckVolumeValue(SelLot))
      return;
//*************************************************************//
   if(Close[1] > Open[1])
      if((b == 0) || (b > 0 && (BuyPriceMin - Ask) > (iPointOrderStep * Point())))
         if(OrderSend(Symbol(), OP_BUY, NormalizeDouble(BuyLot, 2), NormalizeDouble(Ask, Digits()), iSlippage, 0, 0, "Test Buy", iMagicNumber, 0, clrGreen) < 0)
            Print("OrderSend error #", GetLastError());

   if(Close[1] < Open[1])
      if((s == 0) || (s > 0 && (Bid - SelPriceMax) > (iPointOrderStep * Point())))
         if(OrderSend(Symbol(), OP_SELL, NormalizeDouble(SelLot, 2), NormalizeDouble(Bid, Digits()), iSlippage, 0, 0, "Test Sell", iMagicNumber, 0, clrGreen) < 0)
            Print("OrderSend error #", GetLastError());
//*************************************************************//
   for(int i = OrdersTotal() - 1; i >= 0; i--)
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber() == iMagicNumber)
            if(OrderSymbol() == Symbol())
              {
               op = NormalizeDouble(OrderOpenPrice(), Digits());
               tp = NormalizeDouble(OrderTakeProfit(), Digits());
               lt = NormalizeDouble(OrderLots(), 2);
               tk = OrderTicket();

               if(OrderType() == OP_BUY && b == 1 && tp == 0)
                  if(!OrderModify(tk, op, OrderStopLoss(), NormalizeDouble(Ask + iTakeProfit * Point(), Digits()), 0, clrRed))
                     Print("OrderModify error #", GetLastError());

               if(OrderType() == OP_SELL && s == 1 && tp == 0)
                  if(!OrderModify(tk, op, OrderStopLoss(), NormalizeDouble(Bid - iTakeProfit * Point(), Digits()), 0, clrRed))
                     Print("OrderModify error #", GetLastError());

               if(iCloseOrder == Awerage)
                 {
                  if(OrderType() == OP_BUY && b >= 2)
                    {
                     if(tk == BuyPriceMaxTic || tk == BuyPriceMinTic)
                        if(Bid < AwerageBuyPrice && tp != AwerageBuyPrice)
                           if(!OrderModify(tk, op, OrderStopLoss(), AwerageBuyPrice, 0, clrRed))
                              Print("OrderModify error #", GetLastError());

                     if(tk != BuyPriceMaxTic && tk != BuyPriceMinTic && tp != 0)
                        if(!OrderModify(tk, op, 0, 0, 0, clrRed))
                           Print("OrderModify error #", GetLastError());
                    }
                  if(OrderType() == OP_SELL && s >= 2)
                    {
                     if(tk == SelPriceMaxTic || tk == SelPriceMinTic)
                        if(Ask > AwerageSelPrice && tp != AwerageSelPrice)
                           if(!OrderModify(tk, op, OrderStopLoss(), AwerageSelPrice, 0, clrRed))
                              Print("OrderModify error #", GetLastError());

                     if(tk != SelPriceMaxTic && tk != SelPriceMinTic && tp != 0)
                        if(!OrderModify(tk, op, 0, 0, 0, clrRed))
                           Print("OrderModify error #", GetLastError());
                    }
                 }

              }

   if(iCloseOrder == PartClose)
     {
      if(b >= 2)
         if(AwerageBuyPrice > 0 && Bid >= AwerageBuyPrice)
           {
            if(!OrderClose(BuyPriceMaxTic, iStartLots,Bid, iSlippage, clrRed))
               Print("OrderClose Error", GetLastError());
            if(!OrderClose(BuyPriceMinTic, BuyPriceMinLot,Bid, iSlippage, clrRed))
               Print("OrderClose Error #", GetLastError());
           }
      if(s >= 2)
         if(AwerageSelPrice > 0 && Ask <= AwerageSelPrice)
           {
            if(!OrderClose(SelPriceMinTic, iStartLots,Ask, iSlippage, clrRed))
               Print("OrderClose Error #", GetLastError());
            if(!OrderClose(SelPriceMaxTic, SelPriceMaxLot,Bid, iSlippage, clrRed))
               Print("OrderClose Error #", GetLastError());
           }
     }
  }
//************************************************************************************************/
//*                                                                                              */
//************************************************************************************************/
void OnDeinit(const int reason)
  {

  }
//************************************************************************************************/
//*                                                                                              */
//************************************************************************************************/
bool CheckVolumeValue(double volume)
  {

   double min_volume = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);
   if(volume < min_volume)
      return(false);


   double max_volume = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);
   if(volume > max_volume)
      return(false);


   double volume_step = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP);

   int ratio = (int)MathRound(volume / volume_step);
   if(MathAbs(ratio * volume_step - volume) > 0.0000001)
      return(false);

   return(true);
  }
//************************************************************************************************/
//*                                                                                              */
//************************************************************************************************/
Files:
Immagine.jpg  55 kb
 
if I set 0.20 as start lots it is validated: D what can this depend on? What should I change? 0.01 no 0.20 yes
 
fxduep2020:
if I set 0.20 as start lots it is validated: D what can this depend on? What should I change? 0.01 no 0.20 yes

Your volume check is okay .Lot digits ,margin checks ?

 
Lorentzos Roussos:

Your volume check is okay .Lot digits ,margin checks ?

yes I tried that too, done thousands of tests, with start lot 0.01 it does not pass with 0.20 yes, as if the initial volume is too low to open orders !!
 
void OnTick(){
   double
   BuyPriceMax = 0, BuyPriceMin = 0, BuyPriceMaxLot = 0, BuyPriceMinLot = 0,
   SelPriceMin = 0, SelPriceMax = 0, SelPriceMinLot = 0, SelPriceMaxLot = 0;
⋮ // No open orders
(BuyPriceMax * BuyPriceMaxLot + BuyPriceMin * BuyPriceMinLot) 
/ (BuyPriceMaxLot + BuyPriceMinLot) + iMinimalProfit * Point(), Digits());
If you have no open orders, you divide by zero.
 
fxduep2020:
yes I tried that too, done thousands of tests, with start lot 0.01 it does not pass with 0.20 yes, as if the initial volume is too low to open orders !!

so with one lot digit it passes ,with 2 lot digits it does not pass ?
if you normalize the lot with the lot digits you find in code ?(meaning,dont assume 2 lot digits?)

 
William Roeder:
If you have no open orders, you divide by zero.
do i have to change something? if yes you can indicate me precisely I do not understand where to insert the code you posted
 
Lorentzos Roussos:

so with one lot digit it passes ,with 2 lot digits it does not pass ?
if you normalize the lot with the lot digits you find in code ?(meaning,dont assume 2 lot digits?)

0.01 no, 0.15 no, 0.20 passed :D
 
fxduep2020:
0.01 no, 0.15 no, 0.20 passed :D

hmm .

perhaps try 

NormalizeDouble(BuyLot, 1)

etc in lots , -to see if it passes- but dont use it in final product ,this will indicate you need to find the brokers lot digits (if they all pass).

 
Lorentzos Roussos:

hmm .

perhaps try 

etc in lots , -to see if it passes- but dont use it in final product ,this will indicate you need to find the brokers lot digits (if they all pass).

done now this test, nothing, only passed with 0.20, ps. not even with 0.02
 
//wherever you send orders ,change the normalization in the order send to variable get_lot
//get lot should be before order send like this 
double get_lot=lot_me(BuyLot);//or SellLot accordingly
//then perform one more check for sending the order ,if get_lot>0

double lot_me(double lot)
{
double send=-1;
   int errors=0;
   ResetLastError();
   double minlot=(double)MarketInfo(Symbol(),MODE_MINLOT);errors+=GetLastError();
   if(errors==0)
   {
   int digits_lot=LotDigits(minlot);
   send=NormalizeDouble(lot,digits_lot);
   if(send<minlot){send=minlot;send=NormalizeDouble(send,digits_lot);}
   }
return(send);
}
//Find Lot Digits 
int LotDigits(double lot)
{
int returnio=0;
double digitos=0;
double transfer=lot;
while(transfer<1)
{
digitos++;
transfer=transfer*10;
} 
returnio=(int)digitos;
//Print("Lot ("+lot+") Digits "+digitos+" Returnio "+returnio);
return(returnio);
}

try these 

example : 

double get_lot=lot_me(BuyLot);
if(get_lot>0)
{
int ticket=OrderSend(Symbol(), OP_BUY, get_lot, NormalizeDouble(Ask, Digits()), iSlippage, 0, 0, "Test Buy", iMagicNumber, 0, clrGreen)
}