God Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vanitati
//@version=5
indicator('God Candles', overlay=true)

// ----------- Inputs ------------
// God Candle Settings
lookbackPeriod = input.int(30, title="Dilution", tooltip ='Higher value will show fewer god candles, but should be stronger. Lower value will show more god candles, but they will be less strong',group ='god candle settings')
lineLength = input.int(6, "Line Length", minval=1, maxval=100, group ='god candle settings')
godCandleLineColor1 = input(color.rgb(20, 190, 202), "Bullish God candle line", group='god candle settings')
godCandleLineColor2 = input(color.rgb(171, 49, 196), "Bearish God candle line", group='god candle settings')
showDiamonds = input.bool(true, "Show Diamonds", group ='god candle settings')

// Session Time Filter
useSessionFilter = input.bool(defval=false, title="Use Session Time Filter", group="Session Time Filter")
sessionStartHour = input.int(defval=15, title="Session Start Hour", minval=0, maxval=23, group="Session Time Filter")
sessionEndHour = input.int(defval=19, title="Session End Hour", minval=0, maxval=23, group="Session Time Filter")

// ----------- God Candle Conditions ------------
//basic engulfing maths
bodyBarPrevious = math.abs(close[1] - open[1])
bodyBarCurrent = math.abs(close - open)
bullishEngulfing = (low <= low[1]) and (close >= high[1])
bearishEngulfing = (high >= high[1]) and (close <= low[1])

//god candle volume calculations
sumVolume = math.sum(volume, 10)
av = sumVolume / 10
valueVolume = volume * (high - low)
hivalueVolume = ta.highest(valueVolume, 10)
va = volume >= av * 2 or valueVolume >= hivalueVolume ? 1 : (volume >= av * 1.5 ? 2 : 0)
isBull = close > open

//god candle colour settings
CUColor = color.lime 
CDColor = color.red 

//engulfing calculations
lowestLow = ta.lowest(low[1], lookbackPeriod)
highestHigh = ta.highest(high[1], lookbackPeriod)
bullishEngulfingClimax = bullishEngulfing and (va == 1) and isBull and (low < lowestLow)
bearishEngulfingClimax = bearishEngulfing and (va == 1) and not isBull and (high > highestHigh)
bullishEngulfingDiamond = bullishEngulfing and bodyBarCurrent >= bodyBarPrevious * 2 and low[1] < open and low < open and low[1] < close and low < close
bearishEngulfingDiamond = bearishEngulfing and bodyBarCurrent >= bodyBarPrevious * 2 and high[1] > open and high > open and high[1] > close and high > close

// Determine if the current bar's time is within the session
newDay = sessionEndHour < sessionStartHour
isInSession = useSessionFilter ? (newDay ? ((hour(time) >= sessionStartHour) or (hour(time) < sessionEndHour)) : ((hour(time) >= sessionStartHour) and (hour(time) < sessionEndHour))) : true

// ----------- Plotting ------------
//candles
barcolor(isInSession and bullishEngulfingClimax ? CUColor : isInSession and bearishEngulfingClimax ? CDColor : na)
//Session filter background colour
bgcolor(useSessionFilter and isInSession ? color.new(color.gray, 90) : na, title="Session Background")
//god candle plotting
if isInSession and bullishEngulfingClimax
    line.new(x1 = bar_index, y1 = low + (open - low) / 2, x2 = bar_index + lineLength, y2 = low + (open - low) / 2, color = godCandleLineColor1, width = 1)
if isInSession and bearishEngulfingClimax
    line.new(x1 = bar_index, y1 = high - (high - open) / 2, x2 = bar_index + lineLength, y2 = high - (high - open) / 2, color = godCandleLineColor2, width = 1)
//Dots plotting
plotshape(series=isInSession and bullishEngulfing and bullishEngulfingDiamond and not bullishEngulfingClimax, title='Pressure up', location=location.belowbar, color=showDiamonds ? color.rgb(29, 155, 177) : na, style=shape.diamond)
plotshape(series=isInSession and bearishEngulfing and bearishEngulfingDiamond and not bearishEngulfingClimax, title='Pressure down', location=location.abovebar, color=showDiamonds ? color.rgb(171, 46, 196) : na, style=shape.diamond)
// Alert conditions
alertcondition(bullishEngulfingClimax or bearishEngulfingClimax, title='God Candle', message='God candle')