How to get the "Percentage of Margin" programmatically - page 6

 
ir0407:
The margin percentage is not the calculated collateral. It is just one of the components to calculate the margin. And the result of this calculation (using formulas from the table) is returned in margin currency, which then (if it differs from the deposit currency) needs to be converted to the deposit currency.
All understood except one, what is the essence of this component)
 
K-2SO:

Can you suggest a broker, please? For my tests...
Robo.
 

I'll share the tests, and I'm finally confused by these formulas, which so far have failed to add up to one single formula. Three different brokers - three different percentages of margin - three different formulas, which you could almost say fit the result. This is the calculation of the margin on one already placed(!) order (BUY) on gold.

As you can see the margin percentage is still sometimes important, but the meta-quotes really don't need it.

 margin=OrderLots()*contract*OrderOpenPrice()/leverage/100; // метаквоты - процент маржи 100%

 margin=OrderLots()*contract*OrderOpenPrice()*Percentage/100; // инста - процент маржи 1%
            
 margin=(OrderLots()*contract*OrderOpenPrice()*Percentage)/leverage/100;// робо - процент маржи 200%
 
I think I understood how the general formula is supposed to work (when I finally realised that there is actually a margin percentage) - the tests didn't go away for nothing. And yes... The margin percentage value should definitely be used (even for an already placed order). When I understand how to calculate and get this value, I will share my conclusions about the general formula for calculating a margin for a single placed CFD order, if anyone is interested... Although, in principle, everything is in sight (the dependence we are looking for), in the code of the message above.
 
K-2SO:
I think I understood how the general formula should work (when I finally realised what the margin percentage actually is) - the tests didn't go in vain. And yes... The margin percentage value should definitely be used (even for an already placed order). When I understand how to calculate and get this value, I will share my conclusions about the general formula for calculating a margin for a single placed CFD order, if anyone is interested... Although, in principle, everything is in sight (the dependence we are looking for) in the code in the message above.

Everything is on the front page

Forum on trading, automated trading systems and testing trading strategies

How to get "Margin Percentage" programmatically

Alexey Viktorov, 2017.06.02 09:42

Right... I do not know why people trying to help you should fantasize about what you fantasized 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)

It is not in the list of obsolete functions yet. So it produces the missing value to determine the percentage of margin.

MarketInfo(_Symbol, MODE_MARGINREQUIRED);

Having obtained all the data for the current time and at the current price, it is very easy to recalculate the margin that has been withheld 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   // коэффициент взимания поддерживающей маржи 
   );

When opening an order, the current price and the same margin percentage are taken into consideration that can only be obtained from the formula. Accordingly, we should first define the expected margin for the full lot at the current price using the following function

MarketInfo(symbol, MODE_MARGINREQUIRED);

From this margin we calculate the percentage of margin and then calculate the margin for a specific order. But again, margin is not always calculated in the currency of the deposit and is converted to it at the current exchange rate at the time of opening the order, and there is no way to get it (exchange rate) later in mql4. In mql5 it is easier, there is CopyTicks() and you can get the exchange rate at the time of opening the order, but this is not the language we are talking about here.

 
Alexey Viktorov:

But again, margin is not always calculated in the currency of the deposit and is converted to it at the current exchange rate at the time of opening the order, and there is no way to get it (the exchange rate) later in mql4.

I still get confused at this point. It is clear that MARGINREQUIRED returns the value in the deposit currency, but what currency do we need? In the currency of the symbol quote?
 
K-2SO:
I'm still confused at this point. It is clear that MARGINREQUIRED returns the value in the deposit currency. In the currency of the symbol quote?

Compare the 2 pictures, in the first post and on page 5 in my post. Both are gold specifications, but in the first one the margin currency is USD and in the second one XAU. Hence, in the first case, you can do nothing. In the second, after receiving margin in the currency of deposit it should be converted to margin currency, by multiplying to a quote and put it in the formula using which we calculate the margin percentage. Otherwise, the result will be incorrect. Accordingly, we should again add to the formula

SYMBOL_CURRENCY_MARGIN

Currency in which the margin is calculated

string

 

After some experiments, I must admit that here

Forum on trading, automated trading systems and testing trading strategies

How to get "Margin Percentage" programmatically

Alexey Viktorov, 2017.06.02 10:24

Leverage is generally outside of the basic formula Margin = (Lots*ContractSize*MarketPrice*Percentage)/Leverage, so I did not take it into account. For determining what the question is about, the leverage is like a cow's saddle. But further it may be required, but it doesn't concern the current question...

I was wrong. Leverage must be used because the margin

MarketInfo(symbol, MODE_MARGINREQUIRED);

we get it with leverage.



 
Alexey Viktorov:

After some experimentation, I have to admit that here

I was wrong. The leverage must be used because the margin

is obtained by taking the leverage into account.

Tests show that when the margin percentage is equal to 1 the leverage is not taken into account! But from 100 and above, it is already taken into account. I wish I could find a broker with margin percentage from 2 to 99 inclusive.

 
K-2SO:

Tests show that when the margin percentage is equal to 1, leverage is not taken into account! But when it is 100 and above, it is already taken into account. I wish I could find a broker with a margin percentage between 2 and 99 inclusive.

Open a demo account for 5 quid with 100 leverage and see what the margin percentage is, for example, the same for gold or even better - the ruble ...
Reason: