Features of the mql5 language, subtleties and tricks - page 9

 
fxsaber:
The solution came immediately.
// Размер свободных средств, необходимых для открытия 1 лота на покупку
double GetMarginRequired( const string Symb )
{
  MqlTick Tick;

  return(SymbolInfoTick(Symb, Tick) ? Tick.ask * SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE) * SymbolInfoDouble(Symb, SYMBOL_TRADE_CONTRACT_SIZE) *
         SymbolInfoDouble(Symb, SYMBOL_POINT) / (SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_SIZE) * AccountInfoInteger(ACCOUNT_LEVERAGE)) : 0);
}
This is a calculation for FOREX. The rest - by analogy.
Great, now we just need to bring margin in the currency of deposit, because the margin, for example, for AUDCAD with the currency of deposit USD should be in USD.
 
Alexey Volchanskiy:
Great, it remains to bring the margin in the currency of the deposit, because the margin, for example, for AUDCAD with the currency of the deposit USD should be in USD.
It was done.
 
Alexey Volchanskiy:
Great, the only thing left is to bring the margin in the currency of deposit, because the margin, for example for AUDCAD with the currency of deposit USD should be in USD.
SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE ) seems to be in the function just for this... to bring lots to the currency of deposit
 
fxsaber:
This is what was done.

Everything works great in forex, but the only remaining question is "why half as much, and how to fix it?"

 
Vitaly Muzichenko:

All works great in forex, but the last question remains: "Why is it half as much, and how to fix it?

I have encountered the fact that the margin on SGDJPY is several times different on FIBOGroup-MT5 Server and MetaQuotes-Demo. This suggests that MT5 itself calculates the margin with errors. And since it is so, there is no benchmark for comparison. That is why it is impossible to say for sure if this is my error or that of the developers. I think there is an error in both cases. The problem is the lack of correct information.
 
fxsaber:
Faced with the fact that on SGDJPY the margin is times different on FIBOGroup-MT5 Server and MetaQuotes-Demo. This suggests that MT5 itself calculates the margin with errors. And since it is so, there is no benchmark for comparison. That is why it is impossible to say for sure if this is my error or that of the developers. I think there is an error in both cases. The problem is the lack of correct information.

My two servers show correctly, with one exception - highlighted in red

How do I find out the property highlighted in red?

Thank you!

 
Vitaly Muzichenko:

I have two servers showing correctly, with one exception - highlighted in red

How to know the property in red?

Thanks for the tip! In the wilderness it isSymbolInfoMarginRate. So now it's like this
// Размер свободных средств, необходимых для открытия 1 лота на покупку
double GetMarginRequired( const string Symb )
{
  MqlTick Tick;
  double MarginInit, MarginMain;

  return((SymbolInfoTick(Symb, Tick) && SymbolInfoMarginRate(Symb, ORDER_TYPE_BUY, MarginInit, MarginMain)) ? MarginInit * Tick.ask *
          SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE) / (SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_SIZE) * AccountInfoInteger(ACCOUNT_LEVERAGE)) : 0);
}

You have to clearly understand that in MT5 there may be very different margin requirements in different directions. That is, a single MT4 variant may not work. On Forex, of course, this will not be the case. But you have to remember. Therefore, in the general case it should be written like this
// Альтернатива OrderCalcMargin
bool MyOrderCalcMargin( const ENUM_ORDER_TYPE action, const string symbol, const double volume, const double price, double &margin )
{
  double MarginInit, MarginMain;

  const bool Res = SymbolInfoMarginRate(symbol, action, MarginInit, MarginMain);
  
  margin = Res ? MarginInit * price * volume * SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE) /
                 (SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE) * AccountInfoInteger(ACCOUNT_LEVERAGE)) : 0;
  
  return(Res);  
}
 
fxsaber:
That's what was done.
Yeah, that's right, that's right.
 

MT5 joke on FIBOGroup-MT5 Server SGDJPY

In this situation MyOrderCalcMargin counts correctly, but the regular OrderCalcMargin does not!

 
fxsaber:

MT5 prank on FIBOGroup-MT5 Server SGDJPY

In this situation MyOrderCalcMargin counts correctly, but the regular OrderCalcMargin does not!

Is there any way to calculate the hedged margin?