Get Spread Value in MQL4

In this guide, you can learn how to get the current spread value using MQL4 and MQL5 languages.

The spread can be in many cases the reason why a strategy is not profitable. As traders, we always want to trade when the spread is low and we might pass on opportunities when it is too high, especially when scalping. If you code indicators and expert advisors, you need to know how to get the spread value with MQL4 and MQL5 language.

MQL4 Spread Value

Dealing with the spread is something that all traders have to do. Let's start by defining the spread and then we can see some code to get the actual spread value in MQL4.

The spread is the difference between the Ask and the Bid prices. It is basically the commission that you pay the broker for a transaction. Depending on the broker and the account, you can have a fixed or variable spread. A fixed spread stays the same (but is usually associated with slippage). A variable spread changes over time. Usually, in the moments of high volatility, the spread increases.

You might think that getting the spread value via MQL4 code involves calculations and so on, but it isn't the case.

MQL4 — Get Current Spread with MarketInfo

With MQL4, we have the function MarketInfo(), which can easily return the current value of the spread.

The following MQL4 code saves the current spread value for the current instrument in a variable and shows it in a popup window:

void OnStart()
{
   // MQL4 Get Spread Value
   // Get Spread Value with MarketInfo() 
   // and save the value into a variable
   double Spread = MarketInfo(Symbol(), MODE_SPREAD);

   // Showing a message box with the values.
   MessageBox("Spread = " + Spread);
}

MarketInfo() provides many details about the instruments and market, and one of these is, in fact, the spread.

MQL4 and MQL5 — Spread with SymbolInfoDouble

With the version upgrade and the new release of MetaTrader 5, something changed.

MT5 and MQL5 language does not include the MarketInfo() function, so the approach is a little different.

In this case, to use MQL5 to get the spread, you need to use the function SymbolInfoInteger().

SymbolInfoInteger() is available in both MQL4 and MQL5 and returns the details about the specified trading instrument.

To return the spread, we query SymbolInfoInteger() for the property SYMBOL_SPREAD:

void OnStart()
{
   // MQL4/MQL5 Get Spread Value.
   // Get Spread Value with SymbolInfoInteger() 
   // and save the value into a variable
   double Spread = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);

   // Showing a message box with the values.
   MessageBox("Spread = " + Spread);
}

The spread returned in both cases is expressed in points — depending on your broker, those can be pips or pipettes.

Conclusion

As you can see in the code above, it only takes one line of code, one function, to get the spread value via MQL4 or MQL5 language. The task is very easy and quick to perform.

If you are looking for an indicator to see the spread on chart and receive notifications about it, you can use our free MQL4TA Spread Indicator.

We also talk about disabling the trading when the spread is too high in our How to Disable Trading When the Spread is Too Wide guide.


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.