How can I get symbol's margin percentage?

 
MQL4/5 adds new "symbol margin percentage" property, but how can I get this value?
 
Xiangdong Guo:
MQL4/5 adds new "symbol margin percentage" property, but how can I get this value?

Use

SymbolInfoDouble

SymbolInfoDouble Returns the corresponding property of a specified symbol. There are 2 variants of the function. 1. Immediately returns the property value. double SymbolInfoDouble ( string name, // symbol ENUM_SYMBOL_INFO_DOUBLE prop_id // identifier of the property ); 2. Returns true or false depending on whether a function is successfully performed. In case of success, the value of the property is placed into a recipient variable, passed by reference by the last parameter. bool SymbolInfoDouble ( string name, // symbol ENUM_SYMBOL_INFO_DOUBLE prop_id, // identifier of the property double& double_var // here we accept the property value ); Parameters name [in] Symbol name. prop_id [in] Identifier of a symbol property. The value can be one of the values of the ENUM_SYMBOL_INFO_DOUBLE enumeration. double_var [out] Variable of double type receiving the value of the requested property. Return Value The value of double type. In case of execution failure, information about the error can...

Documentation | 2015.12.03 11:38

 
Karputov Vladimir:

Use

Thanks for your reply.

But actually, I didn't find SYMBOL_MARGIN_PERCENTAGE enum.

 
Xiangdong Guo:
MQL4/5 adds new "symbol margin percentage" property, but how can I get this value?
No such property. The margin is calculated in money, not as a percentage.
 
Karputov Vladimir:
No such property. The margin is calculated in money, not as a percentage.

Looks like you miss the new property of symbol.

Please open Market Watch -> Right click forex symbol -> Specification, then you will find "Margin percentage".

 

 
Xiangdong Guo:

Looks like you miss the new property of symbol.

Please open Market Watch -> Right click forex symbol -> Specification, then you will find "Margin percentage".

 

I do not know where you got this picture. In MetaTrader 5 has no such options :)
 
Vladimir Karputov:
I do not know where you got this picture. In MetaTrader 5 has no such options :)
It is in MT4
 
Xiangdong Guo:
MQL4/5 adds new "symbol margin percentage" property, but how can I get this value?

It looks like this property can not be accessed directly by far, and the only workaround is indirect - reverse calculation. For example, according to CFD margin formula for 1 lot:

Margin (amount) = Contract size * Open market price

On the other hand, the margin for 1 lot can be reteieved as:

Margin (builtin) = MarketInfo(Symbol(), MODE_MARGINREQUIRED)

Hence, the percentage can be found via division:

Margin (percentage) = Margin (builtin) / Margin (amount) * 100

 

Stanislav Korotky's answer is the one I've been looking for, but for anyone else coming to this topic, I thought I'd restate it in an easier-to-understand way.

(and hopefully someone will confirm I'm right or let me know if I'm spreading false information!)

In order to get "leverage" (e.g., 200), all you need is contract_size, price, and required_margin:

[ * I'm in MT4. Also, I'm only familiar Forex and CFD margin calculations types. * ]
double calcLeverage(string symbol) {
   int contractSize = MarketInfo(symbol, MODE_LOTSIZE);
   double reqMarg = MarketInfo(symbol, MODE_MARGINREQUIRED);
   double ask = MarketInfo(symbol, MODE_ASK);
   int lev = round(contractSize * ask / reqMarg);
   return lev;

You can convert to a percentage with 1/leverage * 100.  

***

The Market Watch "Margin percentage" specification seems to be as expected for crypto/metals.  For Forex, however, the value is 100%.

That may be specific to my broker, who allows you to set an Account Leverage value, but it only applies to Forex.  For Crypto, Metals, etc. they offer fixed leverages according to the asset type.  

Account leverage (for Forex):

AccountInfoInteger(ACCOUNT_LEVERAGE)

I'm basing a lot of this on what I read here:  https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants#enum_symbol_calc_mode 

There, I believe leverage is ACCOUNT_LEVERAGE and margin rate is the Margin percentage specification number . 

None of that is very interesting to me.  I say ignore it all and just use the function above.

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: