how to convert MarketInfo(Symbol(), MODE_MARGINREQUIRED) to mt5 version
It is a little more complicate in MT5 because it can operate on several different markets, not just Forex, where the calculation of Margin can be different on each one.
Instead, you can use the OrderCalcMargin() function!
bool OrderCalcMargin(
ENUM_ORDER_TYPE action, // type of order
string symbol, // symbol name
double volume, // volume
double price, // open price
double& margin // variable for obtaining the margin value
);
thanks for your reply. but OrderCalcMargin return a bool, I need a double
Read the documentation again!
bool OrderCalcMargin(
ENUM_ORDER_TYPE action, // type of order
string symbol, // symbol name
double volume, // volume
double price, // open price
double& margin // variable for obtaining the margin value
);
margin
[out] The variable, to which the value of the required margin will be written in case the function is successfully executed. The calculation is performed as if there were no pending orders and open positions on the current account. The margin value depends on many factors, and can differ in different market environments.
// Размер свободных средств, необходимых для открытия 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);
}
// Альтернатива 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);
}
According to ENUM_SYMBOL_CALC_MODE documentation, I’m not sure that margin can be calculated via current price unconditionally as in the presented code - there are cases when formulae differ.
It is necessary that someone checked.
It can’t be. Depends of the symbol : Forex (yes), Futures (No)…