SymbolInfoMarginRate - I can't understand it

 

Hi,

I'm trying to find how much margin is required to open a buy a standard lot of different forex pairs/share CFDs/ etc.in MQL5.

I think I might be heading in the right direction, but there's a good chance I'm miles out.

Anyhow ......

I have trolled these forums and googled until my mouse finger is sore trying to find a simple practical value of how to use this .....

bool  SymbolInfoMarginRate(
   string             name,                     // symbol name
   ENUM_ORDER_TYPE    order_type,               // order type
   double&            initial_margin_rate,      // initial margin rate
   double&            maintenance_margin_rate   // maintenance margin rate
   );

Yes, I have read the manual, but I simply can't understand how it works.

Please ... could somebody point me in the right direction by showing me how to use this in a script, perhaps to determine the margin rate to buy a single lot of any pair you'd like to choose.

A simple practical example would be greatly appreciated, and than I could build on that.

Thanks in advance :)

 

Hi

I think you can read about it here, find ENUM_SYMBOL_CALC_MODE and read

https://www.mql5.com/en/docs/constants/environment_state/marketinfoconstants

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

So far I have this:

double GetMarginRequired(string symbol, double lotSize)
{
    double marginRequired = 0.0;

    // Retrieve margin requirements for the symbol
    if (SymbolInfoDouble(symbol, SYMBOL_MARGIN_INITIAL, marginRequired))
    {
        // Calculate margin required for 1 lot
        marginRequired *= lotSize;
    }
    else
    {
        // Failed to retrieve margin requirements
        Print("Failed to retrieve margin requirements for symbol: ", symbol);
    }

    return marginRequired;
}


void OnStart()
{
    double marginRequired = GetMarginRequired("AUDUSD.a", 1);
    Print(marginRequired); 
}

But it doesn't work, the answer is always 0.

 
Graham from Brisbane #:

So far I have this:

But it doesn't work, the answer is always 0.

Hi Graham,

I've only moved to MT5 this year (so I could actually backtest my multicurrency EA). Solid learning curve in the transition from MT4 to MT5 methods.

I think I found same thing you did, where the method closest to what MQL4 provided doesn't seem to work in MT5.

I ended up writing my own function to get that info using other MQL5 functions.

double
Get1LotMarginRequired(string aSymbol)
//  Calculates the free margin needed to be able to place a trade of 1.0 lot in size.
//  This is needed when calculating the lot size for a new trade.
{
    double  dMargin = 0.0;

    if (!OrderCalcMargin(ORDER_TYPE_BUY, aSymbol, 1.0, lastTick.ask, dMargin))
    {
        Print("Get1LotMarginRequired: OrderCalcMargin failed to calculate margin required for a 1 lot ", aSymbol," trade! GetLastError(): ", GetLastError());
    }
    return dMargin;
}

The lastTick variable is an MqlTick variable I update elsewhere before calling this function as part of lot size calculations.

Seems to work well for my uses, though I've only tested it on forex pairs. (the 28 pairs of the 8 main forex currencies)

Cheers from Sydney!

 
double GetMarginRequired2( const string Symb )
{
  const double MinVolume = ::SymbolInfoDouble(Symb, SYMBOL_VOLUME_MIN);
  double Margin;
              
  return(::OrderCalcMargin(ORDER_TYPE_BUY, Symb, MinVolume, ::SymbolInfoDouble(Symb, SYMBOL_ASK), Margin)
          ? (MinVolume ? Margin / MinVolume : 0) : 0);  
}

double GetMarginRequired( const string Symb )
{  
  MqlTick Tick;
  double MarginInitRate, MarginMainRate, MarginInfo;

  return(((::SymbolInfoInteger(Symb, SYMBOL_TRADE_CALC_MODE) == SYMBOL_CALC_MODE_FOREX) &&
          ::SymbolInfoTick(Symb, Tick) &&
          ::SymbolInfoMarginRate(Symb, ORDER_TYPE_BUY, MarginInitRate, MarginMainRate) && MarginInitRate &&
          (bool)(MarginInfo = ::MathMax(::SymbolInfoDouble(Symb, SYMBOL_MARGIN_INITIAL), ::SymbolInfoDouble(Symb, SYMBOL_MARGIN_HEDGED))))
            ? MarginInitRate * Tick.ask * ::SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE) *
              (MarginInfo / ::SymbolInfoDouble(Symb, SYMBOL_TRADE_CONTRACT_SIZE)) /
              (::SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_SIZE) * ::AccountInfoInteger(ACCOUNT_LEVERAGE))
            : GetMarginRequired2(Symb));
}
 

Thanks for the replies, everybody. I've got a base to work from now, and I'll add to this post when I get a result. The move from MQL4 to MQL5 has been quite an experience, especially for a 75 year  old with no formal programing training. However, we will persevere :)

 
Graham from Brisbane #Thanks for the replies, everybody. I've got a base to work from now, and I'll add to this post when I get a result. The move from MQL4 to MQL5 has been quite an experience, especially for a 75 year  old with no formal programing training. However, we will persevere :)

Cheer up Graham! My congratulations for taking it in such a good mood.

Anything you need let us know.

Edit: I leave you here an article that I am sure will help you: https://www.mql5.com/en/articles/81

There you will find most of the equivalences from MQL4 to MQL5.

Migrating from MQL4 to MQL5
Migrating from MQL4 to MQL5
  • www.mql5.com
This article is a quick guide to MQL4 language functions, it will help you to migrate your programs from MQL4 to MQL5. For each MQL4 function (except trading functions) the description and MQL5 implementation are presented, it allows you to reduce the conversion time significantly. For convenience, the MQL4 functions are divided into groups, similar to MQL4 Reference.
 
Graham from Brisbane #:

Thanks for the replies, everybody. I've got a base to work from now, and I'll add to this post when I get a result. The move from MQL4 to MQL5 has been quite an experience, especially for a 75 year  old with no formal programing training. However, we will persevere :)

No probs. The MQL4 to MQL5 change is quite large in some aspects, & finding new methods to match the old ones can be a challenge. Took me a bit to find my 1 lot margin required method after finding SYMBOL_MARGIN_INITIAL not working for me.

The biggest changes for me have been the very different trading methods, & accessing indicator values (including custom indicators with *many* buffers, which I *needed* to have work for my EA given it is based on one I've written). I made my own trade class to give easier trade management for my needs, & now have a decent handle on indicator access. (yes, pun intended ;-)

My biggest issue now is a major Strategy Tester optimisation performance problem after a recent unplanned hardware upgrade (after frying my trusty 12 year old i5 2500k), but I won't hijack this thread with that issue.

Having had formal computer science training myself, you're doing well to get programs working first in MQL4, then the major switch to MQL5 methods, without formal programming training. It's a big challenge, but if we get it right then there are some nice benefits to have after all the hard work.

Reason: