Average Range @coldbrewrosh
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © coldbrewrosh

//@version=5
indicator(title="Average Range @coldbrewrosh", overlay=true, max_bars_back = 1000)

Bool_5D     = input(true, "5 Day AR", group="AR")
Bool_1M     = input(true, "1 Month AR", group="AR")
Bool_3M     = input(true, "3 Month AR", group = "AR")
Bool_1Y     = input(true, "1 Year AR", group = "AR")

Bool_5D_Opp = input(true, "5 Day Opposite Move AR", group="Opposite AR")
Bool_1M_Opp = input(true, "1 Month Opposite Move AR", group="Opposite AR")
Bool_3M_Opp = input(true, "3 Month Opposite Move AR", group="Opposite AR")
Bool_1Y_Opp = input(true, "1 Year Opposite Move AR", group="Opposite AR")

DHigh       = request.security(syminfo.tickerid, 'D', high)
DLow        = request.security(syminfo.tickerid, 'D', low)
DOpen       = request.security(syminfo.tickerid, 'D', open)
DClose      = request.security(syminfo.tickerid, 'D', close)
// Function to convert forex pips into whole numbers
atr = ta.atr(14)
toWhole(number) =>
    if syminfo.type == "forex" // This method only works on forex pairs
        _return = atr < 1.0 ? (number / syminfo.mintick) / 10 : number
        _return := atr >= 1.0 and atr < 100.0 and syminfo.currency == "JPY" ? _return * 100 : _return
    else
        number

BullATR(period)=>
    j               = 0
    BullATR         = 0.0
    BullATRCount    = 0
    for i = 1 to 365
        if DOpen[j] < DClose[j]
            BullATR := BullATR + ( DHigh[j] - DOpen[j] )
            BullATRCount := BullATRCount + 1
        if BullATRCount == period
            break
        j := j + 1
    j := 0
    _return = math.round(toWhole(BullATR / period), 2)

BearATR(period)=>
    j               = 0
    BearATR         = 0.0
    BearATRCount    = 0
    for i = 1 to 365
        if DOpen[j] > DClose[j]
            BearATR := BearATR + ( DOpen[j] - DLow[j] )
            BearATRCount := BearATRCount + 1
        if BearATRCount == period
            break
        j := j + 1
    j := 0
    _return = math.round(toWhole(BearATR / period), 2)

BullOpp(period)=>
    j               = 0
    BullATR         = 0.0
    BullATRCount    = 0
    for i = 1 to 365
        if DOpen[j] < DClose[j]
            BullATR := BullATR + ( DOpen[j] - DLow[j] )
            BullATRCount := BullATRCount + 1
        if BullATRCount == period
            break
        j := j + 1
    j := 0
    _return = math.round(toWhole(BullATR / period), 2)

BearOpp(period)=>
    j               = 0
    BearATR         = 0.0
    BearATRCount    = 0
    for i = 1 to 365
        if DOpen[j] > DClose[j]
            BearATR := BearATR + ( DHigh[j] - DOpen[j] )
            BearATRCount := BearATRCount + 1
        if BearATRCount == period
            break
        j := j + 1
    j := 0
    _return = math.round(toWhole(BearATR / period), 2)

BullTR5 = BullATR(5)
BearTR5 = BearATR(5)
BullTR20 = BullATR(20)
BearTR20 = BearATR(20)
BullTR60 = BullATR(60)
BearTR60 = BearATR(60)
BullTR240 = BullATR(240)
BearTR240 = BearATR(240)

BullOR5 = BullOpp(5)
BearOR5 = BearOpp(5)
BullOR20 = BullOpp(20)
BearOR20 = BearOpp(20)
BullOR60 = BullOpp(60)
BearOR60 = BearOpp(60)
BullOR240 = BullOpp(240)
BearOR240 = BearOpp(240)

var table ATR = table.new(position.top_right, columns = 2, rows = 8, border_width=1)
if barstate.islast
    if Bool_5D == true
        table.cell(ATR, column = 0, row = 0, text=str.tostring("Bullish 5 Day Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 0, text=(str.tostring(BullTR5) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(ATR, column = 0, row = 1, text=str.tostring("Bearish 5 Day Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 1, text=(str.tostring(BearTR5) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
    if Bool_1M == true
        table.cell(ATR, column = 0, row = 2, text=str.tostring("Bullish 1M Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 2, text=(str.tostring(BullTR20) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(ATR, column = 0, row = 3, text=str.tostring("Bearish 1M Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 3, text=(str.tostring(BearTR20) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
    if Bool_3M == true
        table.cell(ATR, column = 0, row = 4, text=str.tostring("Bullish 3M Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 4, text=(str.tostring(BullTR60) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(ATR, column = 0, row = 5, text=str.tostring("Bearish 3M Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 5, text=(str.tostring(BearTR60) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
    if Bool_1Y == true
        table.cell(ATR, column = 0, row = 6, text=str.tostring("Bullish 1Y Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 6, text=(str.tostring(BullTR240) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(ATR, column = 0, row = 7, text=str.tostring("Bearish 1Y Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(ATR, column = 1, row = 7, text=(str.tostring(BearTR240) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)


var table OPPR = table.new(position.bottom_right, columns = 2, rows = 8, border_width=1)
if barstate.islast
    if Bool_5D_Opp == true
        table.cell(OPPR, column = 0, row = 0, text=str.tostring("Bullish 5 Day Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 0, text=(str.tostring(BullOR5) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(OPPR, column = 0, row = 1, text=str.tostring("Bearish 5 Day Open to High   ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 1, text=("  " + str.tostring(BearOR5) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
    if Bool_1M_Opp == true
        table.cell(OPPR, column = 0, row = 2, text=str.tostring("Bullish 1M Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 2, text=(str.tostring(BullOR20) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(OPPR, column = 0, row = 3, text=str.tostring("Bearish 1M Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 3, text=(str.tostring(BearOR20) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
    if Bool_3M_Opp == true
        table.cell(OPPR, column = 0, row = 4, text=str.tostring("Bullish 3M Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 4, text=(str.tostring(BullOR60) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(OPPR, column = 0, row = 5, text=str.tostring("Bearish 3M Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 5, text=(str.tostring(BearOR60) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
    if Bool_1Y_Opp == true
        table.cell(OPPR, column = 0, row = 6, text=str.tostring("Bullish 1Y Open to Low ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 6, text=(str.tostring(BullOR240) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)

        table.cell(OPPR, column = 0, row = 7, text=str.tostring("Bearish 1Y Open to High ") , bgcolor=color.black, text_color=color.white, text_halign=text.align_left, text_size=size.auto)
        table.cell(OPPR, column = 1, row = 7, text=(str.tostring(BearOR240) + " pips") , bgcolor=color.black, text_color=color.white, text_halign=text.align_right, text_size=size.auto)
