SymbolInfoMarginRate: acting as specifications?

 

Dear people,

I spent some times already looking into the forums and documentation and cannot really understand how is it possible that SymbolInfoMarginRate() is not behaving according to specifications. I'm running MT4 Version 5, release 4380. I have compiled a little script (which is very similar to the example in the Mql5 book). Here the code:


//+------------------------------------------------------------------+
//|                                               TestMarginInfo.mq5 |
//+------------------------------------------------------------------+

input bool MarketWatchOnly = true;
input ENUM_ORDER_TYPE OrderType = ORDER_TYPE_BUY;
   
void OnStart()
{
   const int n = SymbolsTotal(MarketWatchOnly);
   PrintFormat("Margin rates per symbol for %s:", EnumToString(OrderType));
   for(int i = 0; i < n; ++i)
   {
      const string s = SymbolName(i, MarketWatchOnly);
      double initial = 1.0, maintenance = 1.0;
      SymbolInfoMarginRate(s, OrderType, initial, maintenance);
      PrintFormat("%4d %s = %f %f", i, s, initial, maintenance);
   }

And here the result after running it:

Output of SymbolInfoMarginRate

You can clearly see that on the Symbols specification tab, for EURUSD (just as an example: it is the same for every Symbol) the maintenance margin is not zero, as loaded into the maintenance variable by the SymbolInfoMarginRate function.


What am I missing here? Thank you so much in advance for the support.

 

I agree, It doesn't seem to be working very well. Like you, I also noticed some discrepancies between the functions and the specifications.

There is another thread about the same subject at https://www.mql5.com/en/forum/460668

I've tried this code in different accounts and the results suggest a bug in margin functions:

int OnInit()
{
   double margin=0, marginIni=0, marginMaint=0;
   bool result=0;
   double lotMin = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   double price = SymbolInfoDouble( _Symbol, SYMBOL_ASK);

   Print("*** Testing margin functions ***");
   Print("Company: ", AccountInfoString(ACCOUNT_COMPANY));
   Print("Currency: ", AccountInfoString(ACCOUNT_CURRENCY));
   Print("Name: ", AccountInfoString(ACCOUNT_NAME));
   Print("Server: ", AccountInfoString(ACCOUNT_SERVER));
   Print("Account trade mode: ", EnumToString((ENUM_ACCOUNT_TRADE_MODE)AccountInfoInteger(ACCOUNT_TRADE_MODE)));
   Print("Symbol: ", _Symbol);
   Print("Symbol Calc Mode: ", EnumToString((ENUM_SYMBOL_CALC_MODE)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_CALC_MODE)) );   
   
   result = OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, lotMin, price, margin);
   Print("OrderCalcMargin() BUY --> margin: ", margin, ", function return: ", result);
   
   result = OrderCalcMargin(ORDER_TYPE_SELL, _Symbol, lotMin, price, margin);
   Print("OrderCalcMargin() SELL --> margin: ", margin, ", function return: ", result);
   
   result = SymbolInfoMarginRate(_Symbol, ORDER_TYPE_BUY, marginIni, marginMaint);
   Print("SymbolInfoMarginRate() --> initial margin: ", marginIni, ", maintenance margin: ", marginMaint, ", function return: ", result);
   
   marginIni = SymbolInfoDouble(_Symbol, SYMBOL_MARGIN_INITIAL);
   Print("SymbolInfoDouble() --> initial margin: ", marginIni);
   
   marginMaint = SymbolInfoDouble(_Symbol, SYMBOL_MARGIN_MAINTENANCE);
   Print("SymbolInfoDouble() --> maintenance margin: ", marginMaint);

   Print("Done, closing EA!");
   
   return(INIT_FAILED);
}

These are some results:

2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	Terminal build: 4380
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	Account trade mode: ACCOUNT_TRADE_MODE_DEMO
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	Symbol: PETR4
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	Symbol Calc Mode: SYMBOL_CALC_MODE_EXCH_STOCKS
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	OrderCalcMargin() BUY --> margin: 0.0, function return: true
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	OrderCalcMargin() SELL --> margin: 0.0, function return: true
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	SymbolInfoMarginRate() --> initial margin: 0.0, maintenance margin: 0.0, function return: true
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	SymbolInfoDouble() --> initial margin: 0.0
2024.06.11 01:26:38.010	MarginCheck (PETR4,M1)	SymbolInfoDouble() --> maintenance margin: 0.0


