Here's the final Pine Script v6 indicator that highlights higher timeframe candles where the current candle's high or low breaches the previous candle's high or low, and the current candle closes within the previous candle's range:
//@version=6
indicator("Candle Breach Indicator", overlay=true)
// Input for higher timeframe
higherTF = input.timeframe("D", "Higher Timeframe")
// Request higher timeframe data
higherOpen = request.security(syminfo.tickerid, higherTF, open)
higherHigh = request.security(syminfo.tickerid, higherTF, high)
higherLow = request.security(syminfo.tickerid, higherTF, low)
higherClose = request.security(syminfo.tickerid, higherTF, close)
higherTimeOpen = request.security(syminfo.tickerid, higherTF, time)
higherTimeClose = request.security(syminfo.tickerid, higherTF, time_close)
// Calculate the condition on the higher timeframe
prevHigh = request.security(syminfo.tickerid, higherTF, high[1])
prevLow = request.security(syminfo.tickerid, higherTF, low[1])
prevClose = request.security(syminfo.tickerid, higherTF, close[1])
currentHigh = higherHigh
currentLow = higherLow
currentClose = higherClose
breachHigh = currentHigh > prevHigh
breachLow = currentLow < prevLow
closeWithinRange = currentClose <= prevHigh and currentClose >= prevLow
condition = (breachHigh or breachLow) and closeWithinRange
// Keep track of the last processed higher timeframe bar's open time to avoid duplicate boxes
var float lastProcessedTime = na
// Check if the current higher timeframe bar meets the condition and hasn't been processed yet
if condition and (na(lastProcessedTime) or higherTimeOpen != lastProcessedTime)
box.new(
left=higherTimeOpen,
right=higherTimeClose,
top=higherHigh,
bottom=higherLow,
border_color=color.red,
border_width=2,
bgcolor=color.new(color.red, 90)
)
lastProcessedTime := higherTimeOpen
This script will highlight the specified higher timeframe candles on the lower timeframe chart with rectangles. The user can adjust the higher timeframe using the input settings.