在 MQL5 中获取给定资产的最小手数

Get the minimal lot size of a given asset in MQL5

我正在尝试为自动交易构建一个简单的策略测试器,但对于某些资产,"Buy" 函数失败,错误代码为 10014 "TRADE_RETCODE_INVALID_VOLUME" .在我看来,最简单的解决方案是事先知道接受交易的资产的最小交易量。到目前为止我的代码是:

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");

      }
   }
  } 

我只想用给定交易品种的最小有效交易量替换购买函数的第一个参数。提前致谢。

我找到了答案,获得资产的最小可接受量:

double minVol = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);

然后,我将它添加到函数调用中

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

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