Remaining ATR [vnhilton]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vnhilton

//@version=5
indicator("Remaining ATR [vnhilton]", "RATR", overlay=true, max_lines_count=500)     //Visible only on intraday timeframes

//RTH Session
rth = time(timeframe.period, session.regular, syminfo.timezone) 

//Parameters
length = input(20, "Day ATR period", "Previous Day ATR will be used in calculations as it's static", group="ATR Parameters")
atrRTHM1 = input.float(1, "ATR Multiplier #1", 0, group="ATR Parameters")
atrRTHM2 = input.float(2, "ATR Multiplier #2", 0, group="ATR Parameters")
atrRTHM3 = input.float(3, "ATR Multiplier #3", 0, group="ATR Parameters")

irYAxis = input(close, "Intraday Range Y-Axis", group="Intraday Range Label Parameters")
irForm = input.string("Multiplier", "Intraday Range Form", ["Multiplier", "Percent", "Price"], tooltip="ATR Multiplier; Intraday Price; Percentage of ATR", group="Intraday Range Label Parameters")
irLabelOffset = input(0, "Intraday Label Offset", "Offsets to the right of the current candle", group="Intraday Range Label Parameters")
irLabelSize = input.string("Tiny", "Label Size", ["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group="Intraday Range Label Parameters")
irLabelTextColor = input.color(color.new(#ffffff, 0), "Intraday Range Label Text Color", group="Intraday Range Label Parameters")

extension = input(false, "Extend Current Lines to The Right?", group="Line Parameters")
recentLinesOnly = input(false, "Show Recent Lines Only?", group="Line Parameters")
insideLineHide = input(false, "Hide RATR Lines When RATR <= 0?", "When RATR Lines go within the intraday high/low range, the lines will be hidden", group="Line Parameters")
lineStyle = input.string("Solid", "Line Style", ["Dashed", "Dotted", "Solid"], group="Line Parameters")

insideLabelHide = input(false, "Hide RATR Labels When RATR <= 0?", "When RATR Lines go within the intraday high/low range, the labels will be hidden", group="Label Parameters")
labelOffset = input(0, "Label Offset", "Offsets to the right of lines", group="Label Parameters")
labelSize = input.string("Normal", "Label Size", ["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group="Label Parameters")

openLineColor = input.color(color.new(#b2b5be, 0), "Open line Color", group = "Open Line Parameters")
openLineThickness = input.int(1, "Open Line's thickness", 1, group="Open Line Parameters")
openLabelTextColor = input.color(color.new(#b2b5be, 0), "Open Label Text Color", group="Open Line Parameters")

baseLineThickness = input.int(1, "Baselines' thickness", 1, tooltip="Baselines are high & low lines", group="Baselines Parameters")
highDLineColor = input.color(color.new(#bfd08c, 0), "High Line Color",  group = "Baselines Parameters")
highDLabelTextColor = input.color(color.new(#bfd08c, 0), "High Label Text Color", group="Baselines Parameters")
highDPlotFillColor = input.color(color.new(#bfd08c, 90), "High Line Plot Fill Color", group = "Baselines Parameters")

lowDLineColor = input.color(color.new(#e35542, 0), "Low Line Color",  group = "Baselines Parameters")
lowDLabelTextColor = input.color(color.new(#e35542, 0), "Low Label Text Color", group="Baselines Parameters")
lowDPlotFillColor = input.color(color.new(#e35542, 90), "Low Line Plot Fill Color", group = "Baselines Parameters")

atrThickness = input.int(1, "ATR lines' thickness", 1, group="ATR Lines Parameters")
atr1LineColor = input.color(color.new(#4deeea, 0), "ATR #1 Lines Color",  group="ATR Lines Parameters")
atr1LineInsideColor = input.color(color.new(#4deeea, 80), "ATR #1 Lines Inside Color", "When RATR <= 0, then lines will change to a different color", group="ATR Lines Parameters")
atr1LabelTextColor = input.color(color.new(#4deeea, 0), "ATR #1 Label Text Color", group="ATR Lines Parameters")
atr1LabelTextInsideColor = input.color(color.new(#4deeea, 80), "ATR #1 Label Text Inside Color", "When RATR <= 0, then label will change to a different color", group="ATR Lines Parameters")
atr1PlotFillColor = input.color(color.new(#4deeea, 90), "ATR #1 Line Plot Fill Color", "For the cleanest plot fills, make sure the ATR multipliers are as follows: M3 > M2 > M1", group = "ATR Lines Parameters")

atr2LineColor = input.color(color.new(#8a00ff, 0), "ATR #2 Lines Color",  group="ATR Lines Parameters")
atr2LineInsideColor = input.color(color.new(#8a00ff, 80), "ATR #2 Lines Inside Color", group="ATR Lines Parameters")
atr2LabelTextColor = input.color(color.new(#8a00ff, 0), "ATR #2 Label Text Color", group="ATR Lines Parameters")
atr2LabelTextInsideColor = input.color(color.new(#8a00ff, 80), "ATR #2 Label Text Inside Color", group="ATR Lines Parameters")
atr2PlotFillColor = input.color(color.new(#8a00ff, 90), "ATR #2 Line Plot Fill Color", group = "ATR Lines Parameters")

atr3LineColor = input.color(color.new(#f000ff, 0), "ATR #3 Lines Color",  group="ATR Lines Parameters")
atr3LineInsideColor = input.color(color.new(#f000ff, 80), "ATR #3 Lines Inside Color", group="ATR Lines Parameters")
atr3LabelTextColor = input.color(color.new(#f000ff, 0), "ATR #3 Label Text Color", group="ATR Lines Parameters")
atr3LabelTextInsideColor = input.color(color.new(#f000ff, 80), "ATR #3 Label Text Inside Color", group="ATR Lines Parameters")
atr3PlotFillColor = input.color(color.new(#f000ff, 90), "ATR #3 Line Plot Fill Color",  group = "ATR Lines Parameters")

//hl2 Range Function
hl2Range(h, l) =>
   (h + l) / 2

//Values Gathering
atr = ta.atr(length)

[yAtrD, yHighD, yLowD] = request.security(syminfo.tickerid, "D", [atr[1], high[1], low[1]], lookahead=barmerge.lookahead_on)    //Daily data as ATR values consistent across daily/intraday TFs, & HL only used as conditions

//Obtain OHL via intraday data due to daily/intraday data inconsistencies
//Counting RTH Bars
rthBars = int(na)

//Reset On New Day
isNewDay = time("D") != time("D")[1]

rthBars := isNewDay ? 1 : rth ? nz(rthBars[1]) + 1 : 0

//Getting Open Value & Resetting rthBars For RTH Only
var float openD = na
if rthBars == 1
    openD := open

//Getting HL Values (This Leads to Warning But Extracting Functions Out Prevents Script From Working)
highD = rthBars < 1 ? ta.highest(high, 1) : ta.highest(high, rthBars)
lowD = rthBars < 1 ? ta.lowest(low, 1) : ta.lowest(low, rthBars)

CATR = highD - lowD                   //Current ATR

//ATR Calculations
remainingATR(multiplier) =>
    RATR = multiplier * yAtrD - CATR      //Remaining ATR

//Yesterday's & Today's HL2
yRange = hl2Range(yHighD, yLowD)    //Used to detect new day
tRange = hl2Range(highD, lowD)      //Used to detect intraday new highs/lows

//Label Size Selection
labelSizing(choice) =>
    switch labelSize
        "Auto" => size.auto
        "Tiny" => size.tiny
        "Small" => size.small
        "Normal" => size.normal
        "Large" => size.large
        "Huge" => size.huge

//Line Style Selection
lineStyling(choice) =>
    switch lineStyle
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        "Solid" => line.style_solid

//Intraday Range Label Form Calculation
irRangeFormSelection(choice) =>
    switch irForm
        "Multiplier" => str.tostring(CATR / yAtrD, '#0.00') + "x"
        "Percent" =>  str.tostring(CATR * 100 / yAtrD, '#0.00') + "%"
        "Price" => str.tostring(CATR, format.mintick)

//Line Style Assignments
style = lineStyling(lineStyle)

//Labels Size Assignment
size = labelSizing(labelSize)
irSize = labelSizing(irLabelSize)

//Intraday Range Assignment
irPrice = irRangeFormSelection(irForm)

//Setup Lines
var line openLine = na
var line highLine = na
var line lowLine = na

var line atr1LineU = na
var line atr1LineD = na
var line atr2LineU = na
var line atr2LineD = na
var line atr3LineU = na
var line atr3LineD = na

//Setup Line Plot Fills
var linefill atr1FillUp = na
var linefill atr1FillDown = na
var linefill atr2FillUp = na
var linefill atr2FillDown = na
var linefill atr3FillUp = na
var linefill atr3FillDown = na

//Setup Labels
var label openLabel = na
var label highLabel = na
var label lowLabel = na

var label irLabel = na

var label atr1LabelU = na
var label atr1LabelD = na
var label atr2LabelU = na
var label atr2LabelD = na
var label atr3LabelU = na
var label atr3LabelD = na

//Code to Clean Up RATR Lines When RATR < 0
ratrCleanUp(ratr, pLinePrice, linePrice, line, label) =>
    if ratr < 0 and pLinePrice != linePrice
        line.set_color(line, color.new(#000000, 100))
        label.set_textcolor(label, color.new(#000000, 100))

//Code to Detect New Day
if yRange[1] != yRange and timeframe.isintraday

    //Stop Old Lines Extensions If There're Any
    line.set_extend(openLine, extend.none)
    line.set_extend(highLine, extend.none)
    line.set_extend(lowLine, extend.none)

    line.set_extend(atr1LineU, extend.none)
    line.set_extend(atr1LineD, extend.none)
    line.set_extend(atr2LineU, extend.none)
    line.set_extend(atr2LineD, extend.none)
    line.set_extend(atr3LineU, extend.none)
    line.set_extend(atr3LineD, extend.none)
    
    //Delete Old Labels to Keep Charts Clean If There're Any
    label.delete(openLabel)
    label.delete(highLabel)
    label.delete(lowLabel)

    label.delete(irLabel)

    label.delete(atr1LabelU)
    label.delete(atr1LabelD)
    label.delete(atr2LabelU)
    label.delete(atr2LabelD)
    label.delete(atr3LabelU)
    label.delete(atr3LabelD)

    //Create New Lines
    openLine := line.new(bar_index, openD, bar_index, openD, color=openLineColor, style=style, width=openLineThickness)
    highLine := line.new(bar_index, highD, bar_index, highD, color=highDLineColor, style=style, width=baseLineThickness)
    lowLine := line.new(bar_index, lowD, bar_index, lowD, color=lowDLineColor, style=style, width=baseLineThickness)

    atr1LineU := line.new(bar_index, highD + remainingATR(atrRTHM1), bar_index, highD + remainingATR(atrRTHM1), color=atr1LineColor, style=style, width=atrThickness)
    atr1LineD := line.new(bar_index, lowD - remainingATR(atrRTHM1), bar_index, lowD - remainingATR(atrRTHM1), color=atr1LineColor, style=style, width=atrThickness)
    atr2LineU := line.new(bar_index, highD + remainingATR(atrRTHM2), bar_index, highD + remainingATR(atrRTHM2), color=atr2LineColor, style=style, width=atrThickness)
    atr2LineD := line.new(bar_index, lowD - remainingATR(atrRTHM2), bar_index, lowD - remainingATR(atrRTHM2), color=atr2LineColor, style=style, width=atrThickness)
    atr3LineU := line.new(bar_index, highD + remainingATR(atrRTHM3), bar_index, highD + remainingATR(atrRTHM3), color=atr3LineColor, style=style, width=atrThickness)
    atr3LineD := line.new(bar_index, lowD - remainingATR(atrRTHM3), bar_index, lowD - remainingATR(atrRTHM3), color=atr3LineColor, style=style, width=atrThickness)

    //Line Fills
    linefill.new(openLine, highLine, highDPlotFillColor)
    linefill.new(openLine, lowLine, lowDPlotFillColor)

    atr1FillUp := linefill.new(highLine, atr1LineU, atr1PlotFillColor)
    atr1FillDown := linefill.new(lowLine, atr1LineD, atr1PlotFillColor)
    atr2FillUp := linefill.new(highLine, atr2LineU, atr2PlotFillColor)
    atr2FillDown := linefill.new(lowLine, atr2LineD, atr2PlotFillColor)
    atr3FillUp := linefill.new(highLine, atr3LineU, atr3PlotFillColor)
    atr3FillDown := linefill.new(lowLine, atr3LineD, atr3PlotFillColor)

    //Create Labels
    openLabel := label.new(bar_index + labelOffset, openD, 'Open: ' + str.tostring(openD, format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=openLabelTextColor, size=size)
    highLabel := label.new(bar_index + labelOffset, highD, 'High: ' + str.tostring(highD, format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=highDLabelTextColor, size=size)
    lowLabel := label.new(bar_index + labelOffset, lowD, 'Low: ' + str.tostring(lowD, format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=lowDLabelTextColor, size=size)

    irLabel := label.new(bar_index + irLabelOffset, irYAxis, 'IR: ' + irPrice, color=color.new(#000000, 100), style=label.style_label_left, textcolor=irLabelTextColor, size=irSize)

    atr1LabelU := label.new(bar_index + labelOffset, highD + remainingATR(atrRTHM1), 'RATR #1: ' + str.tostring(highD + remainingATR(atrRTHM1), format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=atr1LabelTextColor, size=size)
    atr1LabelD := label.new(bar_index + labelOffset, lowD - remainingATR(atrRTHM1), 'RATR #1: ' + str.tostring(lowD - remainingATR(atrRTHM1), format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=atr1LabelTextColor, size=size)
    atr2LabelU := label.new(bar_index + labelOffset, highD + remainingATR(atrRTHM2), 'RATR #2: ' + str.tostring(highD + remainingATR(atrRTHM2), format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=atr2LabelTextColor, size=size)
    atr2LabelD := label.new(bar_index + labelOffset, lowD - remainingATR(atrRTHM2), 'RATR #2: ' + str.tostring(lowD - remainingATR(atrRTHM2), format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=atr2LabelTextColor, size=size)
    atr3LabelU := label.new(bar_index + labelOffset, highD + remainingATR(atrRTHM3), 'RATR #3: ' + str.tostring(highD + remainingATR(atrRTHM3), format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=atr3LabelTextColor, size=size)
    atr3LabelD := label.new(bar_index + labelOffset, lowD - remainingATR(atrRTHM3), 'RATR #3: ' + str.tostring(lowD - remainingATR(atrRTHM3), format.mintick), color=color.new(#000000, 100), style=label.style_label_left, textcolor=atr3LabelTextColor, size=size)

    //If Only Want Recent Lines On Chart
    if recentLinesOnly
        line.delete(openLine[1])
        line.delete(highLine[1])
        line.delete(lowLine[1])

        line.delete(atr1LineU[1])
        line.delete(atr1LineD[1])
        line.delete(atr2LineU[1])
        line.delete(atr2LineD[1])
        line.delete(atr3LineU[1])
        line.delete(atr3LineD[1])

    //Extending lines
    if extension
        line.set_extend(openLine, extend.right)
        line.set_extend(highLine, extend.right)
        line.set_extend(lowLine, extend.right)

        line.set_extend(atr1LineU, extend.right)
        line.set_extend(atr1LineD, extend.right)
        line.set_extend(atr2LineU, extend.right)
        line.set_extend(atr2LineD, extend.right)
        line.set_extend(atr3LineU, extend.right)
        line.set_extend(atr3LineD, extend.right)

//Code to Detect Intraday Range Change
if tRange[1] != tRange and rth and timeframe.isintraday

    //Delete Line Plot Fills
    linefill.delete(atr1FillUp)
    linefill.delete(atr1FillDown)
    linefill.delete(atr2FillUp)
    linefill.delete(atr2FillDown)
    linefill.delete(atr3FillUp)
    linefill.delete(atr3FillDown)

    //Create & Update Line Plot Fills
    atr1FillUp := linefill.new(highLine, atr1LineU, atr1PlotFillColor)
    atr1FillDown := linefill.new(lowLine, atr1LineD, atr1PlotFillColor)
    atr2FillUp := linefill.new(highD >= line.get_y1(atr1LineU) ? highLine : atr1LineU, atr2LineU, atr2PlotFillColor)
    atr2FillDown := linefill.new(lowD <= line.get_y1(atr1LineD) ? lowLine : atr1LineD, atr2LineD, atr2PlotFillColor)
    atr3FillUp := linefill.new(highD >= line.get_y1(atr2LineU) ? highLine : atr2LineU, atr3LineU, atr3PlotFillColor)
    atr3FillDown := linefill.new(lowD <= line.get_y1(atr2LineD) ? lowLine : atr2LineD, atr3LineD, atr3PlotFillColor)

    linefill.set_color(atr1FillUp, remainingATR(atrRTHM1) >= 0 ? atr1PlotFillColor : na)
    linefill.set_color(atr1FillDown, remainingATR(atrRTHM1) >= 0 ? atr1PlotFillColor : na)
    linefill.set_color(atr2FillUp, remainingATR(atrRTHM2) >= 0 ? atr2PlotFillColor : na)
    linefill.set_color(atr2FillDown, remainingATR(atrRTHM2) >= 0 ? atr2PlotFillColor : na)
    linefill.set_color(atr3FillUp, remainingATR(atrRTHM3) >= 0 ? atr3PlotFillColor : na)
    linefill.set_color(atr3FillDown, remainingATR(atrRTHM3) >= 0 ? atr3PlotFillColor : na)

    //Code For When RATR <= 0? i.e. RATR Lines Are Inside Range
    if remainingATR(atrRTHM1) <= 0

        line.set_color(atr1LineU, atr1LineInsideColor)
        line.set_color(atr1LineD, atr1LineInsideColor)

        label.set_textcolor(atr1LabelU, atr1LabelTextInsideColor)
        label.set_textcolor(atr1LabelD, atr1LabelTextInsideColor)

        if insideLineHide
            line.set_color(atr1LineU, color.new(#000000, 100))
            line.set_color(atr1LineD, color.new(#000000, 100))

        if insideLabelHide
            label.set_textcolor(atr1LabelU, color.new(#000000, 100))
            label.set_textcolor(atr1LabelD, color.new(#000000, 100))

    if remainingATR(atrRTHM2) <= 0

        line.set_color(atr2LineU, atr2LineInsideColor)
        line.set_color(atr2LineD, atr2LineInsideColor)

        label.set_textcolor(atr2LabelU, atr2LabelTextInsideColor)
        label.set_textcolor(atr2LabelD, atr2LabelTextInsideColor)

        if insideLineHide
            line.set_color(atr2LineU, color.new(#000000, 100))
            line.set_color(atr2LineD, color.new(#000000, 100))

        if insideLabelHide
            label.set_textcolor(atr2LabelU, color.new(#000000, 100))
            label.set_textcolor(atr2LabelD, color.new(#000000, 100))

    if remainingATR(atrRTHM3) <= 0

        line.set_color(atr3LineU, atr3LineInsideColor)
        line.set_color(atr3LineD, atr3LineInsideColor)

        label.set_textcolor(atr3LabelU, atr3LabelTextInsideColor)
        label.set_textcolor(atr3LabelD, atr3LabelTextInsideColor)

        if insideLineHide
            line.set_color(atr3LineU, color.new(#000000, 100))
            line.set_color(atr3LineD, color.new(#000000, 100))

        if insideLabelHide
            label.set_textcolor(atr3LabelU, color.new(#000000, 100))
            label.set_textcolor(atr3LabelD, color.new(#000000, 100))
    
    //Remove Moving RATR Lines To Prevent Visual Confusion when RATR < 0
    ratrCleanUp(remainingATR(atrRTHM1), line.get_y1(atr1LineU)[1], line.get_y1(atr1LineU), atr1LineU, atr1LabelU)
    ratrCleanUp(remainingATR(atrRTHM1), line.get_y1(atr1LineD)[1], line.get_y1(atr1LineD), atr1LineD, atr1LabelD)
    ratrCleanUp(remainingATR(atrRTHM2), line.get_y1(atr2LineU)[1], line.get_y1(atr2LineU), atr2LineU, atr2LabelU)
    ratrCleanUp(remainingATR(atrRTHM2), line.get_y1(atr2LineD)[1], line.get_y1(atr2LineD), atr2LineD, atr2LabelD)
    ratrCleanUp(remainingATR(atrRTHM3), line.get_y1(atr3LineU)[1], line.get_y1(atr3LineU), atr3LineU, atr3LabelU)
    ratrCleanUp(remainingATR(atrRTHM3), line.get_y1(atr3LineD)[1], line.get_y1(atr3LineD), atr3LineD, atr3LabelD)

    //Update Line Coordinates
    line.set_x2(openLine, bar_index)
    line.set_x2(highLine, bar_index)
    line.set_x2(lowLine, bar_index)

    line.set_x2(atr1LineU, bar_index)
    line.set_x2(atr1LineD, bar_index)
    line.set_x2(atr2LineU, bar_index)
    line.set_x2(atr2LineD, bar_index)
    line.set_x2(atr3LineU, bar_index)
    line.set_x2(atr3LineD, bar_index)

    line.set_y1(highLine, highD)
    line.set_y2(highLine, highD)
    line.set_y1(lowLine, lowD)
    line.set_y2(lowLine, lowD)

    line.set_y1(atr1LineU, highD + remainingATR(atrRTHM1))
    line.set_y2(atr1LineU, highD + remainingATR(atrRTHM1))
    line.set_y1(atr1LineD, lowD - remainingATR(atrRTHM1))
    line.set_y2(atr1LineD, lowD - remainingATR(atrRTHM1))
    line.set_y1(atr2LineU, highD + remainingATR(atrRTHM2))
    line.set_y2(atr2LineU, highD + remainingATR(atrRTHM2))
    line.set_y1(atr2LineD, lowD - remainingATR(atrRTHM2))
    line.set_y2(atr2LineD, lowD - remainingATR(atrRTHM2))
    line.set_y1(atr3LineU, highD + remainingATR(atrRTHM3))
    line.set_y2(atr3LineU, highD + remainingATR(atrRTHM3))
    line.set_y1(atr3LineD, lowD - remainingATR(atrRTHM3))
    line.set_y2(atr3LineD, lowD - remainingATR(atrRTHM3))
 
    //Update Label Coordinates
    label.set_x(openLabel, bar_index + labelOffset)
    label.set_x(highLabel, bar_index + labelOffset)
    label.set_x(lowLabel, bar_index + labelOffset)

    label.set_x(irLabel, bar_index + irLabelOffset)

    label.set_x(atr1LabelU, bar_index + labelOffset)
    label.set_x(atr1LabelD, bar_index + labelOffset)
    label.set_x(atr2LabelU, bar_index + labelOffset)
    label.set_x(atr2LabelD, bar_index + labelOffset)
    label.set_x(atr3LabelU, bar_index + labelOffset)
    label.set_x(atr3LabelD, bar_index + labelOffset)

    label.set_y(highLabel, highD)
    label.set_y(lowLabel, lowD)

    label.set_y(irLabel, irYAxis)

    label.set_y(atr1LabelU, highD + remainingATR(atrRTHM1))
    label.set_y(atr1LabelD, lowD - remainingATR(atrRTHM1))
    label.set_y(atr2LabelU, highD + remainingATR(atrRTHM2))
    label.set_y(atr2LabelD, lowD - remainingATR(atrRTHM2))
    label.set_y(atr3LabelU, highD + remainingATR(atrRTHM3))
    label.set_y(atr3LabelD, lowD - remainingATR(atrRTHM3))

    //Update Label Text
    label.set_text(highLabel, 'High: ' + str.tostring(highD, format.mintick))
    label.set_text(lowLabel, 'Low: ' + str.tostring(lowD, format.mintick))

    label.set_text(irLabel, 'IR: ' + irPrice)

    label.set_text(atr1LabelU, 'RATR #1: ' + str.tostring(highD + remainingATR(atrRTHM1), format.mintick))
    label.set_text(atr1LabelD, 'RATR #1: ' + str.tostring(lowD - remainingATR(atrRTHM1), format.mintick))
    label.set_text(atr2LabelU, 'RATR #2: ' + str.tostring(highD + remainingATR(atrRTHM2), format.mintick))
    label.set_text(atr2LabelD, 'RATR #2: ' + str.tostring(lowD - remainingATR(atrRTHM2), format.mintick))
    label.set_text(atr3LabelU, 'RATR #3: ' + str.tostring(highD + remainingATR(atrRTHM3), format.mintick))
    label.set_text(atr3LabelD, 'RATR #3: ' + str.tostring(lowD - remainingATR(atrRTHM3), format.mintick))

//Code to Detect No Intraday Range Change
if tRange[1] == tRange and rth and timeframe.isintraday

    //Update Line Coordinates
    line.set_x2(openLine, bar_index)
    line.set_x2(highLine, bar_index)
    line.set_x2(lowLine, bar_index)

    line.set_x2(atr1LineU, bar_index)
    line.set_x2(atr1LineD, bar_index)
    line.set_x2(atr2LineU, bar_index)
    line.set_x2(atr2LineD, bar_index)
    line.set_x2(atr3LineU, bar_index)
    line.set_x2(atr3LineD, bar_index)

    //Update Label Coordinates
    label.set_x(openLabel, bar_index + labelOffset)
    label.set_x(highLabel, bar_index + labelOffset)
    label.set_x(lowLabel, bar_index + labelOffset)

    label.set_x(irLabel, bar_index + irLabelOffset)
    label.set_y(irLabel, irYAxis)

    label.set_x(atr1LabelU, bar_index + labelOffset)
    label.set_x(atr1LabelD, bar_index + labelOffset)
    label.set_x(atr2LabelU, bar_index + labelOffset)
    label.set_x(atr2LabelD, bar_index + labelOffset)
    label.set_x(atr3LabelU, bar_index + labelOffset)
    label.set_x(atr3LabelD, bar_index + labelOffset)

//Code to Detect ETH Range
if not rth and timeframe.isintraday
    //Stop Old Lines Extensions If There're Any
    line.set_extend(openLine, extend.none)
    line.set_extend(highLine, extend.none)
    line.set_extend(lowLine, extend.none)

    line.set_extend(atr1LineU, extend.none)
    line.set_extend(atr1LineD, extend.none)
    line.set_extend(atr2LineU, extend.none)
    line.set_extend(atr2LineD, extend.none)
    line.set_extend(atr3LineU, extend.none)
    line.set_extend(atr3LineD, extend.none)