2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	Terminal build: 4380
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	Account trade mode: ACCOUNT_TRADE_MODE_DEMO
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	Symbol: WDON24
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	Symbol Calc Mode: SYMBOL_CALC_MODE_EXCH_FUTURES
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	OrderCalcMargin() BUY --> margin: 0.0, function return: false
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	OrderCalcMargin() SELL --> margin: 0.0, function return: false
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	SymbolInfoMarginRate() --> initial margin: 0.0, maintenance margin: 0.0, function return: true
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	SymbolInfoDouble() --> initial margin: 148.5152997267
2024.06.11 01:25:31.126	MarginCheck (WDON24,H1)	SymbolInfoDouble() --> maintenance margin: 148.5152997267


2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	Terminal build: 4380
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	Account trade mode: ACCOUNT_TRADE_MODE_REAL
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	Symbol: EURUSD
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	Symbol Calc Mode: SYMBOL_CALC_MODE_FOREX
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	OrderCalcMargin() BUY --> margin: 66.69, function return: true
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	OrderCalcMargin() SELL --> margin: 66.68, function return: true
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	SymbolInfoMarginRate() --> initial margin: 18.0, maintenance margin: 0.0, function return: true
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	SymbolInfoDouble() --> initial margin: 0.0
2024.06.11 01:28:23.392	MarginCheck (EURUSD,M15)	SymbolInfoDouble() --> maintenance margin: 0.0


2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	Terminal build: 4380
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	Account trade mode: ACCOUNT_TRADE_MODE_REAL
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	Symbol: US_2000
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	Symbol Calc Mode: SYMBOL_CALC_MODE_CFD
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	OrderCalcMargin() BUY --> margin: 279.75, function return: true
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	OrderCalcMargin() SELL --> margin: 279.72, function return: true
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	SymbolInfoMarginRate() --> initial margin: 0.1, maintenance margin: 0.0, function return: true
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	SymbolInfoDouble() --> initial margin: 0.0
2024.06.11 01:31:19.965	MarginCheck (US_2000,M15)	SymbolInfoDouble() --> maintenance margin: 0.0


We can see that some margin functions return values ​​for some assets and not others.

Maybe I'm misinterpreting the documentation, but I expected all margin values ​​to be consistent and that the functions returned some value for all asset types (and according to the specifications).

OrderCalcMargin() always returns true and margin 0 - How to fix a problem with OrderCalcMargin in MoneyManagement
OrderCalcMargin() always returns true and margin 0 - How to fix a problem with OrderCalcMargin in MoneyManagement
  • 2024.01.16
  • bred_bred1
  • www.mql5.com
As ordercalcmargin is not debugable i really can't figure out what's going wrong. Oh sorry i confused it with the ordercalcprofit function, you can calculate margin for pending order types, have you used the getlasterror() function to see what error you've received
 
Gustavo Enedir Hennemann #:

I agree, It doesn't seem to be working very well. Like you, I also noticed some discrepancies between the functions and the specifications.

There is another thread about the same subject at https://www.mql5.com/en/forum/460668

I've tried this code in different accounts and the results suggest a bug in margin functions:

You need to fix your code before talking about an MT5 bug. On stock/futures the price required for OrderCalcMargin() is the last price.


Your EURUSD/US_2000 results are correct.


 SymbolInfoDouble(_Symbol, SYMBOL_MARGIN_INITIAL) and SymbolInfoDouble(_Symbol, SYMBOL_MARGIN_MAINTENANCE) are not always used/set. If they are needed and not set, it's a broker issue (it's the broker which configure the symbol's specifications) and not an MT5 one.

 
Fab:

Dear people,

I spent some times already looking into the forums and documentation and cannot really understand how is it possible that SymbolInfoMarginRate() is not behaving according to specifications. I'm running MT4 Version 5, release 4380. I have compiled a little script (which is very similar to the example in the Mql5 book). Here the code:


And here the result after running it:

You can clearly see that on the Symbols specification tab, for EURUSD (just as an example: it is the same for every Symbol) the maintenance margin is not zero, as loaded into the maintenance variable by the SymbolInfoMarginRate function.


What am I missing here? Thank you so much in advance for the support.

From the help :

  • Margin rate — margin rates for various order types are specified in this table. The rates are set for the initial and maintenance margin individually. If no ratio is set for the maintenance margin (set to zero), the initial margin ratio is applied used for it.
 
Alain Verleyen #:

From the help :

  • Margin rate — margin rates for various order types are specified in this table. The rates are set for the initial and maintenance margin individually. If no ratio is set for the maintenance margin (set to zero), the initial margin ratio is applied used for it.

Thank you so much. Got lost in the details up to your pointer.