Margin required for opening a position

 

A question to the gurus of MQL4.


MarketInfo(Symbol(),MODE_MARGINREQUIRED) returns the margin required to open one lot.

Is the realtion between lots and margin required linear? In other words, if I want to open 0.1lots shall I have freemargin > MarketInfo(Symbol(),MODE_MARGINREQUIRED) * 0.1?

...same for volumes > of 1 lot? ...say to open 10lots I need freemargin > MarketInfo(Symbol(),MODE_MARGINREQUIRED) * 10 ???


Is there any other way to check which is the max size in lots I can open with the current freemargin?


Thanks a lot for replies.



Zyp

 

The relation between lots and margin is linear and is a fixed ratio, based on your type of account, i.e. it's leverage, so simple arithmetic would tell you the maximum number of lots you can open.


However, as the open position moves in the negative direction, that would also eat into the margin, so you need to allow for that too.

 
blogzr3:

The relation between lots and margin is linear and is a fixed ratio, based on your type of account, i.e. it's leverage, so simple arithmetic would tell you the maximum number of lots you can open.


However, as the open position moves in the negative direction, that would also eat into the margin, so you need to allow for that too.


...uhmmm... not quite clear.



MarketInfo(Symbol(),MODE_MARGINREQUIRED) returns the margin required to open 1.00 lot... and I expect it takes account of everything already.

Let's make an example to clarify what I aim to:

- balance 10 000 EUR, no order open yet.

- symbol = USDCHF

- running MarketInfo(Symbol(),MODE_MARGINREQUIRED) funcion returns ---> 718.29 ...which means I must have at least 718.29 EUR of free margin to open 1.00 lot position.


Now, if I want to know how many lots I can buy/sell with my 10 000 EUR I do the following math:

10 000 / 718.29 = 13.92


What I expect is that 13.92 is the maximum size of an order that the system will accept from my account without raising an error message. Is that correct?




 
You may need to include
AccountStopoutLevel( )

in your calculations.

 
- symbol = USDCHF

- running MarketInfo(Symbol(),MODE_MARGINREQUIRED) funcion returns ---> 718.29 ...which means I must have at least 718.29 EUR of free margin to open 1.00 lot position.


Now, if I want to know how many lots I can buy/sell with my 10 000 EUR I do the following math:

10 000 / 718.29 = 13.92


What I expect is that 13.92 is the maximum size of an order that the system will accept from my account without raising an error message. Is that correct?

Sorry, my previous answer was based on the account currency being USD, which is not your case. In your case, the value returned by the MarketInfo function will vary according to the exchange rate.


Theoritically, 13.92 is the maximum lot size you can open. Practically, you may need to account for AccountStopOutLevel as phy mentioned, which may be different from broker to broker.

 
double MarginCalculate(string symbol, double volume){
   double leverage = AccountLeverage(); 
   double contract = MarketInfo(symbol, MODE_LOTSIZE);
   return(contract*volume / leverage);
}
 
Zypkin: Is there any other way to check which is the max size in lots I can open with the current freemargin
  1. AccountFreeMarginCheck - Account Information - MQL4 Reference
  2. But FM for opening is not enough. See last item.
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
    • Do NOT use TickValue by itself - DeltaPerlot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
 
sysmaya:


double Margin=MarketInfo(Symbol(),MODE_MARGINREQUIRED);
double FreeMargin=AccountFreeMargin();
double howLots=NormalizeDouble(FreeMargin/Margin,2);

Print ("howLots=",DoubleToStr(howLots,2));
 
   // input int Symbol_Leverage - manually, because AccountLeverage() is not always the same as leverage for an instrument
   double LEVERAGE = Symbol_Leverage; 
   // input string Account_Currency_Symbol - ie. "EURPLN", because AccountCurrency() might be other than USD
   // you also need to know in which currency the instrument is quoted
   double AccCurAsk = MarketInfo(Account_Currency_Symbol,MODE_ASK);
   if(Account_Currency_Symbol=="")
   AccCurAsk=1;
   // Calculate required margin for basic lotsize (1Lot usually)
   double MARGIN = Bid*MarketInfo(Symbol(), MODE_LOTSIZE)/LEVERAGE*AccCurAsk;
   // Calculate required margin for your specific lotsize, ie. double MY_LOT = 0.2;
   double MY_MARGIN = MARGIN*MY_LOT;
Reason: