The margin requirement is correct MODE_MARGINREQUIRED if lot size > 25 or 50

 

I checked that when the lot size is below 20, the margin requirement is correct. However, when the lot size increases to around 50, the margin requirement increases by an additional 9,000 USD. Why is there such a large difference, and how can I handle this in MQL4?

margin required to open 1 lot USDJPY for buying=200.0 --> 50 lot ~ 10000 but image 19000 usd.


//+------------------------------------------------------------------+
//|                                                        test2.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

void OnTick()
  {
   if(!isNewbar())
      return;
   if(OrdersTotal()>0)
      return;

   string sym="USDJPY";
   double totalVolume = calculateTotalVolume(0,AccountLeverage(),sym,AccountEquity());
   Print("AccountLeverage() ",AccountEquity());
   double bid=SymbolInfoDouble(sym,SYMBOL_BID);
   int ticket=OrderSend(sym,OP_SELL,50,bid,20,bid+1000*Point,bid-1000*Point);

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double calculateTotalVolume(double stdMarginLevel, double standardLeverage, string symbol, double accountEquity)
  {
   double marginRatio = standardLeverage/100.0*200.0;
   Print("marginRatio ",marginRatio);
   double FreeMargin = accountEquity/(marginRatio/100.0) - AccountMargin();  // Margin Ratio (%)= Used Margin/Equity×100
   Print("FreeMargin ",FreeMargin);
   double marginPerLot = MarketInfo(symbol, MODE_MARGINREQUIRED);
   Print("marginPerLot ",marginPerLot);

   double totalVolume = NormalizeDouble(FreeMargin,2)/NormalizeDouble(marginPerLot,2);
   return getStandardVolume(symbol,totalVolume);
  }
//+------------------------------------------------------------------+
double getStandardVolume(string symbol, double volume)
  {
   double minLot = MarketInfo(symbol, MODE_MINLOT);
   double maxLot = MarketInfo(symbol, MODE_MAXLOT);
   double res = MathRound(volume/minLot) * minLot;
   res = MathMax(res, minLot);
   res = MathMin(res, maxLot);
   return res;
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   Print("Free margin required to open 1 lot USDJPY for buying=",MarketInfo("USDJPY",MODE_MARGINREQUIRED));
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool isNewbar()
  {
   static long lastBarCount = Bars(Symbol(), _Period);
   long currentBarCount =  Bars(Symbol(), _Period);
   if(lastBarCount != currentBarCount)
     {
      lastBarCount = currentBarCount;
      return true;
     }
   return false;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.11.08
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions