Liquidity Candles with Prev Day High/Low and Midnight Open
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theDOGEguy1

//@version=4
study(title="Liquidity Candles with Prev Day High/Low and Midnight Open", overlay=true)

// Inputs
lookback = input(20, title="Lookback Period")

// Calculate LH and HL
lh = high >= highest(high[1], lookback) and high > high[lookback]
hl = low <= lowest(low[1], lookback) and low < low[lookback]

// Calculate Bullish and Bearish Engulfing Candles
eng_bull = (close[1] > open[1]) and (close < open) and ((open - close[1]) / (close - open) > 0.6)
eng_bear = (close[1] < open[1]) and (close > open) and ((close - open[1]) / (open - close) > 0.6)

// Calculate Previous Day's High, Low, and Midnight Open
prev_high = security(syminfo.tickerid, 'D', high[1], lookahead=true)
prev_low = security(syminfo.tickerid, 'D', low[1], lookahead=true)
midnight_open = security(syminfo.tickerid, 'D', open, lookahead=true)

// Color the Candles
candle_color = lh or hl ? color.yellow : na
candle_color := eng_bull ? color.green : candle_color
candle_color := eng_bear ? color.red : candle_color

plotcandle(open, high, low, close, color=candle_color)

// Plot Previous Day's High, Low, and Midnight Open
plot(prev_high, title="Prev Day High", style=plot.style_linebr, color=color.white)
plot(prev_low, title="Prev Day Low", style=plot.style_linebr, color=color.white)
plot(midnight_open, title="Midnight Open", style=plot.style_linebr, color=color.purple)
