Topic: Protection - Maximum Spread

Hello Traders,

If an Expert advisor is designed to work on a "normal" market, it may not trade well when something unusual happens (like unexpected brexit). Fortunately, it is relatively easy and safe to detect such unexpected market event by monitoring the spread.

Here is how to set a Maximum Spread protection to the expert advisors:

Expert Advisor for MT4

1. Add a Maximum Spread parameter to the expert.

Open the EA in the editor and the following line below the Magic_Number input parameter. It sets default spread protection 25 points. You can change the value in the code or when you run the EA.

static input int Maximum_Spread=25; // Maximum Spread (points)

The code looks like that:

2. Add a check for the market spread in the ManageOpen function. The following code will check the spread and if it is higher than the input parameter, the "return" command will terminate the position opening.


The code will look like that:

Expert Advisor for MT5

1. Add a Maximum Spread parameter to the expert below the Magic_Number input parameter:

static input int Maximum_Spread=25; // Maximum Spread (points)

2. Add the following check in the ManageOpen function.

   MqlTick tick;

Re: Protection - Maximum Spread

Hi Popov,

This spread level protection function is already not available? I exported the MT4 EA and opened it to edit, but these texts i dont see in the code. Should i add it same as like here? Or something changed and already not available this function?

Re: Protection - Maximum Spread

Hello Santaferes,

These are modifications you have to made alone.

The code exported from EA Studio is clean and simple. It is designed to allow easy modifications.

This topic is a guide how a trader to modify his export in order to implement Spread protection.

Re: Protection - Maximum Spread

just to confirm

this EA modification, works with portfolios?

Re: Protection - Maximum Spread

I wanted to set a criteria ... if spread is greater than allowable spread, don't trade.

Is this post still relevant ?  I couldn't find ManageOpen in the EA generated by FSB Pro.

If this post is no long relevant, what is the open logic that I can use to prevent trading when spread is too high ?

Re: Protection - Maximum Spread

This thread is for the Expert Advisor Studio experts.

You don't need this for FSB Pro. You can do it there with the Spread Level Pro indicator.

7 (edited by ChristianS 2017-12-29 22:21:54)

Re: Protection - Maximum Spread

Hi, the idea is great to protect the EA when spread arise.

In the EA studio export I found the coding insert place:  ManageOpen() function, BUT what is with the EA studio portfolio?

In only found the function for every strategy Signal GetEntrySignal_01(), can I use the same coding block for spread protection for every strategy in the EA portfolio or is there a better coding way? For example maybe in the void SetSignals(Signal &signalList[]) ?

Any idea? Thanks Chris

Re: Protection - Maximum Spread

There is a function called ManageSignal() -- I've added code there to check for certain conditions and it seems to work fine.  If you place your code within the 'if' condition then it will only affect the opening of new positions -- which is probably what you want.

Re: Protection - Maximum Spread

Great, thanks for your input.

So for the EA studio portfolio we can insert for maximum spread this solution:

static input int    Maximum_Spread     = 25; // Maximum Spread (points)

and add in the function void ManageSignal(Signal &signal)

Position position=CreatePosition(signal.MagicNumber);
   if(position.Type==OP_FLAT && signal.Scope==ORDER_SCOPE_ENTRY)
       //Add protection maximum spread
      if(signal.Direction==ORDER_DIRECTION_BUY ||
         if (OrdersTotal()<MaxTrades) {

Re: Protection - Maximum Spread

Yes -- I believe that is the idea, though I'm not prepared to guarantee that is the only or most correct solution.  However, that would be the first thing I would try.

Also, it goes without saying, this needs to be tested.  It should be relatively easy -- e.g. you could use MT4's Strategy Tester and experiment with changing the Spread's input parameter.