There are times when the spread suddenly increases and, depending on your trading strategy and style, you may want to stop trading when this happens. In this guide, you will see how, with some simple MQL4 code, you can tell your expert advisor to disable any trading activity if the spread becomes too wide.
The Spread
As you should already know the spread is the difference between the Ask price and the Bid price. The spread is the cost that you pay to the broker for using their services. It is most often an equivalent of a "commission" in Forex.
You can learn in more detail about spread in article on spread indicator.
In MetaTrader 4, you can see the spread in the Market Watch panel. You can turn the spread display on by right-clicking it and selecting Spread.
Usually, brokers use tight spreads to attract traders. A low spread will mean less costs (or more profits) for traders, so this is a big marketing tool. Some brokers offer accounts with zero spread, but it is usually replaced by some other type of commission or cost. As explained in our guide on How to Choose the Best Forex Broker for You, spread is one of the factors to consider when choosing your broker.
Most often, each currency pair has its own average spread during trading hours. For example, it is very common to have a spread between 1 and 1.5 pips for the EUR/USD pair. There are market situations, though, that can cause the spread to suddenly increase and, as a consequence, your cost can increase too!
Some of the reasons why the spread increases can be:
- Low market liquidity
- High market volatility
- Rollover time
- Unexpected news
- Reports published
Considering that the spread is a cost for you, you may want to avoid trading when the spread is above a specific threshold. This threshold depends on your trading style and strategies, so it can be vary greatly from one trader to another.
How to Get Spread Value with MQL4?
In our Get Spread Value in MQL4 guide, you can see some code to retrieve the spread value using MQL4 and MQL5 languages.
You know what the spread is and that sometimes is better to stay away from trading when the spread is too high, but what is the problem? The problem is that if you are automating your trading, you need a way to understand what is the spread.
There are several ways to get the spread for a currency pair, for example by calculating it using
MarketInfo(Symbol(), MODE_SPREAD)
This function will return the current spread of the current pair expressed in points, so if I run the following:
//+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Print("The Spread for the pair ", Symbol(), " is currently ", MarketInfo(Symbol(), MODE_SPREAD), " pipettes"); } //+------------------------------------------------------------------+
The result will be something like this:
So, for the EUR/USD currency pair, the current spread was 4.9 pips (that might seem high, but it was retrieved on Sunday when the market was closed).
Disable Trading When Spread Is Too Wide
Having captured the spread value with the MarketInfo()
function, it is now easy to define a threshold and write a condition that will only allo some action to be performed if the spread is below that threshold.
The following code is only valid for MQL4 in MetaTrader 4, however, you can easily adapt it for MQL5 in MetaTrader 5.
With a couple more lines of code you can achieve the following:
//+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ #property strict #property show_inputs extern int MaxSpread = 20; // Max Spread in pipettes. void OnStart() { if (MarketInfo(Symbol(), MODE_SPREAD) < MaxSpread) { Print("I can order"); } else { Print("Cannot order, spread is too high"); } } //+------------------------------------------------------------------+
And obtain the following result:
Download Spread Indicator for MT4 and MT5
If you trade manually and you want to keep the spread monitored, you can download our Spread Indicator for MT4 and MT5.
This indicator allow you to keep an eye on the spread and also receive notifications about it.
Conclusion
In this article you learned how you can quickly get the spread of a pair using MQL4 language in MetaTrader 4. You also saw how you can use a simple condition to compare the spread to a limit and continue or not with the program.
This code can be very helpful and may save you some pips!