# Calculation of real leverage? (solved)

796
2014.04.13 10:17

Good morning,

I hope you are fine. I have been having quite a problem calculating the real leverage used by the trading account. We can define real leverage as:

`double real_leverage =  {total value of the open trades}/AccountEquity();`

I am using the SymbolInfoDouble function to calculate the value of open trades. This is what I have so far:

```double GetRealLeverageInUse()
{
double exposure   = 0;
double equity    = AccountEquity();
if(equity <= 0) return(0);
for(int i = OrdersTotal()-1; i >= 0; i--)
{
{
string symbol = OrderSymbol();
double lots   = OrderLots();
double contract_size = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE); // NOT IN BASE CURRENCY
exposure += contract_size * lots;
}
}
return(exposure/equity);
}```

But since the base currency of the instruments used is not the same, my real leverage is not properly calculated. The SymbolInfoDouble function returns, for example...

• 1Lot of USDCAD is 100,000USD
• 1Lot of AUDUAS is 100,000AUD
• 1lot of XAUUSD is 100 ounzes of gold

The nasty work-around would be reading the counter-currency using SubStr and use the different pairs available to calculate the value of contract. This solution of course can be quite a problem because the symbol name, which is an arbitrary string, can change from broker to broker: fxEURUSD, EURUSDfx, EURUSDm or EURUSDf are different names I have seen. It is quite problematic, does anyone have a more elegant solution?

1401
2014.04.13 11:32
You know your account leverage when you open your account, right? Why need to calculate it separately?
796
2014.04.13 11:34
deysmacro:
You know your account leverage when you open your account, right? Why need to calculate it separately?

I know the max leverage I am allowed to use, not the one I am currently using. For example, I might be allowed to use 1:500 but want to use only 1:5.

Even more, I might want to decrease my lotsizes as I approach that number, or I might want to control exposure changes due to currency fluctuations.

That is why knowing the real leverage used in the account is important.

14753
2014.04.13 11:37
`double real_leverage =  {total value of the open trades}/AccountEquity();`

order value = (OrderClosePrice - OrderOpenPrice)*DIR * OrderLots * DeltaValuePerLot( OrderSymbol )

Even more, I might want to decrease my lotsizes as I approach that number
To prevent a margin call you must verify that there will be free margin at MAE. Contract Size - MQL4 forum
1401
2014.04.13 11:48
Aren't free margin required showed when you open any buy/sell orders? Can't we just use that?
796
2014.04.13 12:08
WHRoeder:

order value = (OrderClosePrice - OrderOpenPrice)*DIR * OrderLots * DeltaValuePerLot( OrderSymbol )

To prevent a margin call you must verify that there will be free margin at MAE. Contract Size - MQL4 forum

Hi @WHRoeder,

Thanks for your reply, it has been very useful. Kindly note I want the total trade size, not the current value of the trades. This is how I have coded the two functions:

```/**
* Returns the total leverage used in the account at the time
* @return   double
*/
double GetRealLeverageInUse2()
{
double exposure     = 0;
double equity       = AccountEquity();
string basecurrency = AccountCurrency();
if(equity <= 0) return(0);
for(int i = OrdersTotal()-1; i >= 0; i--)
{
{
// Pair
string symbol = OrderSymbol();

double lots   = OrderLots();

// Contract size in currency
Print(lots +" of "+ symbol +" are "+ size_in_currency_base +" in size ("+ currency_base_for_trade +"), "+
"and "+ trade_size_in_local_currency +" in base currency ("+ basecurrency +")");

}
}
return(exposure/equity);
}

/**
* Returns the base currency value per lot
* @param    string   pair
* @return   double
*/
double DeltaValuePerLot(string pair="")
{
if(pair == "") pair = Symbol();
return(MarketInfo(pair, MODE_TICKVALUE)/MarketInfo(pair, MODE_TICKSIZE));
}```

However, it does not seem to work as it should. When I backtest EURUSD with an EUR account...

When I backest EURUSD with and USD account...

And when I backtest USDCHF with an USD account...

I believe these numbers are not right.

0.02 lots of EURUSD are always 2,000€ of trade size on an EUR accont.

0.02 lots of USDCHF are always 2,000\$ of trade size on an USD account.

And the trade size for 0.02 lots of EURUSD on the USD account should be around 2,700\$ instead of 1,400\$.

Am I doing something wrong? Maybe DeltaValuePerLot() needs a change? Kindly clarify =)

A.

796
2014.04.13 12:18
deysmacro:
Aren't free margin required showed when you open any buy/sell orders? Can't we just use that?

No. Suppose that the broker gives you a leverage of 1:500 but you have a conservative investor who wants to use only 1:3.

This forces you to calculate the leverage currently used and stop trading there, even if you have plenty of free margin left.

Moderator
9640
2014.04.13 13:18

Are you over-complicating the thinking here?

I have no open orders so I cannot test, but I think that this should do the job

```   double equity =AccountInfoDouble(ACCOUNT_EQUITY);
double margin_used =AccountInfoDouble(ACCOUNT_MARGIN);
int leverage =(int)AccountInfoInteger(ACCOUNT_LEVERAGE);
double real_leverage = margin_used/equity*leverage;
Print(real_leverage);
```

Please forgive me if I am wrong :)

796
2014.04.13 14:35
GumRai:

Are you over-complicating the thinking here?

I have no open orders so I cannot test, but I think that this should do the job

Please forgive me if I am wrong :)

Hi GumRai,

Thanks for your message. You were right, I was over-complicating things =D This works. The other issue now is that I do not want to consider hedging in my leverage calculation.

I will update the functions and post it. Thanks!