MQL4 Code Help : Volume Check and Adjust

Article URL : The checks a trading robot must pass before publication in the Market - MQL5 Articles

I am getting Error 131

To resolve this, I am trying something like Volume Error Handling. It update value automatically.


   Buy_Lot_Size = MathMax(SymbolInfoDouble(buy_random_symbol, SYMBOL_VOLUME_MIN), MathMin(Buy_Lot_Size, SymbolInfoDouble(buy_random_symbol, SYMBOL_VOLUME_MAX)));
   Sell_Lot_Size = MathMax(SymbolInfoDouble(sell_random_symbol, SYMBOL_VOLUME_MIN), MathMin(Sell_Lot_Size, SymbolInfoDouble(sell_random_symbol, SYMBOL_VOLUME_MAX)));

or

   Buy_Lot_Size = MathMax(MarketInfo(buy_random_symbol, MODE_MINLOT), MathMin(Buy_Lot_Size, MarketInfo(buy_random_symbol, MODE_MAXLOT)));
   Sell_Lot_Size = MathMax(MarketInfo(sell_random_symbol, MODE_MINLOT), MathMin(Sell_Lot_Size, MarketInfo(sell_random_symbol, MODE_MAXLOT)));

Instead of

//+------------------------------------------------------------------+
//| Check the correctness of the order volume                        |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume,string &description)
  {
//--- minimal allowed volume for trade operations
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   if(volume<min_volume)
     {
      description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
      return(false);
     }

//--- maximal allowed volume of trade operations
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(volume>max_volume)
     {
      description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
      return(false);
     }

//--- get minimal step of volume changing
   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
                               volume_step,ratio*volume_step);
      return(false);
     }
   description="Correct volume value";
   return(true);
  }

But my both of the code is not working. What wrong with my code. it does exact same and even better than article URL. it also adjust the Volume size after comparison.

Have you tried ChatGPT or DeepSeek?

Sometimes they can fix this kind of thing easily, sometimes they keep giving you the same old buggy code and then say it’s your code! :smiley:

Let me know if anyone knows better AI for coding, specially MQL as I am sure there are better ones out there.

1 Like

Even MQL5 dot com is a better place to seek advice than here

In other words, find a coding forum specifically for MQL language.

The chance of finding an expert coder here is exactly ZERO.

Good Luck

Hello. Nice to meet you.
I am also a forex trader but now I am a manual trader.
In recent days I am thinking about automating my trading strategies.
I want to have a discussion with you.