Based on a request on the forum (https://www.prorealcode.com/topic/count-5-10candle-sticks-that-closes-above-50-ema/), I wrote this indicator that may help to better identify when an Engulfing pattern can be a good reversal point.
It counts candles BELOW an MA (on a Bullish Engulfing) and candles ABOVE an MA (on a Bearish Engulfing). I added two MA’s, to detect when their average counts are rising or dropping to make an even better decision:
// Count candles Above or Below an average before an Engulfing occurs
//
// (requested at https://www.prorealcode.com/topic/count-5-10candle-sticks-that-closes-above-50-ema/)
//
// AvgP = 100
// AvgT = 0
// p = 50
// t = 1
//
AvgP = max(2,min(999,AvgP)) //2-999
AvgT = max(0,min(6,AvgT)) //0-6
p = max(2,min(999,p)) //2-999
t = max(0,min(6,t)) //0-6
//
Bullish = close > open
Bearish = close < open
Opposite = (Bearish AND Bullish[1]) OR (Bearish[1] AND Bullish)
Body = abs(close - open)
Engulfing = (Body > Body[1]) AND Opposite
BullishEng= Engulfing AND Bullish AND (close >= open[1]) AND (open <= close[1])
BearishEng= Engulfing AND Bearish AND (Open >= close[1]) AND (close <= open[1])
MyEma = average[p,t](close)
//
// Count candles ABOVE average before a BEARISH Engulfing occurs
//
BullBarCount = 0
BearBarCount = 0
If BearishEng THEN
//BullBarCount = 0
FOR i = BarIndex - 1 DOWNTO 1
x = BarIndex - i
IF close[x] > MyEma[x] THEN
BullBarCount = BullBarCount + 1
ELSE
BREAK
ENDIF
NEXT
ENDIF
//
// Count candles BELOW average before a BULLISH Engulfing occurs
//
If BullishEng THEN
//BearBarCount = 0
FOR i = BarIndex - 1 DOWNTO 1
x = BarIndex - i
IF close[x] < MyEma[x] THEN
BearBarCount = BearBarCount + 1
ELSE
BREAK
ENDIF
NEXT
BearBarCount = -BearBarCount
ENDIF
//
BullAvg = average[AvgP,AvgT](BullBarCount)
BearAvg = average[AvgP,AvgT](BearBarCount)
//
RETURN BullBarCount AS "Bulls",BearBarCount AS "Bears",BullAvg AS "BullMA",BearAvg AS "BearMA",0 AS "Zero"