SKYROCKET | CBDR Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adelghaeinian

//@version=5
indicator("SKYROCKET | CBDR Range", overlay=true, max_lines_count = 500)

range_start_hour = input.int(defval=12, minval=0, maxval=23, title="Range start Hour", group="START", inline="start")
range_start_minute = input.int(defval=0, minval=0, maxval=59, title="Minutes", group="START", inline="start")
range_end_hour = input.int(defval=13, minval=0, maxval=23, title="Range start Hour", group="END", inline="end")
range_end_minute = input.int(defval=0, minval=0, maxval=59, title="Minutes", group="END", inline="end")

count = input(3, title="Count")

colorTop = input(color.green)
colorBetween = input(color.new(color.gray, 50))
colorBottom = input(color.red)

colorRange = input(color.blue)
rangeIsDotted = input(true, "Dash Line Style For The Range")

plot_mid = input(true)

close_to_close = input(false, "Close To Close (instead of low to high distance)")

isInRange = hour*60 + minute<= (range_end_hour*60+range_end_minute) and hour*60 + minute > range_start_minute+range_start_hour*60

isOnEnd = hour[1]*60 + minute[1] < range_end_hour*60+range_end_minute and hour*60 + minute >= range_end_hour*60+range_end_minute
isOnStart =  hour[1]*60 + minute[1] < range_start_minute+range_start_hour*60 and hour*60 + minute >= range_start_minute+range_start_hour*60
var startIDX = -1

var range_high = -100000.0
var range_low = 99999999.9

var distance = 0.0

if isOnStart
    startIDX:=bar_index

if isInRange
    if not close_to_close
        range_high := math.max(range_high, high)
        range_low:= math.min(range_low, low)
    
    if close_to_close
        range_high := math.max(range_high, close)
        range_low:= math.min(range_low, close)
    distance := range_high - range_low
    
if isOnEnd
    line.new(startIDX, range_high, bar_index, range_high, xloc=xloc.bar_index, style=rangeIsDotted?line.style_dashed:line.style_solid, color=colorRange)
    line.new(startIDX, range_low, bar_index, range_low, xloc=xloc.bar_index, style=rangeIsDotted?line.style_dashed:line.style_solid, color=colorRange)
	for i = 1 to count
        line.new(startIDX, range_high + i*distance, bar_index, range_high + i*distance, xloc=xloc.bar_index, style=line.style_solid, color=colorTop)
	if plot_mid
        for i = 1 to count
        	line.new(startIDX, range_high + (i-1)*distance + distance/2, bar_index, range_high + (i-1)*distance + distance/2, xloc=xloc.bar_index, style=line.style_dashed, color=colorBetween)
        for i = 1 to count
        	line.new(startIDX, range_low - (i-1)*distance - distance/2, bar_index, range_low - (i-1)*distance - distance/2, xloc=xloc.bar_index, style=line.style_dashed, color=colorBetween)

	for i = 1 to count
		line.new(startIDX, range_low - i*distance, bar_index, range_low - i*distance, xloc=xloc.bar_index, style=line.style_solid, color=colorBottom)
	
    range_high := -100000.0
    range_low := 99999999.9