In case you can add this to the default code that EA Studio exports, I´ve written an implementation that follows your coding style (I hope, haha) that you can use. Add this:
Input parameters section:
input int Maximum_Spread_Points = 0;
To the OpenPosition function (need to add it there as we want to be able to tell if the EA tried to place a trade during the time the spread was to high or not):
void OpenPosition(int command)
{
if (!IsWithinMaxSpread())
return;
[EXISTING CODE FROM HERE ON]
And a new function at the end of the MQ4 code:
bool IsWithinMaxSpread()
{
bool WithinMaxSpread = true;
if (Maximum_Spread_Points > 0)
{
double spread = NormalizeDouble(((Ask - Bid) / _Point), 0);
//Need NormalizeDouble here because of rounding errors in MT4 that otherwise occur (confirmed in several backtests).
if (spread > Maximum_Spread_Points)
{
Print("The current spread of ", DoubleToString(spread, 0), " points, is higher than the maximum allowed of ", DoubleToString(Maximum_Spread_Points, 0), " points. Skipping trade!");
WithinMaxSpread = false;
}
}
return(WithinMaxSpread);
}
Example validation-output (backtest and live tested)
2019.01.17 01:19:39.190 2018.09.26 05:00:00 10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.190 2018.09.25 13:00:00 10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.189 2018.09.24 04:00:00 10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.189 2018.09.20 03:00:00 10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
2019.01.17 01:19:39.189 2018.09.19 18:00:00 10000000 SuperStrat GBPJPY H1 GBPJPY,H1: The current spread of 7 points, is higher than the maximum allowed of 6 points. Skipping trade!
Note that "Maximum_Spread_Points" is default at 0, which means the protection is disabled. Only if it is > 0, it will be used. This way you can default turn it off by leaving it at 0 if freshly exported from EA Studio and users can turn it on only if they really need it ;-)
By the amount of views this forum thread has (it is the second most viewed after "auto lot size calculation"), I can tell that people really seem to be looking for this feature in EA Studio "out of the box" and I´d say it´s a feature that every serious trader really NEEDS to use to protect from "catastrophic" entries that can kill the account in the worst case. if you just think Brexit / black swan events (CHF comes to mind) where we had spreads > 500 pips at times, it would be fatal to enter a trade at that spread and, if trading bigger amounts, can cost you a few thousand dollars or more quickly as right when you enter the trade you will be at -500 pips loss. And if that happens, EA Studio´s MT4 code will also not be able to set the stop loss (in case it´s < 500 pips) and the trade would go completely uncontrolled from there, not even having a stop loss (and this, by the way, is another feature you should think about: if the EA is unable to set a stop loss to a just opened position after say 3 times, it should close it right away as something BAD just happened - I have that in my own EAs all the time. Yes, one might think I am paranoid, but it´s a bit like driving without a seat belt, usually you never need it, but that one time you didn´t use it, can cost your life, or in this case, your whole account). Hence I think that the spread protection feature should be a *standard* in any serious platform / for any serious trader that does not just trade on demo.