OrderSend: Error code 131

 
David Diez:
I get this error on validation my product but it is running well on backtests.
131Invalid trade volume.

You should backtest the EA with a larger lot size that exceeds the margin limit, 

or the broker lot size limit. Then, of course you will get an error,
-- because the validation process performs that stage.

So, now you can set a new function to handle it automatically.

Regards.

 
David Diez:
I get this error on validation my product but it is running well on backtests.
You have to adjust your volume to SYMBOL_VOLUME_MIN, SYMBOL_VOLUME_MAX and SYMBOL_VOLUME_STEP.
 
Petr Nosek:
You have to adjust your volume to SYMBOL_VOLUME_MIN, SYMBOL_VOLUME_MAX and SYMBOL_VOLUME_STEP.
It seems to be more difficult than it appears.
 
David Diez:
It seems to be more difficult than it appears.
We discussed about this here: https://www.mql5.com/en/forum/231444#comment_6769843
 
David Diez:
It seems to be more difficult than it appears.

You can use this function to normalize volume:

double NormalizeVolume(const double volume,string symbol=NULL)
  {
   if(symbol==NULL || symbol=="") symbol=_Symbol;
   double volumeStep=SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP);
   double volumeMin=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN);
   double volumeMax=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX);
   double volumeNormalized=int(volume/volumeStep)*volumeStep;
   return(volumeNormalized<volumeMin?0.0:(volumeNormalized>volumeMax?volumeMax:volumeNormalized));
  }

You have to check if the returned value isn't zero (it means that your requested volume is less than the minimum allowed volume).

 

I am using this code for Money Management, it uses NormalizeDouble for round lots and minimum lot size (0.01):

//---------------------------------------------- Money management ---!
double LotsOptimized(){
   double LotSize=NormalizeDouble(AccountFreeMargin()*0.618*30/100000,2);
   if(LotSize<0.01){LotSize=0.01;}
   return(LotSize);
   }

So I think MQL5 system doesn't admit less than 0.1 lot, is it true?

 
Is trending of a good time 
 
Petr Nosek:

You can use this function to normalize volume:

You have to check if the returned value isn't zero (it means that your requested volume is less than the minimum allowed volume).

Where in the whole code may I add this?
 
Yohana Parmi:
131Invalid trade volume.

You should backtest the EA with a larger lot size that exceeds the margin limit, 

or the broker lot size limit. Then, of course you will get an error,
-- because the validation process performs that stage.

So, now you can set a new function to handle it automatically.

Regards.

I tried this and doesn't give that error anymore.

double LotsOptimized(){
   double LotSize=NormalizeDouble(AccountFreeMargin()*0.618*30/100000,2);
   if(LotSize>MarketInfo(Symbol(),MODE_MAXLOT)){LotSize=MarketInfo(Symbol(),MODE_MAXLOT);}
   if(LotSize<MarketInfo(Symbol(),MODE_MINLOT)){LotSize=MarketInfo(Symbol(),MODE_MINLOT);}
   return(LotSize);
   }

Now it gives a new error code 134, I tried this:

   if(AccountFreeMargin()<MarketInfo(Symbol(),MODE_MARGINREQUIRED)){return;}

And now error 131 again..............

 
David Diez:

I tried this and doesn't give that error anymore.

Now it gives a new error code 134, I tried this:

And now error 131 again..............

Passed!

"If a check shows that there are insufficient funds to perform a trade operation, it is necessary to output an error message to the log instead of calling the OrderSend() function".

Just added that error message and passed the validation. Thanks to all.