margin required

 

Good day everyone.

Please how do I calculate margin required for MQL5

//I know the one for mql4
MarketInfo(Symbol(), MODE_MARGINREQUIRED)

Thanks

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoMarginRate - I can't understand it

fxsaber, 2023.07.18 22:28

double GetMarginRequired2( const string Symb )
{
  const double MinVolume = ::SymbolInfoDouble(Symb, SYMBOL_VOLUME_MIN);
  double Margin;
              
  return(::OrderCalcMargin(ORDER_TYPE_BUY, Symb, MinVolume, ::SymbolInfoDouble(Symb, SYMBOL_ASK), Margin)
          ? (MinVolume ? Margin / MinVolume : 0) : 0);  
}

double GetMarginRequired( const string Symb )
{  
  MqlTick Tick;
  double MarginInitRate, MarginMainRate, MarginInfo;

  return(((::SymbolInfoInteger(Symb, SYMBOL_TRADE_CALC_MODE) == SYMBOL_CALC_MODE_FOREX) &&
          ::SymbolInfoTick(Symb, Tick) &&
          ::SymbolInfoMarginRate(Symb, ORDER_TYPE_BUY, MarginInitRate, MarginMainRate) && MarginInitRate &&
          (bool)(MarginInfo = ::MathMax(::SymbolInfoDouble(Symb, SYMBOL_MARGIN_INITIAL), ::SymbolInfoDouble(Symb, SYMBOL_MARGIN_HEDGED))))
            ? MarginInitRate * Tick.ask * ::SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE) *
              (MarginInfo / ::SymbolInfoDouble(Symb, SYMBOL_TRADE_CONTRACT_SIZE)) /
              (::SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_SIZE) * ::AccountInfoInteger(ACCOUNT_LEVERAGE))
            : GetMarginRequired2(Symb));
}
 
fxsaber #:

Thank you so much.

I'll try it out.