Margin and Leverage in MQL4

Forex traders understand well the importance of margin and leverage in trading. After all, high leverage is what attracts most of retail traders to online FX brokers.

When you trade in MetaTrader 4 platform, it is also crucial to know how it displays your free margin and used margin and also how to get account margin and leverage values when creating your own indicators and expert advisors.

In this guide, we talk about margin and leverage in MetaTrader 4 and MQL4.

MetaTrader 4 shows used margin, free margin, and margin level in the Terminal panel — both in the Trade tab and in the Exposure tab.

Here is an example of how the margin display looks in the Trade tab:

MT4 - Margin Display in Trade Tab

And here is an example of how it is in the Exposure tab:

MT4 - Margin Display in Exposure Tab

If you need to use the leverage or margin information in your expert advisor or any other MQL4 program, the language offers some native functions to retrieve these details for you:

  • AccountInfoInteger(ACCOUNT_LEVERAGE) returns the leverage set on your trading account. Leverage for specific trading instruments may sometimes differ from this.
  • AccountInfoDouble(ACCOUNT_MARGIN) returns the used margin for your account.
  • AccountInfoDouble(ACCOUNT_MARGIN_FREE) returns the free margin — the margin that is still available to hold or open positions.
  • AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) returns thee margin level — the account equity divided by its free margin and multiplied by 100.

Margin level is an important parameter as it tells you how much of equity your account has relative to the margin used by your trades:

You need to know this level to avoid margin calls and stop-outs when being overleveraged or holding some large losing trades.

Below is a sample script to show how MQL4 returns these values. It is based on Demo-1 MT4 test script provided in one of the previous guides.

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   Print("Account leverage: 1:", AccountInfoInteger(ACCOUNT_LEVERAGE));
   Print("Account used margin: ", AccountInfoDouble(ACCOUNT_MARGIN));
   Print("Account free margin: ", AccountInfoDouble(ACCOUNT_MARGIN_FREE));
   Print("Account margin level: ", NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL), 2));
}

Executing this script on a demo account with one open position produces the following result (in the Experts tab):

MT4 - Margin and Leverage Information with MQL4 Script

Conclusion

Leverage is a great advantage for the trader, it allows to borrow money from a broker in order to control a bigger capital than the funds deposited. To allow leveraging, brokers have margin requirements, which is a minimum amount of equity to be available on your account to hold or open positions.

MQL4 provides functions to retrieve details about leverage and margin, so that you could use this information in the code of your expert advisors.


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.