Margin Calculation ?

 
At the bottom of the MT4 terminal when the Trade Tab is selected MT4 shows the following parameters: --->  Balance: Equity: Margin: Free Margin: Margin level:  <---  My question is "What is the equation used to calculate Margin?"
 
Alan Ray Northam: At the bottom of the MT4 terminal when the Trade Tab is selected MT4 shows the following parameters: --->  Balance: Equity: Margin: Free Margin: Margin level:  <---  My question is "What is the equation used to calculate Margin?"

Free Margin = Equity – Margin (open positions)

 
I am not looking for what MT4 shows as (Free Margin:).  I am looking for the equation for what MT4 shows as (Margin:) for a single open trade.
 
Alan Ray Northam #: I am not looking for what MT4 shows as (Free Margin:).  I am looking for the equation for what MT4 shows as (Margin:) for a single open trade.

Reference: How to calculate Margin & Margin Level on MT4?

For FX currency pairs:

Required Margin = Notional value * Trading Volume/ Leverage
Example: 1 lot EUR/USD at 1:500 Leverage: 100,000 EUR * 1/ 500 = 200 EUR

For Precious Metals:

Required Margin = Volume * Contract Size * Open Price / Leverage
Example: 1 lot XAUUSD: 1 * 100 * 1,263.14 / 500 = $252.63

For Stocks/Equity CFDs:

Required Margin = Volume * Contract Size * Open Price * Margin %
Example: 1 lot AAPL: 1 * 100 * 42.96 * 0.04 = $171.84

Some other reference material:

All this found with a simple Google search.

 
Thank you Fernando.  Looks like that equation for FX market would be the margin for one lot.  If I want the margin for a partial lot, such as 0.03 lots then would I just multiply by 0.03?

margin = 100,000 * 0.03 / 50
 
Alan Ray Northam #:
Thank you Fernando.  Looks like that equation for FX market would be the margin for one lot.  If I want the margin for a partial lot, such as 0.03 lots then would I just multiply by 0.03?

margin = 100,000 * 0.03 / 50

Required Margin = Notional value * Trading Volume/ Leverage
Example: 1 lot EUR/USD at 1:500 Leverage: 100,000 EUR * 1/ 500 = 200 EUR

Example: 0.03 lot EUR/USD at 1:500 Leverage: 100,000 EUR * 0.03 / 500 = 6.00 EUR

Consider also using this instead:

MarketInfo( _Symbol, MODE_MARGINREQUIRED )

Also have a look at the following:

Forum on trading, automated trading systems and testing trading strategies

AccountFreeMarginCheck issue.

Fernando Carreiro, 2022.04.05 01:03

In MQL4, I use these:

MODE_MARGINCALCMODE

28

Margin calculation mode. 0 - Forex; 1 - CFD; 2 - Futures; 3 - CFD for indices

MODE_MARGININIT

29

Initial margin requirements for 1 lot

MODE_MARGINMAINTENANCE

30

Margin to maintain open orders calculated for 1 lot

MODE_MARGINHEDGED

31

Hedged margin calculated for 1 lot

MODE_MARGINREQUIRED

32

Free margin required to open 1 lot for buying

Forum on trading, automated trading systems and testing trading strategies

Margin requirements calculation

gordon, 2010.04.08 04:42

Margin requirement for 1 lot is given by MarketInfo(symbol,MODE_MARGINREQUIRED). Calculating this value seems to be different among some brokers (specifically ones that use a bridge).


The majority of brokers use this calculation (taken from the article 'Forex Trading ABC' -> https://www.mql5.com/en/articles/1453):

double MarginCalculate(string symbol, double volume)
  {
   string first    = StringSubstr(symbol,0,3); // the first symbol, for example,  EUR
   string second   = StringSubstr(symbol,3,3); // the second symbol, for example, USD
   string currency = AccountCurrency();        // deposit currency, for example,  USD
   double leverage = AccountLeverage();        // leverage, for example,          100
// contract size, for example, 100000
   double contract = MarketInfo(symbol, MODE_LOTSIZE);
   double bid      = MarketInfo(symbol, MODE_BID);      // Bid price
//---- allow only standard forex symbols like XXXYYY
   if(StringLen(symbol) != 6)
     {
      Print("MarginCalculate: '",symbol,"' must be standard forex symbol XXXYYY");
      return(0.0);
     }
//---- check for data availability
   if(bid <= 0 || contract <= 0) 
     {
      Print("MarginCalculate: no market information for '",symbol,"'");
      return(0.0);
     }
//---- check the simplest variations - without cross currencies
   if(first == currency)   
       return(contract*volume / leverage);           // USDxxx
   if(second == currency)  
       return(contract*bid*volume / leverage);       // xxxUSD
//---- check normal cross currencies, search for direct conversion through deposit currency
   string base = currency + first;                   // USDxxx
   if(MarketInfo(base, MODE_BID) > 0) 
       return(contract / MarketInfo(base, MODE_BID)*volume / leverage);
//---- try vice versa
   base = first + currency;                          // xxxUSD
   if(MarketInfo(base, MODE_BID) > 0) 
       return(contract*MarketInfo(base, MODE_BID)*volume / leverage);
//---- direct conversion is impossible
   Print("MarginCalculate: can not convert '",symbol,"'");
   return(0.0);
  }

But some brokers do not factor 'bid' price when calculating (regardless of symbol):

double MarginCalculate(string symbol, double volume)
  {
   double leverage = AccountLeverage(); 
   double contract = MarketInfo(symbol, MODE_LOTSIZE);
   return(contract*volume / leverage);
  }

Note that MODE_MARGINREQUIRED is 'static' in Tester and that both methods of calculation cannot be used in Tester (they rely on MarketInfo() of other symbols...).

 

Hi all, I would like to find the max size for a new position using no more than 50% of free margin.

Is this formula correct?

double available_margin = AccountFreeMargin() * 50/100 ;

double max_size = available_margin * MarketInfo(Symbol(),MODE_MINLOT) / MarketInfo(Symbol(),MODE_MARGINREQUIRED) ;

Thank you!

 
Alberto Tortella #:

Hi all, I would like to find the max size for a new position using no more than 50% of free margin.

Is this formula correct?

Thank you!

This is an integer operation

50/100 
Its outcome will be 0.

You want 0.5, to get that, do this:

50.0/100.0

I haven't checked the rest.



 

Is there a way to recall the requesed margin for every opened position?

I'm trying with Order functions but I don't find it.

 
Alberto Tortella #:

Is there a way to recall the requesed margin for every opened position?

I'm trying with Order functions but I don't find it.

https://docs.mql4.com/account/accountfreemargincheck
AccountFreeMarginCheck - Account Information - MQL4 Reference
AccountFreeMarginCheck - Account Information - MQL4 Reference
  • docs.mql4.com
AccountFreeMarginCheck - Account Information - MQL4 Reference
 
Dominik Christian Egert #:
This is an integer operation

Its outcome will be 0.

You want 0.5, to get that, do this:

Or type 0.5