OrderCalcMargin price parameter

 

The OrderCalcMargin function returns required margin for opening a position.

While the volume and type of order are understandable (sell or buy may have different margin requirements?), why is the open price needed and how does it affect the margin required.

Edit: Thinking about it,maybe to calculate the margin requires the rate while opening the trade in order to know how much is the position volume to open in account currency. And also for pending orders. 

Thanks

 
Amir Yacoby:

The OrderCalcMargin function returns required margin for opening a position.

While the volume and type of order are understandable (sell or buy may have different margin requirements?), why is the open price needed and how does it affect the margin required.

Edit: Thinking about it,maybe to calculate the margin requires the rate while opening the trade in order to know how much is the position volume to open in account currency. And also for pending orders. 

Thanks

Yes, margin is in account currency, so depending of the symbol, the price may matters to calculate it.

If your account is in USD, and you trade USDCAD, margin is fixed, but if you trade EURUSD a conversion is needed which depends of EURUSD quotation.

 

I created a little script to check :

void OnStart()
  {
//---
   ENUM_ORDER_TYPE types[]={ORDER_TYPE_BUY,ORDER_TYPE_SELL};
                          //,ORDER_TYPE_BUY_LIMIT,ORDER_TYPE_SELL_LIMIT,ORDER_TYPE_BUY_STOP,
                           ORDER_TYPE_SELL_STOP,ORDER_TYPE_BUY_STOP_LIMIT,ORDER_TYPE_SELL_STOP_LIMIT,ORDER_TYPE_CLOSE_BY};
   MqlTick tick;
   if(!SymbolInfoTick(_Symbol,tick)) return;
   double lowerlimit=tick.bid-500*_Point;
   double upperlimit=tick.bid+500*_Point;
   
   for(int t=0;t<ArraySize(types);t++)
     {
      double margin;

      for(double price=lowerlimit;price<upperlimit;price+=10*_Point)
        {
         string msg=StringFormat("Type: %s Symbol: %s Volume: 1.0 Price: %f",EnumToString(types[t]),_Symbol,price);

         if(OrderCalcMargin(types[t],_Symbol,1.0,price,margin))
            printf("Margin %.2f. %s",margin,msg);
         else
            printf("Margin can't be calculated, error %i. %s",GetLastError(),msg);
        }
     }
  }
 
Alain Verleyen:

I created a little script to check :

Thank you Alain for your effort. 
In my case account currency is USD and I ran the script on EURNZD I get both buy and sell order types the same margin for all open prices.

 
Amir Yacoby:

Thank you Alain for your effort. 
In my case account currency is USD and I ran the script on EURNZD I get both buy and sell order types the same margin for all open prices.

Yes as the margin in USD doesn't depend of EURNZD price, but only of EURUSD. So, at a given moment, same margin for all open prices, but different margin at different times.
 
Alain Verleyen:
Yes as the margin in USD doesn't depend of EURNZD price, but only of EURUSD. So, at a given moment, same margin for all open prices, but different margin at different times.

So, to my initial question, why should we supply the open price of EURNZD if what matters is the price of EURUSD on time of execution?

Edit: the standard case I guess is I want to check how much of margin I will need to open a EURNZD position. It does not depend on EURNZD price as you say, and the EURUSD price is known so it's silly to ask it from me, and in case of future pending order on EURNZD, I can't predict the EURUSD price.. So, I am left without an answer.

 
Amir Yacoby:

So, to my initial question, why should we supply the open price of EURNZD if what matters is the price of EURUSD on time of execution?

Edit: the standard case I guess is I want to check how much of margin I will need to open a EURNZD position. It does not depend on EURNZD price as you say, and the EURUSD price is known so it's silly to ask it from me, and in case of future pending order on EURNZD, I can't predict the EURUSD price.. So, I am left without an answer.

It's a generic function, so seems normal to me to have a price as parameter (which will not always be used).

Example USD account, 3 cases :

  1. USDxxx doesn't need a price (margin is directly in USD).
  2. xxxyyy (no USD implied), doesn't use a price but only xxxUSD (or USDxxx), seems the current market watch price is used, which is in contradiction to case 3 below.
  3. xxxUSD, need the price to be able to convert xxx margin in USD, and you need to provide it as it's not necessarily current price.
