How do you calculate the margin? - page 4

 
Janis Ozols:

If the leverage for an instrument changed due to an increase in total lot or some other reason, it would be logical to expect that these queries should return an increased amount of margin, no?

No, these functions use account leverage.

 
Andrey Khatimlianskii:

Yes, it is.

Only, most likely, the leverage changes not for an individual trade, but for the instrument as a whole, but it doesn't change the essence.

I'll add an alert to my informer for this case...

Wise, it's wise.

 
Andrey Khatimlianskii:

No, these functions use the leverage of the account.

Please tell me which function can be used to calculate the current margin for a particular instrument if the leverage of that instrument is not the same as the account's leverage at the moment?

 
Janis Ozols:

Please tell me, which function can be used to get the current margin for a particular instrument, if the leverage of that instrument at the moment is different from the leverage of the account?

It would be a long time ago
 
Janis Ozols:

Please tell me, which function can be used to get the actual margin for a certain instrument, if the leverage of this instrument at the moment is different from the leverage of the account?

Maybe SymbolInfoDouble(Symbol,SYMBOL_MARGIN_INITIAL/SYMBOL_MARGIN_MAINTENANCE)?

SymbolInfoDouble - Market Info - MQL4 Reference
SymbolInfoDouble - Market Info - MQL4 Reference
  • docs.mql4.com
2. Returns true or false depending on whether a function is successfully performed. In case of success, the value of the property is placed into a recipient variable, passed by reference by the last parameter. It is recommended to use SymbolInfoTick() if the function is used for getting information about the last tick. It may well be that not a...
 
Stanislav Korotky:

Maybe SymbolInfoDouble(Symbol,SYMBOL_MARGIN_INITIAL/SYMBOL_MARGIN_MAINTENANCE)?

Unfortunately, the SymbolInfoDouble function with any of these constants returns zero when dealing with FOREX market instruments.

 

So, now we can answer the question of the thread

Formula for calculating the margin for a direct quote

M=CC/CP

  • Where M - margin,
  • CC - amount of transaction,
  • MP - leverage.

For example, to make a deal with the volume of 1 standard lot (100 000 units) on the currency pair USD/CAD, with a leverage of 1:200, you need to make a margin

  • M=CC/CP
  • М=100 000/200
  • М=500

That is, the amount of margin will be $500. And with a leverage of 1:500, the margin will only be 100,000/500=200 $. But with a leverage of 1:500 you will be able to buy 10 lots of 1 million dollars, and carry out transactions at a margin of 100 000/500=2 000 $.

Margin calculation for inverse quotation

M=MSC/KP x KP

  • Where M - margin;
  • CC - amount of transaction;
  • CC - leverage;
  • VC - currency rate.

Let's take GBR/USD as an example.

  • M = MOP / KP x TOR
  • M = 100,000/200 x 1, 5074
  • М= 753,7 $

You don't get round sums, but the value of one pip will always be in round numbers. And for direct quotes, there is some inconvenience in calculating a pip.

Calculation of margin for cross rates

M = CC/KP x EF

  • M - margin;
  • CC - sum of transaction;
  • CC - leverage;
  • EF - exchange rate of the (base) currency to the dollar.

example:


//+-------------- РАСЧЕТ МАРЖИ ---------------------+
double calcMGN(string SYMB, double VOL)
{
   double Res=0,Price=0;
   if(LEVERAGE!=0)
   {
      if(StringSubstr(SYMB,0,3)!="USD")
      {
         SYMB=StringSubstr(SYMB,0,3)+"USD";
         Price=iClose(SYMB,Period(),0);
         Res=VOL*Price/LEVERAGE;
      }
      else Res=VOL/LEVERAGE;
   }
   return(Res);
}


 
Janis Ozols:

Please tell me, which function can be used to get the current margin for a particular instrument, if the leverage of that instrument at the moment is different from the leverage of the account?

You may find the formulas. But why?

Take the actual margin on all positions in the instrument and calculate what the actual leverage is.

 
Andrey Khatimlianskii:

Formulas can be found. But why?

Take the actual margin on all positions on the instrument and calculate what the actual leverage is.

It won't work.

Leverage is floating.

I've had it like he describes,

it's really unfortunate at quite a high risk.

 
Renat Akhtyamov:

So, now we can answer the branch's question

I'm sorry, I didn't phrase my question correctly in the title of the thread. In fact, I am not interested in how to calculate the margin, but how to get its actual (not calculated) value from the terminal. And not for the entire account, but for a single position separately. That's what we are talking about in the first post. I already understood that it cannot be done by means of MQL4, such a function simply does not exist. However, @Andrey Khatimlianskii suggested some time ago that leverage may vary not for a single position, but for an instrument as a whole. Now it is important for me to find out how to get the value of this leverage in case it differs from the account leverage.


Renat Akhtyamov:

The formula for calculating margin for a direct quote

M=CC/CP

  • Where M - margin,
  • CC - the sum of transaction,
  • KP - leverage.

The problem with this and all subsequent formulas is that they contain KP (leverage). Unfortunately, I haven't yet figured out how to get this value for a single instrument. Functions AccountMargin() and AccountInfoInteger(ACCOUNT_LEVERAGE) return the account leverage, not the symbol leverage, and function MarketInfo(_Symbol,MODE_MARGINREQUIRED) returns the margin value corresponding to the account leverage, even if the positions for this symbol are actually opened with an absolutely different leverage. The SymbolInfoDouble function with constants SYMBOL_MARGIN_INITIAL or SYMBOL_MARGIN_MAINTENANCE always returns zero when working with FOREX symbols.

Thus, I do not understand how this and all other formulas can be used, if the leverage of a broker, which differs from the KP of the account in general, is set for a specific instrument.


Andrey Khatimlianskii:

The formulas can be found. But why?

I'm not trying to find formulas (they have already been proposed enough), but exactly the function of MQL4. In order to get the actual value, not the calculated one. Thanks to Andrey, I've already understood that it's impossible to do this for a single position separately. Now I'm wondering how to get the leverage for the instrument as a whole, if it is different from that of the account.


Andrey Khatimlianskii:

Take the actual margin for all positions in the instrument and calculate what the actual leverage turns out to be.

This is exactly what I did now. The Expert Advisor compares the actual value of margin with the calculated value and in case of a significant excess, it stops trading and sends a notification to the trader. So far I have not received any such notifications (ouch*3).

But what to do if there are positions on other instruments? After all, it has become clear above that it is impossible to obtain the actual value of the margin for a single position from the terminal. But surely it can be done for the instrument as a whole?

Reason: