A breakout confirmed at the close may become unattractive by the time an MT5 Expert Advisor receives the next tick. Instead of buying every confirmed breakout regardless of price, define a maximum adverse price displacement in units of ATR. This is an execution eligibility rule, not a prediction of future returns.

Separate signal from execution

For a long example, require the last completed candle to close above the highest high of the preceding 20 completed candles. In MQL5 series indexing, the signal candle is shift 1 and the comparison window is shifts 2 through 21. Use ATR(14) at shift 1, not the still-forming bar. At the first tick of the new bar, compare the current ask with the signal close. For shorts, compare the signal close with the bid after a close below the preceding 20-bar low.

Adverse gap = max(0, ask − signal close) for longs, or max(0, signal close − bid) for shorts. Permit entry only when adverse gap ≤ 0.25 × ATR. All quantities are in price units, not points.

Filtering Breakout Entries with an ATR-Based Adverse Gap Limit
With a signal close of 100 and ATR of 2, the maximum adverse displacement is 0.50. Three quotes illustrate eligibility, rejection and eligibility.

Three hypothetical entry decisions

Signal close ATR Ask Adverse gap Result
100.00 2.00 100.30 0.30 Eligible
100.00 2.00 100.80 0.80 Reject
100.00 2.00 99.80 0.00 Eligible

The permitted displacement is 0.50. A favorable gap does not automatically establish that the breakout remains valid: an independent check can require price to remain beyond the breakout level. The ask comparison includes the effect of the spread relative to the chart close, which is commonly bid-based in OTC FX.

MQL5 implementation

Create the iATR handle in OnInit, reject INVALID_HANDLE, and read one value with CopyBuffer(handle,0,1,1,buffer). Require a successful copy and sufficient history. Release the handle in OnDeinit. Use iTime at shift 0 to identify new bars; initialize the timestamp at startup if you do not want to trade an old signal immediately. The following helper assumes the signal and ATR are already available.

// Integration helper: call once per newly closed signal bar.
bool GapAllowed(const bool isLong, const double signalClose,
                const double atr, const double maxRatio)
{
    MqlTick tick;
    if(!SymbolInfoTick(_Symbol,tick)) return false;
    if(atr <= 0 || maxRatio < 0 || signalClose <= 0 ||
       tick.ask <= 0 || tick.bid <= 0 || tick.ask < tick.bid)
        return false;
    double gap = isLong ? tick.ask - signalClose
                        : signalClose - tick.bid;
    return MathMax(0.0,gap) <= atr * maxRatio;
}

Call the helper after checking the breakout and before submitting a market order. Mark the signal bar as consumed even if the filter rejects it; this version does not retry later in the bar. Check position ownership, account mode, spread, volume step and margin separately. Use CTrade or MqlTradeRequest and inspect the server return code; a successful method call alone does not prove execution. Track actual fills through trade transactions. A market order may still fill beyond the observed quote; this filter is not a guaranteed execution price.

Testing and limitations

Compare the unfiltered strategy with ratios 0.10, 0.25 and 0.50 using identical exit rules, for example an initial stop 1.5 ATR from the actual entry and a target 3 ATR away, subject to broker tick size and stop restrictions. Keep risk sizing consistent. Use real-tick modeling where available, include costs and inspect rejected trades as well as accepted trades. Test gaps, stale quotes, missing indicator data, terminal restarts and netting versus hedging accounts. Compare trade count, drawdown and net expectancy on unseen periods. A tight threshold can exclude profitable momentum trades, while a favorable gap can signal a failed breakout.

Official reference: SymbolInfoTick, CopyBuffer. The excerpt has not been compiled or backtested here. Trading involves risk of loss and hypothetical results do not guarantee future performance.

To adapt this strategy to your own rules, contact us for Custom MT5 strategy Development Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.