How do you calculate the margin?

 

Good afternoon!

Suddenly encountered a situation in which the margin on open positions has increased significantly (20 times). There is a suspicion that the broker has increased margin requirements for open positions. This assumption is confirmed by the unusually high value of AccountMargin, which is several times higher than the calculated value for this instrument and the set leverage.

On one of the accounts in such a situation, I had to close the last open positions with the largest volume with a loss to reduce the load on the deposit and not to allow the Stop Out. And at some point (after closing another losing position) the valueof AccountMargin returned to the calculated level. Perhaps, I have not noticed it at once, and therefore I cannot say for sure for which positions the increased margin requirements have been applied.

I have not done anything on the other account yet, except opening a locking order. Now I need to make a claim to the broker stating which orders were opened with increased margin requirements. And this raises a question:

Is there any way to find out the collateral (margin) amount for each open position separately in the terminal? In the terminal I only see the total amount of margin, the same value is returned by AccountMargin(). In the MQL4 Reference I have not found a suitable function with the Order prefix. It would be logical to assume that this value can be obtained using OrderGetDouble(), but again, there is no appropriate value inENUM_ORDER_PROPERTY_DOUBLE.

Please tell me how can I actually get the deposit size for a single open position in the MT4 terminal?

 
You can read ithere.
Как получить программно "Процент маржи"
Как получить программно "Процент маржи"
  • 2017.05.31
  • www.mql5.com
В окне терминала "Спецификации контракта" есть пункт "Процент маржи...
 
Alexey Viktorov:
You can read here.

Thank you, I've read it. There is a script on page 7 to calculate the calculated margin value for each open position. I adjusted it a bit, taking into account the fact that it's not CFD, but FOREX, and compiled it in this form:

void OnStart()
{
 double size = 0, percentage = 0, orderMargin = 0, accountMargin = 0;
 long leverage = 0;
 for(int i = 0; i < OrdersTotal(); i++)
  {
   int tupe = -1;
   if(OrderSelect(i, SELECT_BY_POS) && (tupe=OrderType()) < OP_BUYLIMIT)
    {
     string symbol = OrderSymbol();
     string symbolCurencyMargin = SymbolInfoString(symbol, SYMBOL_CURRENCY_MARGIN);
     double orderOpenPrice = OrderOpenPrice();
     double orderLots = OrderLots();
     double margin = MarketInfo(symbol, MODE_MARGINREQUIRED);
     double ask = MarketInfo(symbol, MODE_ASK);
     double bid = MarketInfo(symbol, MODE_BID);
     double price = symbolCurencyMargin == "USD" ? 1 : tupe == OP_BUY ? bid : ask;
      size = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
      leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
      percentage = NormalizeDouble(margin/(size*price/100)*leverage, 0);
      //orderMargin = (size*orderOpenPrice*percentage/100)/leverage;
      orderMargin = (orderLots*size*orderOpenPrice)/leverage;
      accountMargin += orderMargin;
      Print(symbolCurencyMargin, " ******** Маржа ", symbol, " = ", orderMargin);
    }
  }
 Print(AccountInfoString(ACCOUNT_CURRENCY), " ******** AccountMargin = ", DoubleToString(accountMargin, 2));
}

In the vast majority of cases, the final margin value calculated by this script coincides with the actual one. But, alas, not in my case. The problem is that the formula for calculating the margin for each order uses the value of leverage active at that moment.

But in regulations of some brokers (without pointing fingers) it is specified that for positions that are opened under certain conditions (Friday night, averaging method, etc.) the leverage is specified, and it is usually several times less than for the entire account. That is exactly my case. However, in order to correctly prepare a claim and avoid repeating such a situation in the future, I need to find out which positions have been opened with normal leverage and which have been opened with higher leverage.

The broker's support team openly says that the increased leverage is not set for the account as a whole, but for specific orders and this happens after the position has been opened. Thus, it is useless to use the AccountFreeMarginChek request.

This actually causes a question: Is there any way to reliably know, which margin is actually used for each position in the terminal, if we know that the broker can set a different leverage for each position?

 
Janis Ozols:

But in the regulations of some brokers (let us not point fingers) it is prescribed that for positions opened under certain conditions (Friday night, averaging method, etc.) a special leverage is set, usually several times lower than for the whole account. That is exactly my case. But in order to make a proper claim and avoid a repeat of this situation in the future, I need to understand which positions have been opened with normal leverage, and which have been opened with higher le verage.

1) The broker is always right.
2) If the broker is wrong, read rule #1.

 
Janis Ozols:

Thank you, I've read it. There is a script on page 7 to calculate the calculated margin value for each open position. I slightly adjusted it taking into account the fact that it's not CFD, but FOREX and compiled it like this:

In the vast majority of cases, the final margin value calculated by this script coincides with the actual one. But, alas, not in my case. The problem is that the formula for calculating the margin for each order uses the value of leverage active at that moment.

But in regulations of some brokers (without pointing fingers) it is specified that for positions that are opened under certain conditions (Friday night, averaging method, etc.) the leverage is specified, and it is usually several times less than for the entire account. That is exactly my case. However, in order to correctly prepare a claim and avoid repeating such a situation in the future, I need to find out which positions have been opened with normal leverage and which have been opened with higher leverage.

The broker's support team openly says that the increased leverage is not set for the account as a whole, but for specific orders and this happens after the position has been opened. Thus, it is useless to use the AccountFreeMarginChek request.

This actually leads to the following question: Is there any way to reliably know what margin is actually used for each position in the terminal, if we know that the broker can set a different leverage for each position?

Do not waste your time on useless correspondence. It is written in the regulations and arguing is like fighting windmills.

It's not about the symbol, it's about the opening time. Many have it 15 minutes before the weekend closes. And 15 minutes later on Monday, the leverage returns to normal.

 
Alexey Viktorov:

Don't waste your time with useless correspondence. It is written in the regulations and arguing is like fighting windmills.

I want to make a complaint mainly to get clarification about the rules for applying increased leverage. The regulations are not very clear on this.

The main thing I want to understand is how to avoid this situation in the future. Specifically, in my broker's regulations, the clause in question contains the vague wording "the company reserves the right". That is, they may or may not apply increased margin requirements to a particular order. Therefore it is very important for me to figure out how to detect in the terminal the positions to which the increased leverage (other than that issued by AccountLeverage) has been applied. For example, to promptly close such positions to inform the trader and stop trading.

Surely there is some way to request from the terminal the amount of margin for a single position. I just do not know it yet, and I cannot find it in the documentation.

 
Janis Ozols:

I would like to make a complaint mainly to get clarification about the rules for applying increased leverage. It is not very clear in the regulations.

The main thing I want to understand is how to avoid this situation in the future. Specifically, in my broker's regulations, the clause in question contains the vague wording "the company reserves the right". That is, they may or may not apply increased margin requirements to a particular order. Therefore it is very important for me to figure out how to detect in the terminal the positions to which the increased leverage (other than that issued by AccountLeverage) has been applied. For example, to promptly close such positions to inform the trader and stop trading.

Surely there is some way to request from the terminal the amount of margin for a single position. I just don't know it yet, and I can't find it in the documentation.

I do not know about your broker, but there are cases when everything was fine, we close a position and get increased margin. There is no way to trace this. And no one will give any explanations. There is only one way out: Do not trade at a time when your margin may increase. This is most likely regulated by the percentage of margin, not the leverage.

 

Calculate your own margin and leverage at the time you open a position, using publicly available formulas.

Write the leverage in the commentary or in the magik, or in general - write down the leverage-adjusted margin at once.

You will have something to remember

 
Alexey Viktorov:

I don't know about your broker, but there are cases where everything was fine, we close the position and get increased margin.

At my broker it is like this: we open a position and get increased margin. And in some cases we do and in some cases we do not ("the company reserves the right to...").


Alexey Viktorov:

There is only one way out: Do not trade at a time when there may be an increase in margin. This is most likely regulated by the percentage of margin, not the leverage.

In my case, this, alas, does not help. Margin may be increased at any time. And it is regulated by the leverage. In any case, the regulations talk about leverage. I would very much like to avoid discussing the trading conditions of a particular broker in this thread. What I am really interested in is how you can get information from the terminal about the amount of margin for a single position.

 
Renat Akhtyamov:

Calculate your own margin and leverage at the time you open a position, using publicly available formulas.

Write the leverage in the commentary or in the magik, or in general - write down the leverage-adjusted margin at once.

You will have something to remember.

I tried to do that. But the problem is that the margin increases after opening a position. In other words, before opening a position, I get the calculated value of margin for it. And immediately after a successful OrderSend call, I ask the terminal for the value of AccountMargin. And it fully meets my expectations. But after a while this value sharply increases! And the valueof AccountMargin turns out to be tens of times greater than the amount of margin for each open order, obtained immediately after its opening.

My question can be reduced to how at any given moment the terminal can obtain the actual value of margin for a specific order.

This is AccountMargin() or AccountInfoDouble(ACCOUNT_MARGIN) for the whole account. So, there are two ways to get this value. Surely, there must be at least one way to obtain the value of margin for an order. I just do not know about it, that's why I am asking.

 
Janis Ozols:

I have tried this. But the problem is that the margin increases after the position is opened. In other words, before opening a position, I get the calculated margin value for it. And immediately after a successful OrderSend call, I ask the terminal for the value of AccountMargin. And it fully meets my expectations. But after a while this value sharply increases! And the valueof AccountMargin turns out to be tens of times greater than the amount of margin for each open order, obtained immediately after its opening.

My question can be reduced to how at any given moment the terminal can obtain the actual value of margin for a specific order.

This is AccountMargin() or AccountInfoDouble(ACCOUNT_MARGIN) for the whole account. So, there are two ways to get this value. Surely, there must be at least one way to obtain the value of margin for an order. I simply do not know about it yet, that's why I'm asking.

you do not need to ask

Consider formulae

and the shoulder, too. using formulas

the terminal does not track the current leverage, but gives you the one you had when you loaded the terminal

that's why the margin requested by this function is different

Reason: