Search code examples
mql5

Get the minimal lot size of a given asset in MQL5


I'm tring to build a simple strategy tester for auto trading but for some assets the "Buy" function fails with the error code 10014 "TRADE_RETCODE_INVALID_VOLUME". The simplest solution for this in my opinion is to know beforehand the minimum volume of the asset accepted to trade it. My code so far is:

void OnTick()
  {
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
   MqlRates PriceInfo[];
   ArraySetAsSeries(PriceInfo, true);

   int PriceData = CopyRates(_Symbol, _Period, 0 ,3, PriceInfo);

   if(PositionsTotal() == 0)
   {
      bool result = trade.Buy(
      100, //100, 10, 1, 0.1, 0.01, 0.001... any value gives "invalid volume"
      NULL,
      ask, 
      ask-300 * _Point, // stop loss
      ask+150 * _Point, // take profit
      NULL
      );
      if(result){
         double balance = AccountInfoDouble(ACCOUNT_BALANCE);
         Comment("Preco de compra: " +  ask + " stop loss: " + (ask-300 * _Point) + " take profit: " + (ask+150 * _Point)  );
         }
      else{
         Comment("Erro ao realizar compra");

      }
   }
  } 

I just want to replace the first parameter of the buy function with the minimum VALID volume for the given symbol. Thanks in advance.


Solution

  • I've found myself the answer, to get the minimum acceptable volume for an asset:

    double minVol = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
    

    Then, I added it to the function call

      bool result = trade.Buy(
      minVol,
      NULL,
      ask,
      ask-300 * _Point, // stop loss
      ask+150 * _Point, // take profit
      NULL
      );
    

    Source: https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants