A moving-average crossover can look attractive on a chart while the executable buy price is unusually expensive. An MT4 Expert Advisor can reject that opportunity instead of treating every crossover as equally tradable.
What you will learn
- Separate closed-bar signals from executable spread checks.
- Define a one-attempt entry rule and protective exits.
- Implement the gate in MQL4 without confusing points and pips.
- Test variable spreads and missed opportunities.
Strategy hypothesis
At the first tick of a new bar, check whether the 20 EMA crossed above the 50 EMA on the preceding completed bar. Permit a long entry only if the current ask minus bid is at most 20 points and no position or pending entry belonging to the EA exists. Reject the signal for that entire bar if the spread is too wide; do not chase it later. The hypothesis is that avoiding expensive entry conditions may improve net execution economics, at the cost of missing some profitable trades.
Numerical example
On a five-digit EURUSD quote, Point is 0.00001. Bid 1.10000 and ask 1.10018 imply 18 points or 1.8 pips, passing the gate. Ask 1.10035 implies 35 points or 3.5 pips, failing it. Four hypothetical otherwise eligible signals with spreads of 12, 18, 35 and 15 points yield three passes and one rejection.

Hypothetical spread observations; the blue line is the 20-point ceiling.
MQL4 signal and execution gate
// MQL4 EA fragment: one evaluation per new bar
void OnTick()
{
static datetime seen = 0;
if(Bars < 52 || Time[0] == seen) return;
seen = Time[0];
double f1 = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1);
double f2 = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,2);
double s1 = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,1);
double s2 = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,2);
if(!(f2 <= s2 && f1 > s1)) return;
RefreshRates();
if((Ask-Bid)/Point > 20.000001) return;
// Check symbol/magic exposure and broker limits.
// Submit buy with validated volume and protection.
// Inspect OrderSend result; do not blindly retry.
}
This is an integration fragment, not a complete trading EA. iMA shifts 1 and 2 use completed bars; RefreshRates updates the available quote before the spread check. The static timestamp prevents repeated attempts during a bar, but resets on restart; persist the last processed timestamp if restart protection is required. See OrderSend documentation for submission parameters and return handling.
Order management
For a research example, use a fixed broker-valid volume, a 150-point stop and 300-point target measured from actual entry. Validate lot step, margin, stop distance and price normalization; reconcile the actual fill and protective orders. Some brokers require protection to be added after entry, creating an exposure interval that must be handled explicitly. A spread check is not a guarantee against slippage. Filter orders by symbol and magic number and avoid submitting a second entry when execution status is uncertain.
Testing and limitations
Compare the same crossover and exits with no spread gate and ceilings of 15, 20 and 30 points. Use variable-spread tick data where available; a fixed-spread test cannot measure the proposed filter realistically. Log signal time, bid, ask, spread, rejection reason, fill and net costs. Test exact boundary values, restarts, weekend gaps and broker rejections. Evaluate out-of-sample trade count, drawdown and net performance. The filter may exclude profitable trades and does not establish an edge. This fragment has not been compiled or backtested. Trading involves substantial loss risk.
For execution filters and EA integration, see Custom MT4 strategy Development Services.