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:

http://s32.postimg.org/3q4fi9cpx/screenshot_1936.png


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.

    if(Ask-Bid>_Point*Maximum_Spread)
      return;

The code will look like that:
http://s32.postimg.org/xqgp4eisl/screenshot_1940.png





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)

http://s32.postimg.org/5yrsr2ylh/screenshot_1938.png


2. Add the following check in the ManageOpen function.

   MqlTick tick;
   SymbolInfoTick(_Symbol,tick);
   if(tick.ask-tick.bid>_Point*Maximum_Spread)
      return;

http://s32.postimg.org/cqmt7ocyt/screenshot_1939.png

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

https://s9.postimg.org/50171xll7/EAS-_Manage_Signal-572x398.png

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(Ask-Bid>_Point*Maximum_Spread)
      return; 
      
      
      if(signal.Direction==ORDER_DIRECTION_BUY ||
         signal.Direction==ORDER_DIRECTION_SELL)
         if (OrdersTotal()<MaxTrades) {
         OpenPosition(signal);}
     }

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.

Re: Protection - Maximum Spread

What to do that i get a comment in journal that spread is too high?

Re: Protection - Maximum Spread

Something like that.

const double spread = (Ask - Bid) / _Point;
if (spread > Maximum_Spread)
{
    Print("The current spread", DoubleToString(spread, 0), "is higher than the maximum allowed.");
    return;
}

13 (edited by Popov 2018-03-27 19:31:03)

Re: Protection - Maximum Spread

This is working for mt4 but the mt5 code not work. can you check with a print function

14 (edited by deadlef 2018-03-27 20:46:23)

Re: Protection - Maximum Spread

deadlef wrote:

This is working for mt4 but the mt5 code not work. can you check with a print function

Just checked again if i add this to mq5 file.

 MqlTick tick;
   SymbolInfoTick(_Symbol,tick);
   if(tick.ask-tick.bid>_Point*Maximum_Spread)
      return;

it looks working well..but when i add this with a comment in journal

 MqlTick tick;
   SymbolInfoTick(_Symbol,tick);
   if(tick.ask-tick.bid>_Point*Maximum_Spread)
{
Print("The current spread is higher than the maximum allowed.");
}
return;

it doesnt work..can someone help me out

Re: Protection - Maximum Spread

Your code is correct. This is the way MT provide Bid and Ask prices needed for calculating the real spread.

Re: Protection - Maximum Spread

Something is wrong here.

First i set Max Spread to 50. The Gold Spread currently is 200.
https://s14.postimg.org/p7oumqu6l/set1.png
Now the results. Here looks okay.
https://s14.postimg.org/ytif2kvhp/res1.png

Now i set Max Spread to 300. The Gold Spread currently is 200.
https://s14.postimg.org/5ecqtmoe5/set2.png

And now there is something wrong. Now it must trade but this is in journal and make no trades.
And this i have checked with EURUSD and other pairs.
https://s14.postimg.org/eywdgkqlp/res2.png

Re: Protection - Maximum Spread

The "return;" in you second code must be inside the braces.

Re: Protection - Maximum Spread

Hi All,

Has this Portfolio spread protection code by ChristianS been tested for performance, anyone??

ChristianS wrote:

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(Ask-Bid>_Point*Maximum_Spread)
      return; 
      
      
      if(signal.Direction==ORDER_DIRECTION_BUY ||
         signal.Direction==ORDER_DIRECTION_SELL)
         if (OrdersTotal()<MaxTrades) {
         OpenPosition(signal);}
     }