Determining Pair Leverage

 

Hello,

I came across an issue calculating the Margin Required for GBPUSD. MT4 says I need $26.42 to open 0.01 Lots, but when I open that position it uses $66.06.

I got in touch with Oanda, and they informed me that after Brexit the maximum leverage on GBPUSD for an account in the USA is 20:1, which would explain the margin difference.

That's all well and good, but if my Account Leverage is 50:1, how do I automatically determine if the leverage on a given pair is different in MQL4?

I can hard code this into my calculation now, but ideally it can be calculated automatically so I don't have to be kept abreast of any changes my broker or US regulators make.

Can anyone help me solve this one?

All the best,
Fred

 
Frederick Langemark:

Hello,

I came across an issue calculating the Margin Required for GBPUSD. MT4 says I need $26.42 to open 0.01 Lots, but when I open that position it uses $66.06.

I got in touch with Oanda, and they informed me that after Brexit the maximum leverage on GBPUSD for an account in the USA is 20:1, which would explain the margin difference.

That's all well and good, but if my Account Leverage is 50:1, how do I automatically determine if the leverage on a given pair is different in MQL4?

I can hard code this into my calculation now, but ideally it can be calculated automatically so I don't have to be kept abreast of any changes my broker or US regulators make.

Can anyone help me solve this one?

All the best,
Fred

Your broker will have a list of the margin requirements. Basically the calculation goes like this : take how much you would like to trade in your account currency and convert it to the currency on the left hand side of the symbol, multiply that by the margin percent required, that is how much you'll have to tie up from your balance to hold the trade open. I don't know where you're seeing MT4 tell you how much you'll need to open a 0.01 lot trade, I'm assuming you have a custom indicator that isn't working.

0.01 lots = 1000 units.
1000 GBP is currently 1320.48 USD
margin requirement of 5% aka 20:1 leverage
1320.48 * 0.05 = 66.024 USD required for margin at the current GBPUSD exchange rate. 

https://docs.mql4.com/constants/environment_state/marketinfoconstants has information on getting margin requirements from mql code, but it gives them to you in amounts for 1 lot traded. I've attached a script that pulls the relevant lines from the example for you. You'd just multiply the relevant number by your lot size to get the info you want.

Symbol Properties - Environment State - Standard Constants, Enumerations and Structures - MQL4 Reference
Symbol Properties - Environment State - Standard Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
A zero value of MODE_STOPLEVEL means either absence of any restrictions on the minimal distance for Stop Loss/Take Profit or the fact that a trade server utilizes some external mechanisms for dynamic level control, which cannot be translated in the client terminal. In the second case, GetLastError() can return error 130, because MODE_STOPLEVEL...
Files:
blah.mq4  2 kb
 
Matthew Colter:

Your broker will have a list of the margin requirements. Basically the calculation goes like this : take how much you would like to trade in your account currency and convert it to the currency on the left hand side of the symbol, multiply that by the margin percent required, that is how much you'll have to tie up from your balance to hold the trade open. I don't know where you're seeing MT4 tell you how much you'll need to open a 0.01 lot trade, I'm assuming you have a custom indicator that isn't working.

0.01 lots = 1000 units.
1000 GBP is currently 1320.48 USD
margin requirement of 5% aka 20:1 leverage
1320.48 * 0.05 = 66.024 USD required for margin at the current GBPUSD exchange rate. 

https://docs.mql4.com/constants/environment_state/marketinfoconstants has information on getting margin requirements from mql code, but it gives them to you in amounts for 1 lot traded. I've attached a script that pulls the relevant lines from the example for you. You'd just multiply the relevant number by your lot size to get the info you want.

Hi Matthew,

Thank you for the reply. I understand the margin calculation very well. I also know my broker has a list of the margin requirements. What I am looking for is a way in MQL4 to determine the actual leverage for a pair.

Even the script you provided says $26.41 required, instead of the $66.06 it actually uses.

Is there something built into MQL4, like AccountLeverage(), that will give me the leverage for the pair?

All the best,
Fred

 

I know this is an old thread, and sorry to bring it back, but I got curious about this again.

I coded in the margin requirements based on my broker, but this wouldn't necessarily translate to someone I shared my code with. For example, I set it up to check the Account Currency, and apply the requirements based on the currency, however I'm sure you could have an account currency of USD with a broker that wasn't subjected to the restrictions.

