Issue with EA Verification: "Volume limit reached" Error

 

Hello,

I am experiencing an issue during the verification process for publishing my Expert Advisor (EA) on the market. The strategy tester is showing the following error message: 

test on EURUSD,H1 (netting)

2022.02.04 18:00:00 failed instant buy 3.4 EURUSD at 1.14423 [Volume limit reached]

2022.07.07 08:00:00 failed instant sell 3.4 EURUSD at 1.02019 [Volume limit reached]

strategy tester report 20 total trades

I have included the relevant Lots calculation code below.

input double MinLots = 0.02; 
input double leverage = 100; 
input double risque = 2.0;
double calculLots(ENUM_ORDER_TYPE order) {
   // Leverage check
   if (leverage <= 0) {
      Print("Error: Invalid leverage -> ", leverage);
      return SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   }
   double balance = AccountInfoDouble(ACCOUNT_BALANCE);
   double marginFree = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   double minV = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   double maxV = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   double stepV = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
   double OneLots = 100000 / leverage; // Value of one lot with leverage
   double lots = (balance * (risque / 100.0)) / OneLots;
   if (lots <= MinLots) {
      Print("Error: Invalid calculated volume -> ", lots);
      return minV;
   }
   lots = MathMax(minV, MathMin(lots, maxV));
   lots = MathRound(lots / stepV) * stepV;
   if (lots > maxV) lots = maxV;
   if (lots < minV) lots = minV;
   lots = NormalizeVolume(lots);
   Print("Good lot size");
   if (marginFree < (lots * OneLots)) {
      Print("Error: Margin too low. ", lots, " lots | Free Margin: ", marginFree);
      return minV;
   }
   Print("Margin verified successfully");
   if (!CheckVolumeValue(lots)) {
      return minV; // Fallback value
   }
   Print("Volume is good");
   if (CheckMoneyForTrade(_Symbol, lots, order)){
      Print("Lots correspond to balance");
      return lots;
   } else {
      Print("Error: Lots do not correspond to balance");
      return MinLots;
   }
}

bool CheckVolumeValue(double volume) {
   double minV = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);
   double maxV = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);
   double stepV = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP);
   if (volume <= minV || volume >= maxV) {
      Print("Volume out of bounds (%.2f - %.2f) | Minimum: ", minV, " | Maximum: ", maxV);
      return false;
   }
   if (MathAbs(MathRound(volume / stepV) * stepV - volume) > 1e-7) {
      Print("Volume must be a multiple of %.2f", stepV);
      return false;
   }
   return true;
}

bool CheckMoneyForTrade(string symb, double lots, ENUM_ORDER_TYPE type, double op = 0){
   ResetLastError();
   MqlTick mqltick;
   SymbolInfoTick(symb, mqltick);
   double price = op;
   if (price == 0){
      price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      if (type == ORDER_TYPE_SELL || type == ORDER_TYPE_SELL_STOP || type == ORDER_TYPE_SELL_LIMIT){
         price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      }
   }
   type = (ENUM_ORDER_TYPE)MathMod(type, 2);
   double margin, free_margin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   if (!OrderCalcMargin(type, symb, lots, price, margin)) {
      Print("Error in ", __FUNCTION__, " code=", GetLastError());
      return false;
   }
   if (margin > free_margin) {
      Print("Not enough money for ", EnumToString(type), " ", lots, " ", symb, " Error code=", GetLastError());
      return false;
   }
   return true;
}

double NormalizeVolume(double Lot){
   ResetLastError();
   double LotMin  = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   double LotMax  = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   double LotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
   if (GetLastError() != ERR_SUCCESS) {
      return(WRONG_VALUE);
   }
   Lot = LotMin + MathFloor((Lot - LotMin) / LotStep) * LotStep;
   Lot = NormalizeDouble(MathMin(LotMax, MathMax(LotMin, Lot)), 2);
   return(Lot);
}
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

You are not checking for the overall volume limit, namely the property ... SYMBOL_VOLUME_LIMIT

Please read the following ... Invalid volumes in trade operations

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.