MT4 EMA Pullback Continuation Indicator: Closed-Bar Signals with ATR-Based Tolerance
A trending market rarely advances in a straight line. This free MetaTrader 4 custom indicator looks for a candle that revisits a fast exponential moving average and closes back in the trend direction. It plots historical arrows on the price chart, without placing trades.
Design and signal rules
The default fast EMA is 21 bars and the slow EMA is 55 bars, both calculated on closing prices. A bullish trend requires fast EMA above slow EMA, the fast EMA rising relative to the previous bar, and the slow EMA not falling. The bearish test reverses all three comparisons. ATR(14) measures the volatility tolerance for touching the fast EMA.
A candle counts as touching the EMA when its range overlaps the band from fast EMA minus 0.30 × ATR to fast EMA plus 0.30 × ATR. A bullish arrow additionally requires the candle to close above its fast EMA and above its own open; a bearish arrow requires a close below both. Arrows appear 0.50 × ATR below the low or above the high. The current forming candle (index 0) never receives a signal.

Configurable inputs
FastEMAPeriod=21; SlowEMAPeriod=55; ATRPeriod=14; TouchATRMultiplier=0.30; ArrowATRMultiplier=0.50. The fast period must be at least 2 and smaller than the slow period. ATR period must be at least 2, and both multipliers must be nonnegative.
Calculation example
Suppose the fast EMA is 1.1050 and ATR is 0.0020. The allowable touch band is 1.1044 to 1.1056 because 0.30 × 0.0020 = 0.0006. A candle spanning 1.1048–1.1060 overlaps the band. If the EMA trend tests are bullish and that candle opens at 1.1051 and closes at 1.1058, it qualifies. With a candle low of 1.1048, the plotted buy arrow is at 1.1038. These are illustrative values, not a trade recommendation.
Complete MQL4 source code
//+------------------------------------------------------------------+
//| PatternSmart EMA Pullback Continuation - MT4 Custom Indicator |
//| Signals only on the most recently CLOSED candle and history. |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrSeaGreen
#property indicator_color2 clrTomato
#property indicator_width1 2
#property indicator_width2 2
input int FastEMAPeriod = 21;
input int SlowEMAPeriod = 55;
input int ATRPeriod = 14;
input double TouchATRMultiplier = 0.30;
input double ArrowATRMultiplier = 0.50;
double BuyBuffer[];
double SellBuffer[];
int OnInit()
{
if(FastEMAPeriod < 2 || SlowEMAPeriod <= FastEMAPeriod ||
ATRPeriod < 2 || TouchATRMultiplier < 0.0 ||
ArrowATRMultiplier < 0.0)
return(INIT_PARAMETERS_INCORRECT);
SetIndexBuffer(0, BuyBuffer);
SetIndexBuffer(1, SellBuffer);
ArraySetAsSeries(BuyBuffer, true);
ArraySetAsSeries(SellBuffer, true);
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 233);
SetIndexLabel(0, "Bullish EMA Pullback");
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 234);
SetIndexLabel(1, "Bearish EMA Pullback");
SetIndexEmptyValue(0, EMPTY_VALUE);
SetIndexEmptyValue(1, EMPTY_VALUE);
IndicatorShortName("PatternSmart EMA Pullback Continuation");
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int minBars = MathMax(SlowEMAPeriod, ATRPeriod) + 3;
if(rates_total < minBars) return(0);
ArraySetAsSeries(open, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(close, true);
BuyBuffer[0] = EMPTY_VALUE;
SellBuffer[0] = EMPTY_VALUE;
int first = rates_total - minBars;
if(prev_calculated > 0)
first = MathMin(first, rates_total - prev_calculated + 1);
for(int i = first; i >= 1; i--)
{
BuyBuffer[i] = EMPTY_VALUE;
SellBuffer[i] = EMPTY_VALUE;
double fast = iMA(NULL, 0, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
double slow = iMA(NULL, 0, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
double fastPrev = iMA(NULL, 0, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i+1);
double slowPrev = iMA(NULL, 0, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i+1);
double atr = iATR(NULL, 0, ATRPeriod, i);
if(atr <= 0.0) continue;
bool nearFast = (low[i] <= fast + atr * TouchATRMultiplier &&
high[i] >= fast - atr * TouchATRMultiplier);
bool up = fast > slow && fast > fastPrev && slow >= slowPrev;
bool down = fast < slow && fast < fastPrev && slow <= slowPrev;
if(up && nearFast && close[i] > fast && close[i] > open[i])
BuyBuffer[i] = low[i] - atr * ArrowATRMultiplier;
if(down && nearFast && close[i] < fast && close[i] < open[i])
SellBuffer[i] = high[i] + atr * ArrowATRMultiplier;
}
return(rates_total);
}
Installation and use
In MT4 open File → Open Data Folder → MQL4 → Indicators. Create PatternSmart_EMA_Pullback_Continuation.mq4, paste the complete source, compile in MetaEditor, and attach it to a price chart. Verify the two arrow buffers and parameters. Use closed-candle signals for review; arrows do not execute orders or provide stops or targets.
Validation and limitations
Test several symbols, timeframes, and periods in the Strategy Tester or on a demo chart. Confirm that the newest candle has no arrow, that historical signals appear only on qualifying closed candles, and that changed parameters recalculate the expected range. Review flat, strongly trending, and gap-prone markets separately. Historical calculations may be refreshed after broker history changes; EMA and ATR warm-up values depend on available bars. Intrabar price may touch the EMA but fail the close confirmation. A narrow ATR tolerance can miss valid pullbacks; a wide tolerance can admit noisy signals. Spread, slippage, volatility regime, and liquidity can invalidate otherwise attractive patterns. The indicator does not guarantee profitable entries.
Interested in this free indicator and want to customize its filters, alerts, or signal rules? Explore our Custom Trading Software Development Services to discuss your requirements.
Risk disclosure: All trading involves risk. This educational example is not financial advice. Independently compile, backtest, and forward-test before risking capital.