Range H/L Buy and Sell Signal
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theDOGEguy1

//@version=5
indicator("Range H/L Buy and Sell Signal", overlay=true)

// Customizable parameters
rangeStartHour = input(0, "Range Start Hour")
rangeStartMinute = input(0, "Range Start Minute")
rangeEndHour = input(8, "Range End Hour")
rangeEndMinute = input(00, "Range End Minute")
volatilityThreshold = input(0.5, "Volatility Threshold")

// Calculate range start and end timestamps
rangeStart = timestamp(year, month, dayofmonth, rangeStartHour, rangeStartMinute)
rangeEnd = timestamp(year, month, dayofmonth, rangeEndHour, rangeEndMinute)

// Calculate DR lines
High = ta.valuewhen(rangeStart <= time and time <= rangeEnd, ta.highest(high, 1), 0)
Low = ta.valuewhen(rangeStart <= time and time <= rangeEnd, ta.lowest(low, 1), 0)

// Calculate volatility
trueRange = ta.tr(true)
averageTrueRange = ta.sma(trueRange, 14)

// Generate buy and sell signals based on breakout conditions and volatility filter
buySignal = ta.crossover(close, High) and trueRange > volatilityThreshold * averageTrueRange
sellSignal = ta.crossunder(close, Low) and trueRange > volatilityThreshold * averageTrueRange

// Plot DR lines
plot(High, title="High", color=color.green)
plot(Low, title="Low", color=color.red)

// Plot buy and sell signals
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