But I agree with you it's not clear, as case 2 should also depends of cross price which is not the current price but is unknown. Either it's a bug or a feature.
 
Alain Verleyen:

It's a generic function, so seems normal to me to have a price as parameter (which will not always be used).

Example USD account, 3 cases :

  1. USDxxx doesn't need a price (margin is directly in USD).
  2. xxxyyy (no USD implied), doesn't use a price but only xxxUSD (or USDxxx), seems the current market watch price is used, which is in contradiction to case 3 below.
  3. xxxUSD, need the price to be able to convert xxx margin in USD, and you need to provide it as it's not necessarily current price.
But I agree with you it's not clear, as case 2 should also depends of cross price which is not the current price. Either it's a bug or a feature.
Ok, that's the answer. I wouldn't guess.
It started off by me trying to find out how to calculate the margin without the function, so now I am not sure about case 2. 

Thank you.
 

Hi,

my account currency is EUR.

I have a position on the forex pair AUDJPY.

In consequence the margin of this position is dependent from the current quotations of the pairs AUDJPY, EURJPY and EURAUD, I think.

Because of the steady movements of all involved quotations the margin should vary too, isn't it?

But if I open a position in AUDJPY and observe the Margin in the Toolbox-Window the margin remains constant and never changes:

If I run the following EA, the account margin remains constant, but the margin calculated by OrderCalcMargin is variable as I expect.

#property strict
#include <Trade/Trade.mqh>
#include <Trade/AccountInfo.mqh>
#include <Trade/PositionInfo.mqh>
int OnInit() {
  EventSetTimer (100);
  CTrade trade;
  if (!trade.PositionOpen (Symbol(), ORDER_TYPE_BUY, 1.0, SymbolInfoDouble(Symbol(),SYMBOL_ASK), 0.0, 0.0)) {
    return INIT_FAILED;
  }
  MarginInfo();
  return INIT_SUCCEEDED;
}
void OnDeinit(const int reason) {
  EventKillTimer ();
}
void OnTimer(void) {
  MarginInfo();
}
void MarginInfo(void) {
  CAccountInfo  ai;
  CPositionInfo pi;
  double margin;
  int total = PositionsTotal();
  for (int i = 0; i < total; i++) {
    if (pi.SelectByIndex (i) == true) {
      if (OrderCalcMargin (ORDER_TYPE_BUY, Symbol(), pi.Volume(), pi.PriceOpen(), margin)) {
        PrintFormat ("%s: %s Account Margin = %8.2f %s   <---->   Calc Margin = %8.2f %s",
                     TimeToString(TimeCurrent()), Symbol(), ai.Margin(), ai.Currency(), margin, ai.Currency());
      }
    }
  }
}


The output is:

2021.11.08 15:42:56.072 MarginDiverges (AUDJPY,M1) 2021.11.08 16:42: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.65 EUR

2021.11.08 15:44:36.012 MarginDiverges (AUDJPY,M1) 2021.11.08 16:44: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.56 EUR

2021.11.08 15:46:16.025 MarginDiverges (AUDJPY,M1) 2021.11.08 16:46: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.58 EUR

2021.11.08 15:47:56.006 MarginDiverges (AUDJPY,M1) 2021.11.08 16:47: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.66 EUR

2021.11.08 15:49:36.025 MarginDiverges (AUDJPY,M1) 2021.11.08 16:49: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.74 EUR

2021.11.08 15:51:16.018 MarginDiverges (AUDJPY,M1) 2021.11.08 16:51: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.71 EUR

2021.11.08 15:52:56.019 MarginDiverges (AUDJPY,M1) 2021.11.08 16:52: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.74 EUR

2021.11.08 15:54:36.001 MarginDiverges (AUDJPY,M1) 2021.11.08 16:54: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.68 EUR

2021.11.08 15:56:16.014 MarginDiverges (AUDJPY,M1) 2021.11.08 16:56: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.65 EUR

2021.11.08 15:57:56.022 MarginDiverges (AUDJPY,M1) 2021.11.08 16:57: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.47 EUR

2021.11.08 15:59:36.025 MarginDiverges (AUDJPY,M1) 2021.11.08 16:59: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.49 EUR

2021.11.08 16:01:16.029 MarginDiverges (AUDJPY,M1) 2021.11.08 17:01: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.50 EUR

