4H Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © My3rdid

//@version=5
indicator("Custom Range", overlay=true)

colorChoice = input(color.white, "Choose your color")
sminutes = input(true, title="Show Range")
mperiod = input.string("240", title="Select Range Period", options=["5", "15", "30", "60", "240", "D", "W", "M"])

showLabels = input.bool(true, title="Show labels", inline="labels")
labelSize = input.string("normal", options=["tiny", "small", "normal", "large"], title="Label Size")
display_value = input.bool(true, title='Show Price Values in Labels')
nbarsforward = input.int(title='Offset labels N bars to the right', defval=8)

ticksThreshold = input(10, "Number of ticks")
tickSize = syminfo.mintick

var line highLine = na
var line lowLine = na
var line midLine = na
var label highLabel = na
var label lowLabel = na
var label midLabel = na
var float prevHigh = na
var float prevLow = na
var bool firstTime = na

// New series type variables
var float highValue = na
var float lowValue = na
var float midValue = na

// Find the Last candle in minutes series selection
// This is the only candle we want to show the levels for
_minHigh = request.security(syminfo.tickerid, mperiod, high)
_minLow = request.security(syminfo.tickerid, mperiod, low)
_minClose = request.security(syminfo.tickerid, mperiod, close)
_minMid = (_minHigh + _minLow) / 2


//Calculations for Minutes levels
if (na(firstTime)) 
    highLine := line.new(x1=bar_index, y1=_minHigh, x2=bar_index, y2=_minHigh, color=colorChoice, width = 1)
    lowLine := line.new(x1=bar_index, y1=_minLow, x2=bar_index, y2=_minLow, color=colorChoice, width = 1)
    midLine := line.new(x1=bar_index, y1=_minMid, x2=bar_index, y2=_minMid, color=colorChoice, width = 1)
    prevHigh := _minHigh
    prevLow := _minLow
    firstTime := false

if (_minClose > prevHigh + ticksThreshold * tickSize or _minClose < prevLow - ticksThreshold * tickSize)
    line.delete(highLine)
    line.delete(lowLine)
    line.delete(midLine)
    label.delete(highLabel)
    label.delete(lowLabel)
    label.delete(midLabel)
    highLine := line.new(x1=bar_index, y1=_minHigh, x2=bar_index, y2=_minHigh, color=colorChoice, width = 1)
    lowLine := line.new(x1=bar_index, y1=_minLow, x2=bar_index, y2=_minLow, color=colorChoice, width = 1)
    midLine := line.new(x1=bar_index, y1=_minMid, x2=bar_index, y2=_minMid, color=colorChoice, width = 1)
    prevHigh := _minHigh
    prevLow := _minLow

       // Update series type variables
    highValue := _minHigh
    lowValue := _minLow
    midValue := _minMid

    if showLabels
        highLabel := label.new(x=bar_index+nbarsforward, y=_minHigh, text=str.tostring(_minHigh, "#.00"), style=label.style_none, textcolor=colorChoice, textalign=text.align_left, size=labelSize, xloc=xloc.bar_index)
        lowLabel := label.new(x=bar_index+nbarsforward, y=_minLow, text=str.tostring(_minLow, "#.00"), style=label.style_none, textcolor=colorChoice, textalign=text.align_left, size=labelSize, xloc=xloc.bar_index)
        midLabel := label.new(x=bar_index+nbarsforward, y=_minMid, text=str.tostring(_minMid, "#.00"), style=label.style_none, textcolor=colorChoice, textalign=text.align_left, size=labelSize, xloc=xloc.bar_index)

        // Output values for data window display
plot(highValue, "Range High", color.new(color.white,100))
plot(midValue, "Range Mid", color.new(color.white,100))
plot(lowValue, "Range Low", color.new(color.white,100))

line.set_x2(highLine, bar_index)
line.set_x2(lowLine, bar_index)
line.set_x2(midLine, bar_index)

label.set_x(highLabel, bar_index + nbarsforward)
label.set_x(lowLabel, bar_index + nbarsforward)
label.set_x(midLabel, bar_index + nbarsforward)

