How do you calculate the margin? - page 5

 
How to calculate leverage programmatically is written in the same thread. Instead of writing a footnote, pay more attention to the answers to your questions
 

I am probably very clumsy with my questions and therefore I am not getting the answers I expect. Forgive me, Russian is only partly my mother tongue. But I will try again.

I carefully read the whole branch. The answer to my original question was given - it is impossible to get the actual value of leverage for a single position from the terminal.

But it was also mentioned that perhaps the leverage changes not for these positions, but for an instrument as a whole. And now the only question I have left is how to get that leverage from the terminal. Provided that it is different from the total leverage of the account. Not to calculate, but exactly to get the actual value.

It would be quite good if it could be done BEFORE opening the next position.

 
Janis Ozols:

I am probably very clumsy with my questions and therefore I am not getting the answers I am expecting. Forgive me, Russian is only partly my mother tongue. But I will try again.

I carefully read the whole branch. The answer to my original question was given - it is impossible to get the actual value of leverage for a single position from the terminal.

But it was also mentioned that perhaps the leverage changes not for these positions, but for an instrument as a whole. And now the only question I have left is how to get that leverage from the terminal. Provided that it is different from the total leverage of the account. Not to calculate, but exactly to get the actual value.

It would be nice if you could do it BEFORE opening the next position.

The broker told you - may change it AFTER

and BEFORE - everything is already written above, but you need to calculate

 
Renat Akhtyamov:

You were told by your broker - he can change it AFTER
and BEFORE - it's all written above, but you need to calculate

Yes, that's what I want to know, how do I get the actual leverage for a symbol AFTER the broker has changed it, but BEFORE opening the next position on that symbol. I am not able to calculate it correctly yet. Because all the formulas for calculations presented earlier contain either the account leverage (which remains unchanged) or the amount of margin from the symbol settings (which also has not changed).

 
Janis Ozols:

Right, so I'm trying to figure out how to get the actual leverage for a symbol AFTER the broker has changed it, but BEFORE opening the next position on that symbol. I am not able to calculate it correctly yet. Because all the formulas for calculations presented earlier contain either the account leverage (which has remained unchanged) or the amount of margin from the symbol settings (which has not changed either).

come on

actual leverage:

https://www.mql5.com/ru/forum/353040/page2#comment_18675097

leverage in the margin calculation

https://www.mql5.com/ru/forum/353040/page4#comment_18728440

and you're in luck.

;)

Как вычислить маржу?
Как вычислить маржу?
  • 2020.10.09
  • www.mql5.com
Добрый день! Внезапно столкнулся с ситуацией, в которой залог по открытым позициям существенно (в 20 раз) увеличился...
 

There is a formula on the link:

LEVERAGE=NormalizeDouble(VOL/MarketInfo("USDCHF",MODE_MARGINREQUIRED),0);

It contains MarketInfo("USDCHF",MODE_MARGINREQUIRED) value, which does not change after the broker has changed the leverage for this instrument. TheMarketInfo function returns the margin from the symbol settings, which corresponds to the leverage in the account settings. If it didn't, I wouldn't have any more questions. This is exactly where the problem lies.

Renat Akhtyamov:

it is the resulting leverage that we substitute in the margin calculation

https://www.mql5.com/ru/forum/353040/page4#comment_18728440

And the problem will be that the leverage obtained in the previous step will not correspond to the actual leverage. It will be equal to the leverage of account settings, which is returned by AccountLeverage() function. Accordingly, the margin, calculated this way, will be much smaller than the actual one, if the leverage of this instrument doesn't correspond to the account leverage.

If you want, you can easily check it yourself:

  1. Open a demo account on the Alpari-Demo server. While opening the account, select an amount of 10000 USD and a leverage of 1:500.
  2. Open a position to buy 1 lot of USDRUB (UZDZAR, UZDTRY)
  3. Calculate the leverage and then the margin using the formulas you propose.
  4. Compare it with the one you see in your terminal
 
Janis Ozols:

There is a formula on the link:

It contains MarketInfo("USDCHF",MODE_MARGINREQUIRED) value, which does not change after the broker has changed the leverage for this instrument. TheMarketInfo function returns the margin from the symbol settings, which corresponds to the leverage in the account settings. If it didn't, I wouldn't have any more questions. That is exactly where the problem lies.

And the problem here will be that the leverage obtained in the previous step will not match the actual leverage. It will be equal to the leverage of account settings, which is returned by AccountLeverage(). Accordingly, the margin, calculated this way, will be much smaller than the actual one, if the leverage of this instrument doesn't correspond to the account leverage.

If you want, you can easily check it yourself:

  1. Open a demo account on the Alpari-Demo server. When opening the account, select an amount of 10000 USD and a leverage of 1:500.
  2. Open a position to buy 1 lot of USDRUB (UZDZAR, UZDTRY)
  3. Calculate the leverage and then the margin with the formula you propose.
  4. Compare it with what you see in the terminal

You should try it first before predicting the outcome.

I've been working with this formula on floating leverage, it reacts in time

and it's not what you see with your eyes.

and it's certainly not what you saw with your eyes.

and not on demo, but on real

 
Renat Akhtyamov:

You should try it first before predicting the outcome

I did not predict the outcome. Of course, I tried to do so before suggesting it to you.

Here's the script:

void OnStart()
{
   double VOL = MarketInfo("USDRUB",MODE_LOTSIZE);
   double LEVERAGE = NormalizeDouble(VOL/MarketInfo("USDRUB",MODE_MARGINREQUIRED),0);
   double M = VOL / LEVERAGE; // M=CC/КП
   Print("М = ",M);
   Print("LEVERAGE = ",LEVERAGE);
   Print("VOL = ",VOL);
}


Here is the result of its execution:


Here is the actual value:


The reason for the discrepancy is that the USDRUB symbol's leverage is different from the account's leverage. And my question is how to get the value of this leverage by means of MQL4 before opening a position.

 

ok

Try it like this now

void OnStart()

{

   double LEVERAGE = NormalizeDouble( MarketInfo("EURUSD",MODE_LOTSIZE)/MarketInfo("USDCHF",MODE_MARGINREQUIRED),0);

   double M = MarketInfo( "USDRUB" ,MODE_LOTSIZE)/ LEVERAGE; // M=CC/ КП

   Print(" М = ",M);

   Print("LEVERAGE = ",LEVERAGE);

   Print("VOL = ",MarketInfo( "USDRUB" ,MODE_LOTSIZE));

}

 
Renat Akhtyamov:

ok

Try this now.

I did. Here is the result:

However, I don't really understand why this time to calculate the USDRUB leverage you suggest to divide the EURUSD contract volume by the margin to open one standard lot for USD/CHF. But the result is the same value of margin (200). Whereas the actual margin held is 1000.