Is there a way to programmatically get the margin requirements for a pair for that broker? Or even a way to check if the broker is subject to margin restrictions? Or is this just an area where MT4 has not caught up with the regulations?

 
double getLeverage() {
   string   thisCurrency   =  AccountCurrency();
   string   thisSymbol     =  Symbol();
   double   acctLeverage   =  AccountLeverage();
   double   maxLeverage    =  acctLeverage;
   
   if(thisCurrency == "CAD") {
      if(thisSymbol == "TRYJPY" || thisSymbol == "ZARJPY") {
         maxLeverage =  3.4;
      }
      
      if(thisSymbol == "CHFZAR" || thisSymbol == "EURTRY" || thisSymbol == "EURZAR" || thisSymbol == "GBPZAR") {
         maxLeverage =  3.6;
      }
      
      if (thisSymbol == "USDCNH" || thisSymbol == "USDINR" || thisSymbol == "USDTHB" || thisSymbol == "USDTRY" || thisSymbol == "USDZAR") {
         maxLeverage =  3.7;
      }
      
      if(thisSymbol == "SGDHKD") {
         maxLeverage =  5;
      }
      
      if(thisSymbol == "HKDJPY" || thisSymbol == "SGDJPY") {
         maxLeverage =  7.1;
      }
      
      if(thisSymbol == "AUDHKD" || thisSymbol == "AUDSGD" || thisSymbol == "CHFHKD" || thisSymbol == "EURCZK" || thisSymbol == "EURDKK" ||
         thisSymbol == "EURHKD" || thisSymbol == "EURHUF" || thisSymbol == "EURPLN" || thisSymbol == "EURSGD" || thisSymbol == "GBPHKD" ||
         thisSymbol == "GBPPLN" || thisSymbol == "GBPSGD" || thisSymbol == "NZDHKD" || thisSymbol == "NZDSGD" || thisSymbol == "SGDCHF") {
         maxLeverage =  7.7;
      }
      
      if(thisSymbol == "USDCZK" || thisSymbol == "USDDKK" || thisSymbol == "USDHKD" || thisSymbol == "USDHUF" || thisSymbol == "USDPLN" ||
      thisSymbol == "USDSAR" || thisSymbol == "USDSGD") {
         maxLeverage =  8.2;
      }
      
      if(thisSymbol == "CADHKD" || thisSymbol == "CADSGD") {
         maxLeverage =  10;
      }
      
      if(thisSymbol == "USDMXN") {
         maxLeverage =  12.5;
      }
      
      if(thisSymbol == "AUDJPY" || thisSymbol == "CHFJPY" || thisSymbol == "EURJPY" || thisSymbol == "GBPJPY" || thisSymbol == "NZDJPY") {
         maxLeverage =  14.3;
      }
      
      if(thisSymbol == "USDJPY") {
         maxLeverage =  16.1;
      }
      
      if(thisSymbol == "AUDCHF" || thisSymbol == "AUDNZD" || thisSymbol == "EURAUD" || thisSymbol == "EURCHF" || thisSymbol == "EURGBP" ||
         thisSymbol == "EURNOK" || thisSymbol == "EURNZD" || thisSymbol == "EURSEK" || thisSymbol == "GBPAUD" || thisSymbol == "GBPCHF" ||
         thisSymbol == "GBPNZD" || thisSymbol == "NZDCHF") {
         maxLeverage =  16.7;
      }
      
      if(thisSymbol == "AUDUSD" || thisSymbol == "EURUSD" || thisSymbol == "GBPUSD" || thisSymbol == "NZDUSD" || thisSymbol == "USDCHF" ||
      thisSymbol == "USDNOK" || thisSymbol == "USDSEK") {
         maxLeverage =  19.2;
      }
      
      if(thisSymbol == "GBPCAD") {
         maxLeverage =  20;
      }
      
      if(thisSymbol == "CADJPY") {
         maxLeverage =  25;
      }
      
      if(thisSymbol == "AUDCAD" || thisSymbol == "CADCHF" || thisSymbol == "EURCAD" || thisSymbol == "NZDCAD") {
         maxLeverage =  33.3;
      }
      
      if(thisSymbol == "USDCAD") {
         maxLeverage =  45.5;
      }
   }
   
   if(thisCurrency == "EUR" || thisCurrency == "GBP") {
      if(thisSymbol == "CADCHF" || thisSymbol == "CADJPY" || thisSymbol == "EURCAD" || thisSymbol == "EURCHF" ||
         thisSymbol == "EURGBP" || thisSymbol == "EURJPY" || thisSymbol == "EURUSD" || thisSymbol == "GBPCAD" ||
         thisSymbol == "GBPCHF" || thisSymbol == "GBPJPY" || thisSymbol == "GBPUSD" || thisSymbol == "USDCAD" ||
         thisSymbol == "USDCHF" || thisSymbol == "USDJPY") {
         maxLeverage =  30;
      } else {
         maxLeverage =  20;
      }
   }
   
   if(thisCurrency == "USD") {
      if(thisSymbol == "USDMXN") {
         maxLeverage =  12.5;
      }
      
      if(thisSymbol == "AUDHKD" || thisSymbol == "AUDSGD" || thisSymbol == "CADHKD" || thisSymbol == "CADSGD" || thisSymbol == "CHFHKD" ||
         thisSymbol == "CHFZAR" || thisSymbol == "EURCZK" || thisSymbol == "EURGBP" || thisSymbol == "EURHKD" || thisSymbol == "EURHUF" ||
         thisSymbol == "EURPLN" || thisSymbol == "EURSGD" || thisSymbol == "EURTRY" || thisSymbol == "EURZAR" || thisSymbol == "GBPAUD" ||
         thisSymbol == "GBPCAD" || thisSymbol == "GBPCHF" || thisSymbol == "GBPHKD" || thisSymbol == "GBPJPY" || thisSymbol == "GBPNZD" ||
         thisSymbol == "GBPPLN" || thisSymbol == "GBPSGD" || thisSymbol == "GBPUSD" || thisSymbol == "GBPZAR" || thisSymbol == "HKDJPY" ||
         thisSymbol == "NZDHKD" || thisSymbol == "NZDSGD" || thisSymbol == "SGDCHF" || thisSymbol == "SGDHKD" || thisSymbol == "SGDJPY" ||
         thisSymbol == "TRYJPY" || thisSymbol == "USDCNH" || thisSymbol == "USDCZK" || thisSymbol == "USDHKD" || thisSymbol == "USDHUF" ||
         thisSymbol == "USDPLN" || thisSymbol == "USDSAR" || thisSymbol == "USDSGD" || thisSymbol == "USDTHB" || thisSymbol == "USDTRY" ||
         thisSymbol == "USDZAR" || thisSymbol == "ZARJPY") {
         maxLeverage =  20;
      }
      
      if (thisSymbol == "AUDJPY" || thisSymbol == "CADJPY" || thisSymbol == "CHFJPY" || thisSymbol == "EURJPY" || thisSymbol == "NZDJPY" ||
         thisSymbol == "USDJPY") {
         maxLeverage =  25;
      }
      
      if(thisSymbol == "AUDCAD" || thisSymbol == "AUDCHF" || thisSymbol == "AUDNZD" || thisSymbol == "AUDUSD" || thisSymbol == "CADCHF" ||
         thisSymbol == "EURAUD" || thisSymbol == "EURCHF" || thisSymbol == "EURNOK" || thisSymbol == "EURNZD" || thisSymbol == "EURSEK" ||
         thisSymbol == "NZDCAD" || thisSymbol == "NZDCHF" || thisSymbol == "NZDUSD" || thisSymbol == "USDCHF" || thisSymbol == "USDNOK" ||
         thisSymbol == "USDSEK") {
         maxLeverage =  33.3;
      }
      
      if(thisSymbol == "EURCAD" || thisSymbol == "EURDKK" || thisSymbol == "EURUSD" || thisSymbol == "USDCAD" || thisSymbol == "USDDKK") {
         maxLeverage =  50;
      }
   }
   
   if(acctLeverage > maxLeverage) {
      acctLeverage   =  maxLeverage;
   }
   
   return(acctLeverage);
}
 
If you are worried about margin and leverage, you are not controlling your risk. Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account. Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.
  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
Frederick Langemark:

Is there a way to programmatically get the margin requirements for a pair for that broker?

In short, no. Two main reasons:

  • Depending on many factors including regulatory jurisdiction, margin may be calculated either per ticket or per position (net). MT4 doesn't tell you which the broker is applying.
  • Brokers may have tiered margin requirements which aren't covered by the metadata which MT4 provides.
 
William Roeder:
If you are worried about margin and leverage, you are not controlling your risk. Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account. Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.
  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

Hi William. I am controlling my risk, and am not worried about margin, but it would be nice to have a method for accurately calculating how much margin would be used on a position. I trade many pairs and so I often have multiple positions open, so I need to know that I have enough margin to open an additional position. The code I posted allows me to calculate it accurately, but I was merely asking if MQL4 has a built in method for determining this. It would seem the answer is no. But, thank you for your input.

 
JC:

In short, no. Two main reasons:

  • Depending on many factors including regulatory jurisdiction, margin may be calculated either per ticket or per position (net). MT4 doesn't tell you which the broker is applying.
  • Brokers may have tiered margin requirements which aren't covered by the metadata which MT4 provides.

This is what I figured. Thank you JC.