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!
OrderCalcMargin
The function calculates
the margin required for the specified order type, on the current
account, in the current market environment not taking into account
current pending orders and open positions. It allows the evaluation of
margin for the trade operation planned. The value is returned in the
account currency.
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 );
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!
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.
Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий
Особенности языка mql5, тонкости и приёмы работы
fxsaber, 2017.02.27 18:40
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);
}
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.
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.
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.
Retourne la marge dans la devise du compte pour effectuer une opération de trading spécifiée. It's double.
order_calc_margin( |
Paramètres
action
[in] Type de l'ordre dont les valeurs proviennent de l'énumération ORDER_TYPE. Paramètre sans nom requis.
symbol
[in] Nom de l'instrument financier. Paramètre non nommé requis.
volume
[in] Volume de l'opération de trading. Paramètre non nommé requis.
price
[in] Prix d'ouverture. Paramètre non nommé requis.
Valeur de Retour
Valeur réelle en cas de succès, sinon None. Les informations sur l'erreur peuvent être obtenues en utilisant last_error().
EXEMPLE: |
mt5-include/Trade/AccountInfo.mqh
//+------------------------------------------------------------------+ |
//| Access functions OrderCalcMargin(...). | |
//| INPUT: name - symbol name, | |
//| trade_operation - trade operation, | |
//| volume - volume of the opening position, | |
//| price - price of the opening position. | |
//+------------------------------------------------------------------+ |
double CAccountInfo::MarginCheck(const string symbol,const ENUM_ORDER_TYPE trade_operation, |
const double volume,const double price) const |
{ |
double margin=EMPTY_VALUE; |
//--- |
if(!OrderCalcMargin(trade_operation,symbol,volume,price,margin)) |
return(EMPTY_VALUE); |
//--- |
return(margin); |
- gsemet
- github.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
how to convert MarketInfo(Symbol(), MODE_MARGINREQUIRED) to mt5 version