2021.11.08 16:02:56.024 MarginDiverges (AUDJPY,M1) 2021.11.08 17:02: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.51 EUR

2021.11.08 16:04:36.003 MarginDiverges (AUDJPY,M1) 2021.11.08 17:04: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.49 EUR

2021.11.08 16:06:16.020 MarginDiverges (AUDJPY,M1) 2021.11.08 17:06: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.58 EUR


You see the discrepancy between the account margin and the calculated margin. Can somebody explain this?

Thank you.

Matthias


 
Dr Matthias Hammelsbeck #:

Hi,

my account currency is EUR.

I have a position on the forex pair AUDJPY.

In consequence the margin of this position is dependent from the current quotations of the pairs AUDJPY, EURJPY and EURAUD, I think.

Because of the steady movements of all involved quotations the margin should vary too, isn't it?

But if I open a position in AUDJPY and observe the Margin in the Toolbox-Window the margin remains constant and never changes:

If I run the following EA, the account margin remains constant, but the margin calculated by OrderCalcMargin is variable as I expect.


The output is:

2021.11.08 15:42:56.072 MarginDiverges (AUDJPY,M1) 2021.11.08 16:42: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.65 EUR

2021.11.08 15:44:36.012 MarginDiverges (AUDJPY,M1) 2021.11.08 16:44: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.56 EUR

2021.11.08 15:46:16.025 MarginDiverges (AUDJPY,M1) 2021.11.08 16:46: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.58 EUR

2021.11.08 15:47:56.006 MarginDiverges (AUDJPY,M1) 2021.11.08 16:47: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.66 EUR

2021.11.08 15:49:36.025 MarginDiverges (AUDJPY,M1) 2021.11.08 16:49: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.74 EUR

2021.11.08 15:51:16.018 MarginDiverges (AUDJPY,M1) 2021.11.08 16:51: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.71 EUR

2021.11.08 15:52:56.019 MarginDiverges (AUDJPY,M1) 2021.11.08 16:52: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.74 EUR

2021.11.08 15:54:36.001 MarginDiverges (AUDJPY,M1) 2021.11.08 16:54: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.68 EUR

2021.11.08 15:56:16.014 MarginDiverges (AUDJPY,M1) 2021.11.08 16:56: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.65 EUR

2021.11.08 15:57:56.022 MarginDiverges (AUDJPY,M1) 2021.11.08 16:57: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.47 EUR

2021.11.08 15:59:36.025 MarginDiverges (AUDJPY,M1) 2021.11.08 16:59: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.49 EUR

2021.11.08 16:01:16.029 MarginDiverges (AUDJPY,M1) 2021.11.08 17:01: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.50 EUR

2021.11.08 16:02:56.024 MarginDiverges (AUDJPY,M1) 2021.11.08 17:02: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.51 EUR

2021.11.08 16:04:36.003 MarginDiverges (AUDJPY,M1) 2021.11.08 17:04: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.49 EUR

2021.11.08 16:06:16.020 MarginDiverges (AUDJPY,M1) 2021.11.08 17:06: AUDJPY Account Margin =   320.65 EUR   <---->   Calc Margin =   320.58 EUR


You see the discrepancy between the account margin and the calculated margin. Can somebody explain this?

Thank you.

Matthias


When you trade on margin, on Forex, the margin is calculated at the opening of the position, and then doesn't change it's perfectly normal. Once the position is closed, the margin is released.

You borrow that amount to open your position : 100,000 AUD and the margin is calculated using EURAUD in your case, when you close the position the 100,000 AUD are freed and so the margin released. I can say your leverage on this position is 1:200 (100,000/1.156xxx / 320.65).

 
Alain Verleyen #:

When you trade on margin, on Forex, the margin is calculated at the opening of the position, and then doesn't change it's perfectly normal. Once the position is closed, the margin is released.

You borrow that amount to open your position : 100,000 AUD and the margin is calculated using EURAUD in your case, when you close the position the 100,000 AUD are freed and so the margin released. I can say your leverage on this position is 1:200 (100,000/1.156xxx / 320.65).

Thanks for your helpful answer. Slowly but surely my understanding is growing...

BTW: the leverage is 1:300, because the margin_rate is 1.5 (not 1.0).

Matthias

Reason: