# How to get the "Percentage of Margin" programmatically

In the "Contract Specifications" window of the terminal, there is an item "Percentage of Margin". It's supposed to be returned by SymbolInfoDouble(), but I didn't find any value among its specifiers that allows to find out this "Percentage of Margin". How can it be obtained programmatically?

What does it mean, this percentage of margin?

Alexey Viktorov:
What does it mean, this percentage of margin?
It is used in the CFD collateral calculation. See the description of ENUM_SYMBOL_CALC_MODE.

Sorry for the incomplete question, I should have asked more explicitly.

What is the purpose of the question? To check if the margin is calculated correctly/wrong? If not, it can be calculated from that formula in the description. And then what do you do with it?

Alexey Viktorov:

Sorry for the incomplete question, I should have asked more explicitly.

What's the goal? To check whether the margin is calculated correctly or incorrectly?

With the right imagination, you can come up with a lot of goals. :) And not at all to check for "right/wrong".

If not, it can be calculated from the formula in the description.

How? Well, that's what this "Margin Percentage" is for. You're suggesting we type it in by hand?) The code must be able to get it programmatically.

And then what to do with it?

Now the question is - to get the value of "Percentage of margin" programmatically. If it is in the table, it must be possible to get it programmatically. And what to do next is a separate question, which is not relevant to this topic.

```SymbolInfoDouble(symbol, SYMBOL_MARGIN_INITIAL);
SymbolInfoDouble(symbol, SYMBOL_MARGIN_MAINTENANCE);```

Taras Slobodyanik:

see SymbolInfoDouble

Unfortunately, that's not it. In MT4 these values are, more often than not, zero.

Is that okay?

```double procentMgn;
procentMgn=AccountBalance()*100/MarketInfo("GOLD",MODE_MARGINREQUIRED);```

Renat Akhtyamov:

is that okay?

No, it won't.

1. MarketInfo() function is obsolete.

2. MODE_MARGINREQUIRED returns the correct result only for long positions, it fails for short ones.

3. MODE_MARGINREQUIRED returns the result already in the depo currency, which I don't need.

4. I don't need percentage of deposit. I just need the value of "margin percentage" parameter for the symbol. And I can calculate what I need myself.

ir0407:

No, it won't.

1. MarketInfo() function is obsolete.

2. MODE_MARGINREQUIRED returns the correct result only for long positions, for short ones it fails.

3. MODE_MARGINREQUIRED returns results in the depo currency, which I don't need.

4. I don't need a percentage of the deposit. I just need the value of "Percentage of Margin" for the symbol. And I can calculate it myself.

If you need it on MT5, go to another forum section.

You are not surprised by your common knowledge, or worse:

1. It's not the MarketInfo() function, but an expression whose result will be a percentage of

2. you do not have at least 4 decimal places, so the % margin for long and short will be the same.

3. just multiply

`procentMgn`

You may use 0.01, and you'll be happy.

ir0407:

With the right imagination, you can think of many targets. :) And not at all for the right/wrong test.

How does it work? You need this very "Percentage of Margin". You want me to type it in by hand?) The code should be able to get it programmatically.

Now the question is - to get the value of "Percentage of margin" programmatically. If it is in the table, it must be possible to get it programmatically. And what to do next is a separate question which is not relevant to this topic.

Yeah, well... fantasy is everything... I just don't understand why people trying to help you should fantasize about what you fantasize about? Anyway, that's not the question.

How do you get what you need from that formula?

The formula itself is Margin = Lots *ContractSize*MarketPrice*Percentage/100

Hence Percentage = Margin/(Lots *ContractSize*MarketPrice/100)

ir0407:

1. The MarketInfo() function is obsolete.

It is not in the list of deprecated functions yet. Therefore, it produces the missing value to determine the margin percentage.

`MarketInfo(_Symbol, MODE_MARGINREQUIRED);`

Having obtained all the data at the current moment, at the current price, it is easy to recalculate the margin that was withheld when the order was opened, at the opening price of the order.

If you need it for mql5, you should use

```bool  SymbolInfoMarginRate(
string             name,                     // символ
ENUM_ORDER_TYPE    order_type,               // тип ордера
double&            initial_margin_rate,      // коэффициент взимания начальной маржи
double&            maintenance_margin_rate   // коэффициент взимания поддерживающей маржи
);```
Устаревшие функции - Справочник MQL4
• docs.mql4.com
Устаревшие функции - Справочник MQL4