- Margin % mql4
- Lot Size, Lot Size, Lot Size
- How can I calculate the Margin level percentage?
MQL4/5 adds new "symbol margin percentage" property, but how can I get this value?
Use
Documentation | 2015.12.03 11:38
Use
Thanks for your reply.
But actually, I didn't find SYMBOL_MARGIN_PERCENTAGE enum.
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 :)
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.
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use