Margin check: OrderCalcMargin vs OrderCheck

 

Hi, 

reading documentation it seems (maybe i'm wrong) there is no EXACT way to know, before a trade is open, how much margin the trade will take. 

Reading OrderCalcMargin, doc specify:

The function calculates the margin required for the specified order type, on the current account, in the current market environment not taking into account current pending orders and open positions.

In the algotrading book, for OrderCheck it specify:

Unlike the OrderCalcMargin function which calculates the estimated margin required for only one proposed position or order, OrderCheck takes into account, albeit in a simplified mode, the general state of the trading account. So it fills the margin field in the MqlTradeCheckResult structure and other related fields (margin_free, margin_level) with cumulative variables that will be formed after the execution of the order. For example, if a position is already open for any instrument at the time of the OrderCheck call and the request being checked increases the position, the margin field will reflect the amount of deposit, including previous margin liabilities. If the new order contains an operation in the opposite direction, the margin will not increase (in reality, it should decrease, because a position should be closed completely on a netting account and the hedging margin should be applied for opposite positions on a hedging account; however, the function does not perform such accurate calculations).


So, to check margin exposure on account before opening a trade, which function should i use? I'm a bit confisued. Is doc meaning that OrderCheck is more valuable than OrderCalcMarginor am I misunderstanding?

Also, does it makes any difference if the account is in netting mode rather than hedging mode? That is, if i use orderCalcMargin in a netting account, and i have no pending orders, can i be sure OrderCalcMargin give me the right margin for the operation? Or "open positions" can affect margin also in netting account?


Thanks

Manuale MQL5: Margin calculation for a future order: OrderCalcMargin / Trading automation
Manuale MQL5: Margin calculation for a future order: OrderCalcMargin / Trading automation
  • www.mql5.com
Before sending a trade request to the server, an MQL program can calculate the margin required for a planned trade using the OrderCalcMargin...
 
antony23:

reading documentation it seems (maybe i'm wrong) there is no EXACT way to know, before a trade is open, how much margin the trade will take. 

Reading OrderCalcMargin, doc specify:

The function calculates the margin required for the specified order type, on the current account, in the current market environment not taking into account current pending orders and open positions.

In the algotrading book, for OrderCheck it specify:

Unlike the OrderCalcMargin function which calculates the estimated margin required for only one proposed position or order, OrderCheck takes into account, albeit in a simplified mode, the general state of the trading account. So it fills the margin field in the MqlTradeCheckResult structure and other related fields (margin_free, margin_level) with cumulative variables that will be formed after the execution of the order. For example, if a position is already open for any instrument at the time of the OrderCheck call and the request being checked increases the position, the margin field will reflect the amount of deposit, including previous margin liabilities. If the new order contains an operation in the opposite direction, the margin will not increase (in reality, it should decrease, because a position should be closed completely on a netting account and the hedging margin should be applied for opposite positions on a hedging account; however, the function does not perform such accurate calculations).


So, to check margin exposure on account before opening a trade, which function should i use? I'm a bit confisued. Is doc meaning that OrderCheck is more valuable than OrderCalcMarginor am I misunderstanding?

Also, does it makes any difference if the account is in netting mode rather than hedging mode? That is, if i use orderCalcMargin in a netting account, and i have no pending orders, can i be sure OrderCalcMargin give me the right margin for the operation? Or "open positions" can affect margin also in netting account?

The quotes seem very informative and self-explaining IMHO. OrderCalcMargin() estimates a dedicated margin for a single prospective order, not taking into account current pending orders and open positions, whereas OrderCheck() takes this into account.

Both functions should provide more or less correct estimations, provided that the broker correctly setup MT5 server. Netting and hedging accounts will most likely vary in the margin calculations, but I'm afraid this is a broker-specific and market-specific (even symbol-specific) matter. MQL5 API provides more settings related to margin calculation. But, strictly speaking - yes, all of this gives just an estimation, not an EXACT value.

You can use both functions and supplement them with additional calculations/verifications.

 

The question i'm missing is: why open positions and pending orders of OTHER symbols should affect margin of "new symbol" trade?

The formulas do you refer to are this ? Are not them the same the OrderCalcMargin already does? Also it seems them does not take into account open positions and pending order, i'm correct?

We can say that value returned by OrderCalcMargin is a superior limit and real margin could be lower/equal than that, but not greater?
Manuale MQL5: Margin requirements / Trading automation
Manuale MQL5: Margin requirements / Trading automation
  • www.mql5.com
Among the most important information about a financial instrument for a trader is the amount of funds required to open a position. Without knowing...
 
antony23 #:

The question i'm missing is: why open positions and pending orders of OTHER symbols should affect margin of "new symbol" trade?

I don't think it's said anywhere that other symbols have an impact on the "considered" symbol.

The formulas do you refer to are this ? Are not them the same the OrderCalcMargin already does? Also it seems them does not take into account open positions and pending order, i'm correct?

Why do you know to have an exact calculation ?

I never had a problem using OrderCalclMargin(), as it will give you the maximum margin used, the "worst" case scenario, which will allow to decide if you take the order or not.

 
antony23 #:

The question i'm missing is: why open positions and pending orders of OTHER symbols should affect margin of "new symbol" trade?

The formulas do you refer to are this ? Are not them the same the OrderCalcMargin already does? Also it seems them does not take into account open positions and pending order, i'm correct?

We can say that value returned by OrderCalcMargin is a superior limit and real margin could be lower/equal than that, but not greater?

You have missed the point that MqlTradeCheckResult struct (filled in by OrderCheck(request, &result)) contains not only specific order margin, but complete margin requirements after the order placed. Of course, any symbol trade will affect account equity and margin, and hence other trades.

Also existing positions of other symbols can be used as collateral funds for specific/related symbols decreasing required money for new margin.

The link is correct. You can call OrderCalcMargin() many times for all existing positions and planned orders, then apply the formulae (combine all the values appropriately), and should get something close to what a next call to OrderCheck() returns.

 

@Alain Verleyen Hi Alain, thanks, that is what i was looking for, that is OrderCalclMargin() give me the worst secnario, This is ok for me.

Anyway, speaking about documentation, they write that "open positions and pending orders" are not taking into account by OrderCalcMargin calculation. But anywhere i read such statements, it is not specify that this could be a problem with only the "considered" symbol, but it general speaks about overlapping voume and opposite position.

Here there is this statement:
"Looking ahead, let's say that for a given combination of parameters such as the trading operation type, instrument, volume, and price, MQL5 allows you to calculate the margin using the OrderCalcMargin function. This is the simplest method, but it has a significant limitation: the function does not take into account current open positions and pending orders. This, in particular, ignores possible adjustments for overlapping volumes when opposite positions are allowed on the account. Thus, in order to obtain a breakdown of the account funds currently used as a margin for open positions and orders, an MQL program may need to analyze the following properties and calculations using formulas."

So my question is, overlapping volume and/or opposite position could happen in only hedging account or also in netting account? And them could happen if we have only "open positions and pending orders" for same symbol we want to trade, or for any correlated symbol?

As @Stanislav Korotky writes "Also existing positions of other symbols can be used as collateral funds for specific/related symbols decreasing required money for new margin". So i'm bit confused. It maybe would mean that if we have an open trade on an cross like EURGBP, and we would open a trade on EURUSD, the margin for EURUSD trade could be affected by the EURGBP volumes?


About the needed to use OrderCheck and/or forumals i really don't understand the reason, as also for OrderCheck they write: "If the new order contains an operation in the opposite direction, the margin will not increase (in reality, it should decrease, because a position should be closed completely on a netting account and the hedging margin should be applied for opposite positions on a hedging account; however, the function does not perform such accurate calculations) ".

So why i should prefer OrderCheck over OrderCalcMargin ? It seems that OrderCheck is the same as using OrderCalcMargin + current account margin. About formulas, i cannot see the reason to use them if i can simply use OrderCalcMargin or OrderCheck. am i wrong?


 
antony23 #:

@Alain Verleyen Hi Alain, thanks, that is what i was looking for, that is OrderCalclMargin() give me the worst secnario, This is ok for me.

Anyway, speaking about documentation, they write that "open positions and pending orders" are not taking into account by OrderCalcMargin calculation. But anywhere i read such statements, it is not specify that this could be a problem with only the "considered" symbol, but it general speaks about overlapping voume and opposite position.

Here there is this statement:
"Looking ahead, let's say that for a given combination of parameters such as the trading operation type, instrument, volume, and price, MQL5 allows you to calculate the margin using the OrderCalcMargin function. This is the simplest method, but it has a significant limitation: the function does not take into account current open positions and pending orders. This, in particular, ignores possible adjustments for overlapping volumes when opposite positions are allowed on the account. Thus, in order to obtain a breakdown of the account funds currently used as a margin for open positions and orders, an MQL program may need to analyze the following properties and calculations using formulas."

So my question is, overlapping volume and/or opposite position could happen in only hedging account or also in netting account? And them could happen if we have only "open positions and pending orders" for same symbol we want to trade, or for any correlated symbol?

As @Stanislav Korotky writes "Also existing positions of other symbols can be used as collateral funds for specific/related symbols decreasing required money for new margin". So i'm bit confused. It maybe would mean that if we have an open trade on an cross like EURGBP, and we would open a trade on EURUSD, the margin for EURUSD trade could be affected by the EURGBP volumes?


About the needed to use OrderCheck and/or forumals i really don't understand the reason, as also for OrderCheck they write: "If the new order contains an operation in the opposite direction, the margin will not increase (in reality, it should decrease, because a position should be closed completely on a netting account and the hedging margin should be applied for opposite positions on a hedging account; however, the function does not perform such accurate calculations) ".

So why i should prefer OrderCheck over OrderCalcMargin ? It seems that OrderCheck is the same as using OrderCalcMargin + current account margin. About formulas, i cannot see the reason to use them if i can simply use OrderCalcMargin or OrderCheck. am i wrong?

I think I already answered, let's summarize : the ACTUAL margin used by a new order depends of the whole context of trading : existing positions, netting/hedging etc... OrderCalcMargin() gives you the margin for an order INDEPENDENTLY of the context, so it's the maximum margin that could be used. You should work with that. Simple.


You could use OrderCheck() but it's still not always 100% correct as it doesn't take into account all possibilities. So if you really need to know the exact margin which will be used by a new order (why ?), you need to calculate it yourself :

- The margin of a EURGBP is usually in EUR, so if your account is USD, yes the margin will vary according to EURUSD.
- If you open a BUY EURGBP and you already have an EURGBP SELL, the margin will depend of the account type, and the "hedging" margin which is a symbol's parameter.
- Pending orders can also be important to consider, though they don't use margin, but margin will be required when the order will trigger.

This list is not exhaustive of course. The result will be <= to what OrderCalcMargin() gave you.

 
Thanks @Alain Verleyen, all clear!
 
antony23 #:

About formulas, i cannot see the reason to use them if i can simply use OrderCalcMargin or OrderCheck. am i wrong?

How would you estimate new margin for multiple new orders, for example, before placing a grid, without custom calculations?

As for collateral funds, affecting margin, depending from your broker and regulations, for example, you can buy securities, which value will be still included into funds available for free magin for forex trades.