How to Disable Trading when the Spread is too Wide

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 article you will see how with some simple MQL4 code you can tell your expert advisor to disable any trading activity if the spread is 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 to use their services, it is the equivalent of a “commission”.

We talk a little more about the spread in this article.

In MetaTrader4 you can see the spread visually in the Market Watch, right clicking it and selecting Spread.

MetaTrader4 - Disable Trading Spread Wide - 1
Show Spreads in the Market Watch MetaTrader4
MetaTrader4 - Disable Trading Spread Wide - 1b
Market Watch with Spreads

Usually brokers use tight spreads to attract traders, a low spread will mean less costs (or more profits) for the traders so this is a big marketing tool. Some brokers offer accounts with zero spread, but this usually are replaced by some other type of commission or cost. As I explained also in the article How to Choose the Best Forex Broker for You the Spread is one of the factors to consider when choosing your broker.

Usually each currency 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 EURUSD pair. There are market situations though that can cause the spread to suddenly increase and as a consequence, your cost can increase!

Some of the reasons why the spread increases can be:

  • Low market liquidity
  • High market volatility
  • Rollover time
  • Unexpected news
  • Reports published

Considering 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 very different from one trader to another.

How to Get the Spread with MQL4

You can see in this article some code to get the spread value with MQL4 and MQL5 language.

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 you are automating your trading and you need a way to understand what is the spread.

There are several ways to get the spread of a currency, for example calculating it using ask-bid. MQL4 provides a very simple function to get the spread of a specific currency and this is the method I prefer.

This function will return the current spread of the current pair expressed in points, so if I run the following

The result is

MetaTrader4 - Disable Trading Spread Wide - 2

So for the pair EURUSD the current Spread is 4.9 pips (I know it is high but I wrote the article on a Sunday and the market was closed).

Disable Trading when the Spread is too Wide

Having the spread with the MarketInfo()  function it is now easy to define a threshold and write a condition that if the spread is below the threshold then an action can be performed.

The following code is only valid for Metatrader 4 MQL4 however you can easily adapt it for Metatrader 5 and MQL5.

With a couple more lines of code you can achieve the following.

And obtain the following result

MetaTrader4 - Disable Trading Spread Wide - 3

MetaTrader4 - Disable Trading Spread Wide - 4

Download The Spread Indicator For MT4 And MT5

If you trade manually and you want to keep the spread monitored you can download the 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 MetaTrader4. You also saw how you can use a simple condition to compare the spread to a limit and continue or not with the program.

I am sure this code will be helpful and will save you some pips.

Should you have any feedback or if you noticed any bug please feel free to comment below.

Please also remember to Follow and Like Us on Social Media!

7 thoughts on “How to Disable Trading when the Spread is too Wide”

  1. I made like this:

    //| teste1.mq4 |
    //| Copyright 2017, MetaQuotes Software Corp. |
    //| https://www.mql5.com |
    //+——————————————————————+

    //+——————————————————————+
    //| 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");
    }
    }
    //+——————————————————————+

    Reply
    • Hi Natan,
      I am not sure I understand well the problem, there might be missing parts…
      The code I provided as example is part of a script, so it runs only once, also you need to replace part of the code with the operation that you want or don’t want to perform. Ideally the code has to be included in an Expert Advisor and not a script

      void OnStart()
      {
      //—
      if(MarketInfo(Symbol(), MODE_SPREAD) Reply

Leave a Comment

We use cookies to ensure that we give you the best experience on our website.