Higher Time Frame Average True Ranges
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ssxsnowboarder inspired by TOS users 7of9 and John J. Smith
//This code compares the calculates the daily, weekly, and monthly true ranges and compares it to their average true ranges using a higher time frame function.
//The goal is to help traders select the most probable strike price for an given expiration and determine possible exit strategies.
//@version=5

indicator(title="Higher Time Frame Average True Ranges", shorttitle="HTF ATR", overlay=true)

//Average True Range
length = input.int(title="ATR Length", defval=1, minval=1)
ATRlength = input.int(title="ATR Length", defval=14, minval=1)
DTR = ta.atr(length)
ATR = ta.atr(ATRlength)

//Defines higher time frames for ATR
string tf = ""
security(sym, res, src) =>
    request.security(sym, res, src)

//DTR/ATR
DTRpct = math.round(DTR/ATR*100, 0)

//WTR/ATR
float WATR = security(syminfo.tickerid, 'W', ATR)
float WTR = security(syminfo.tickerid, 'W', DTR)
WTRpct = math.round(WTR/WATR*100, 0)

//MTR/ATR
float MATR = security(syminfo.tickerid, 'M', ATR)
float MTR = security(syminfo.tickerid, 'M', DTR)
MTRpct = math.round(MTR/MATR*100, 0)

//QTR/ATR
float QATR = security(syminfo.tickerid, '3M', ATR)
float QTR = security(syminfo.tickerid, '3M', DTR)
QTRpct = math.round(QTR/QATR*100, 0)

//Initialize color variables
DCLR=input.color(color.orange)
WCLR=input.color(color.orange)
MCLR=input.color(color.orange)
QCLR=input.color(color.orange)

//Selects DTR Color
if(DTRpct<81)
    DCLR:=input.color(color.green)
else if(DTRpct>=100)
    DCLR:=input.color(color.red)
else
    DCLR:=input.color(color.orange)

//Selects WTR Color
if(WTRpct<81)
    WCLR:=input.color(color.green)
else if(WTRpct>=100)
    WCLR:=input.color(color.red)
else
    WCLR:=input.color(color.orange)

//Selects MTR Color
if(MTRpct<81)
    MCLR:=input.color(color.green)
else if(MTRpct>=100)
    MCLR:=input.color(color.red)
else
    MCLR:=input.color(color.orange)
//Selects QTR Color
if(QTRpct<81)
    QCLR:=input.color(color.green)
else if(QTRpct>=100)
    QCLR:=input.color(color.red)
else
    QCLR:=input.color(color.orange)

//Creates a table for the ratios
var table table = table.new(position.top_right, 7, 4)
//Creates DTR/ATR Row
table.cell(table, 0, 0, str.tostring("DTR"), bgcolor=DCLR)//Titles and fills table
table.cell(table, 1, 0, str.tostring(math.round(DTR,2)), bgcolor=DCLR)//Titles and fills table
table.cell(table, 2, 0, str.tostring("vs"), bgcolor=DCLR)//Titles and fills table
table.cell(table, 3, 0, str.tostring("DATR"), bgcolor=DCLR)//Titles and fills table
table.cell(table, 4, 0, str.tostring(math.round(ATR,2)), bgcolor=DCLR)//Titles and fills table
table.cell(table, 5, 0, str.tostring(DTRpct), bgcolor=DCLR)//Titles and fills table
table.cell(table, 6, 0, str.tostring("%"), bgcolor=DCLR)//Titles and fills table
//Creates WTR/ATR Row
table.cell(table, 0, 1, str.tostring("WTR"), bgcolor=WCLR)//Titles and fills table
table.cell(table, 1, 1, str.tostring(math.round(WTR,2)), bgcolor=WCLR)//Titles and fills table
table.cell(table, 2, 1, str.tostring("vs"), bgcolor=WCLR)//Titles and fills table
table.cell(table, 3, 1, str.tostring("WATR"), bgcolor=WCLR)//Titles and fills table
table.cell(table, 4, 1, str.tostring(math.round(WATR,2)), bgcolor=WCLR)//Titles and fills table
table.cell(table, 5, 1, str.tostring(WTRpct), bgcolor=WCLR)//Titles and fills table
table.cell(table, 6, 1, str.tostring("%"), bgcolor=WCLR)//Titles and fills table
//Creates MTR/ATR Row
table.cell(table, 0, 2, str.tostring("MTR"), bgcolor=MCLR)//Titles and fills table
table.cell(table, 1, 2, str.tostring(math.round(MTR,2)), bgcolor=MCLR)//Titles and fills table
table.cell(table, 2, 2, str.tostring("vs"), bgcolor=MCLR)//Titles and fills table
table.cell(table, 3, 2, str.tostring("MATR"), bgcolor=MCLR)//Titles and fills table
table.cell(table, 4, 2, str.tostring(math.round(MATR,2)), bgcolor=MCLR)//Titles and fills table
table.cell(table, 5, 2, str.tostring(MTRpct), bgcolor=MCLR)//Titles and fills table
table.cell(table, 6, 2, str.tostring("%"), bgcolor=MCLR)//Titles and fills table
//Creates QTR/ATR Row
table.cell(table, 0, 3, str.tostring("QTR"), bgcolor=QCLR)//Titles and fills table
table.cell(table, 1, 3, str.tostring(math.round(QTR,2)), bgcolor=QCLR)//Titles and fills table
table.cell(table, 2, 3, str.tostring("vs"), bgcolor=QCLR)//Titles and fills table
table.cell(table, 3, 3, str.tostring("QATR"), bgcolor=QCLR)//Titles and fills table
table.cell(table, 4, 3, str.tostring(math.round(QATR,2)), bgcolor=QCLR)//Titles and fills table
table.cell(table, 5, 3, str.tostring(QTRpct), bgcolor=QCLR)//Titles and fills table
table.cell(table, 6, 3, str.tostring("%"), bgcolor=QCLR)//Titles and fills table


//DTR, WTR, MTR, and QTR Plots
float UpperDTR=open+ATR
float LowerDTR=open-ATR
float UpperWTR = security(syminfo.tickerid, 'W', open)+WATR
float LowerWTR = security(syminfo.tickerid, 'W', open)-WATR
float UpperMTR = security(syminfo.tickerid, 'M', open)+MATR
float LowerMTR = security(syminfo.tickerid, 'M', open)-MATR
float UpperQTR = security(syminfo.tickerid, '3M', open)+QATR
float LowerQTR = security(syminfo.tickerid, '3M', open)-QATR
plot(UpperDTR, title="Upper DTR", color=color.red, linewidth=1, show_last = 1, trackprice = true)
plot(LowerDTR, title="Lower DTR", color=color.red, linewidth=1, show_last = 1, trackprice = true)
plot(UpperWTR, title="Upper WTR", color=color.green, linewidth=1, show_last = 1, trackprice = true)
plot(LowerWTR, title="Lower WTR", color=color.green, linewidth=1, show_last = 1, trackprice = true)
plot(UpperMTR, title="Upper MTR", color=color.blue, linewidth=1, show_last = 1, trackprice = true)
plot(LowerMTR, title="Lower MTR", color=color.blue, linewidth=1, show_last = 1, trackprice = true)
plot(UpperQTR, title="Upper QTR", color=color.purple, linewidth=1, show_last = 1, trackprice = true)
plot(LowerQTR, title="Lower QTR", color=color.purple, linewidth=1, show_last = 1, trackprice = true)