// ENGULFING (TradingView 4) (Engulfing Look-back Alert)
//
//https://www.tradingview.com/script/lfEO7LvU-Engulfing-Look-back-Alert/
//
//Engulfing Candle Definition:
//
//- Bullish Engulfing: Trade BELOW the prior candle's LOW and CLOSE ABOVE the prior candle's HIGH.
// Previous candle can be an up ( bullish ) or a down ( bearish ) candle
//- Bearish Engulfing: Trade ABOVE the prior candle’s HIGH and CLOSE BELOW the prior candle’s LOW.
// Previous candle can be an up ( bullish ) or a down ( bearish ) candle
//
//Features:
//
//- Set the look-back period for engulfing candle high and low -> default = 1; e.g. Did the bullish
// eng candle trade below the lows of the last 3 candles and trade above the highs of the last 4
// candles? Set the input values accordingly
LookBack = 1 //1 lookback period (default)
bullHiHis = highest[LookBack](high[1])
bullLoLos = lowest[LookBack](low[1])
bearHiHis = highest[LookBack](high[1])
bearLoLos = lowest[LookBack](low[1])
//===============================================
//BULLISH ENGULFING
//===============================================
//Candle must close above prev candle high AND candle low is lower than the lowest low for the
//look-back period (bullLowLows) AND candle high is higher than look-back period for the highest
//high (bullHiHighs)
BullishEngulfing = (close > high[1]) and (low < bullLoLos) and (high > bullHiHis)
//
//===============================================
//BEARISH ENGULFING
//===============================================
//Candle must close BELOW prev candle low AND candle high is higher than the highest high for the
// look-back period (bearHiHighs) AND candle low is lower than look-back period for the lowest low
// (bearLowLows)
BearishEngulfing = (close < low[1]) and (high > bearHiHis) and (low < bearLoLos)
//
Cond = 0
IF BullishEngulfing THEN
Cond = 1
//DrawCandle(Open,High,Low,Close) coloured(255,255,0,255) //Giallo
//DrawText("↓",BarIndex,high+range,Dialog,Bold,30) coloured(255,255,0,255)
ELSIF BearishEngulfing THEN
Cond = 2
//DrawCandle(Open,High,Low,Close) coloured(0,0,255,255) //Blù
//DrawText("↓",BarIndex,high+range,Dialog,Bold,30) coloured(0,0,255,255)
ENDIF
SCREENER[Cond]