Titans Engulfing Retracement Zones
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mmmarsh

//@version=5
indicator("Titans Engulfing Retracement Zones", "TERZ", overlay=true, max_lines_count=500, max_boxes_count=500)
//This indicator comprises 3 concepts. 
//The engulfing candle concept at user-defined higher timeframe summarises an impulse move. 
//The user-defined retracement levels denotes target entry and target exit zones. 
//The direction of trade is further enhanced by a long candle wick condition indicating rejection in the other direction. 
//Mechanics
//When an engulfing candle with a long wick is detected on the higher timeframe, the entery and exit zones are plotted. 
//The plots remain until a engulfing candle with long wick is detected. 

string tfInput = input.timeframe("60", "Higher Timeframe")

// Initialize Global Variables
var Entry1=float(na)
var Entry2=float(na)
var Profit1=float(na)
var Profit2=float(na)
var line Entry1Line=na
var line Entry2Line=na
var line Profit1Line=na
var line Profit2Line=na
var box EntryBox=na
var box ProfitBox=na
var CandleRange=float(na)

//Get Higher TimeFrame Data Series
Close=request.security(syminfo.tickerid, tfInput, close)
Open=request.security(syminfo.tickerid, tfInput, open)
High=request.security(syminfo.tickerid, tfInput, high)
Low=request.security(syminfo.tickerid, tfInput, low)
CandleRange:=math.abs(High-Low)

//ID Bullish and Bearish Engulfing Candles
Bullish=(Close>Open and Close>Close[1] and Open<Open[1] and math.abs(Close-Open)>math.abs(Close[1]-Open[1]))? true : false
Bearish=(Open>Close and Close<Close[1] and Open>Open[1] and math.abs(Close-Open)>math.abs(Close[1]-Open[1]))? true : false

//Check Engulfing Candle Wick is longer than Previous Candle Wick
LongWick=false
PreviousCandleBullish= Close[1]>Open[1]? true : false

if Bullish and PreviousCandleBullish and (Open-Low)<(Open[1]-Low[1])
    LongWick:=true
else if Bullish and not PreviousCandleBullish and (Open-Low)<(Close[1]-Low[1])
    LongWick:=true
else if Bearish and PreviousCandleBullish and (High-Open)<(High[1]-Close[1])
    LongWick:=true
else if Bearish and not PreviousCandleBullish and (High-Open)<(High[1]-Open[1])
    LongWick:=true
else
    LongWick:=false

//Set Zone Levels
RatioEntry1=input.float(0.000,'Entry 1')
RatioEntry2=input.float(0.382,'Entry 2')
RatioProfit1=input.float(0.786,'Profit 1')
RatioProfit2=input.float(1.618,'Profit 2')

//Calculate Levels for Valid Engulfing Condition
if Bullish and LongWick
    Entry1:=Open+CandleRange*RatioEntry1
    Entry2:=Open+CandleRange*RatioEntry2
    Profit1:=Open+CandleRange*RatioProfit1
    Profit2:=Open+CandleRange*RatioProfit2
    
if Bearish and LongWick
    Entry1:=Open-CandleRange*RatioEntry1
    Entry2:=Open-CandleRange*RatioEntry2
    Profit1:=Open-CandleRange*RatioProfit1
    Profit2:=Open-CandleRange*RatioProfit2
    
// Detect changes in timeframe.
bool newTF = ta.change(time(tfInput))
if newTF
    // New bar in higher timeframe; reset values and create new lines and box.
    Entry1Line:= line.new(bar_index - 1, Entry1, bar_index, Entry1, color = color.blue, width = 1)
    Entry2Line:= line.new(bar_index - 1, Entry1, bar_index, Entry1, color = color.blue, width = 1)
    Profit1Line:= line.new(bar_index - 1, Profit1, bar_index, Profit1, color = color.orange, width = 1)
    Profit2Line:= line.new(bar_index - 1, Profit2, bar_index, Profit2, color = color.orange, width = 1)
    EntryBox := box.new(bar_index - 1, Entry1, bar_index, Entry2, border_color = na, bgcolor = color.new(color.blue, 50))
    ProfitBox := box.new(bar_index - 1, Profit1, bar_index, Profit2, border_color = na, bgcolor = color.new(color.orange, 50))
    int(na)
else
    // On other bars, extend the right coordinate of lines and box.
    line.set_x2(Entry1Line, bar_index)
    line.set_x2(Entry2Line, bar_index)
    line.set_x2(Profit1Line, bar_index)
    line.set_x2(Profit2Line, bar_index)
    box.set_right(EntryBox, bar_index)
    box.set_right(ProfitBox, bar_index)
    int(na)

plotshape(Bullish,style=shape.triangleup,location=location.belowbar,color=color.new(color.green,20))
plotshape(Bearish,style=shape.triangledown,location=location.abovebar,color=color.new(color.red,20))

alertcondition((Bullish and LongWick) or (Bearish and LongWick), 'Engulfing with Wick', '{{exchange}}:{{ticker}} - Engulfing candle with long wick detected on higher timeframe at {{timenow}}. New target zones marked') 