Download MetaTrader 5

Calculation of real leverage? (solved)

To add comments, please log in or register
Point Zero
794
Point Zero  

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--)
   {
                if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                { 
                   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?

Thanks in advance!

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
You know your account leverage when you open your account, right? Why need to calculate it separately?
Point Zero
794
Point Zero  
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.

whroeder1
14779
whroeder1  
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
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
Aren't free margin required showed when you open any buy/sell orders? Can't we just use that?
Point Zero
794
Point Zero  
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--)
        {
                if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                { 
                   // Pair 
                   string symbol = OrderSymbol();
                   
         // Lots traded
                   double lots   = OrderLots();
                   
                   // Contract size in currency
                   double size_in_currency_base        = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE)*lots;
                   string currency_base_for_trade      = SymbolInfoString(symbol, SYMBOL_CURRENCY_BASE);
                   double trade_size_in_local_currency = DeltaValuePerLot(symbol)*lots;
                   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 +")");
   
         // Add to exposure
                   exposure += trade_size_in_local_currency;
      }
   }
   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 =)

Thanks in advance =)

A.

Point Zero
794
Point Zero  
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.

Keith Watford
Moderator
9641
Keith Watford  

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 :)

Point Zero
794
Point Zero  
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!

To add comments, please log in or register