HTF Support & Resistance [QuantVue]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantVue

//@version=5
indicator("HTF Support & Resistance [QuantVue]", overlay = true, shorttitle = 'HTF Support & Resistance [QuantVue]')

// inputs
var g1              = 'Daily Levels'
showDaily           = input.bool(true, 'Show Daily Levels', group = g1)
dayOpenCol          = input.color(color.orange, 'Daily Open Color', inline = '1', group = g1)
dayOpenStyle        = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '1', group = g1)
dayHighCol          = input.color(color.red, 'Daily High Color', inline = '2', group = g1)
dayHighStyle        = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '2', group = g1)
dayLowCol           = input.color(color.lime, 'Daily Low Color', inline = '3', group = g1)
dayLowStyle         = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '3', group = g1)

showPrevDay         = input.bool(true, 'Show Previous Days Levels', group = g1)
prevDayOpenCol      = input.color(color.orange, 'Previous Days Open Color', inline = '4', group = g1)
prevDayOpenStyle    = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '4', group = g1)
prevDayHighCol      = input.color(color.red, 'Previous Days High Color', inline = '5', group = g1)
prevDayHighStyle    = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '5', group = g1)
prevDayLowCol       = input.color(color.lime, 'Previous Days Low Color', inline = '6', group = g1)
prevDayLowStyle     = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '6', group = g1)

var g2              = 'Weekly Levels'
showWeekly          = input.bool(true, 'Show Weekly Levels', group = g2)
weekOpenCol         = input.color(color.orange, 'Weekly Open Color', inline = '7', group = g2)
weekOpenStyle       = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '7', group = g2)
weekHighCol         = input.color(color.red, 'Weekly High Color', inline = '8', group = g2)
weekHighStyle       = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '8', group = g2)
weekLowCol          = input.color(color.lime, 'Weekly Low Color', inline = '9', group = g2)
weekLowStyle        = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '9', group = g2)

showPrevWeek        = input.bool(true, 'Show Previous Weeks Levels', group = g2)
prevWeekOpenCol     = input.color(color.orange, 'Previous Weeks Open Color', inline = '10', group = g2)
prevWeekOpenStyle   = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '10', group = g2)
prevWeekHighCol     = input.color(color.red, 'Previous Weeks High Color', inline = '11', group = g2)
prevWeekHighStyle   = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '11', group = g2)
prevWeekLowCol      = input.color(color.lime, 'Previous Weeks Low Color', inline = '12', group = g2)
prevWeekLowStyle    = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '12', group = g2)

var g3              = 'Monthly Levels'
showMonthly         = input.bool(true, 'Show Monthly Levels', group = g3)
monthOpenCol        = input.color(color.orange, 'Monthly Open Color', inline = '13', group = g3)
monthOpenStyle      = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '13', group = g3)
monthHighCol        = input.color(color.red, 'Monthly High Color', inline = '14', group = g3)
monthHighStyle      = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '14', group = g3)
monthLowCol         = input.color(color.lime, 'Monthly Low Color', inline = '15', group = g3)
monthLowStyle       = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '15', group = g3)

showPrevMonth       = input.bool(false, 'Show Previous Months Levels', group = g3)
prevMonthOpenCol    = input.color(color.orange, 'Previous Months Open Color', inline = '16', group = g3)
prevMonthOpenStyle  = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '16', group = g3)
prevMonthHighCol    = input.color(color.red, 'Previous Months High Color', inline = '17', group = g3)
prevMonthHighStyle  = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '17', group = g3)
prevMonthLowCol     = input.color(color.lime, 'Previous Months Low Color', inline = '18', group = g3)
prevMonthLowStyle   = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '18', group = g3)

var g4              = 'Quarterly Levels'
showQtr             = input.bool(false, 'Show Quarterly Levels', group = g4)
qtrOpenCol          = input.color(color.orange, 'Quarterly Open Color', inline = '19', group = g4)
qtrOpenStyle        = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '19', group = g4)
qtrHighCol          = input.color(color.red, 'Quarterly High Color', inline = '20', group = g4)
qtrHighStyle        = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '20', group = g4)
qtrLowCol           = input.color(color.lime, 'Quarterly Low Color', inline = '21', group = g4)
qtrLowStyle         = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '21', group = g4)

showPrevQtr         = input.bool(false, 'Show Previous Quarters Levels', group = g4)
prevQtrOpenCol      = input.color(color.orange, 'Previous Quarterss Open Color', inline = '22', group = g4)
prevQtrOpenStyle    = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '22', group = g4)
prevQtrHighCol      = input.color(color.red, 'Previous Quarterss High Color', inline = '23', group = g4)
prevQtrHighStyle    = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '23', group = g4)
prevQtrLowCol       = input.color(color.lime, 'Previous Quarterss Low Color', inline = '24', group = g4)
prevQtrLowStyle     = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '24', group = g4)

var g5              = 'Yearly Levels'
showYear            = input.bool(false, 'Show Yearly Levels', group = g5)
yearOpenCol         = input.color(color.orange, 'Yearly Open Color', inline = '25', group = g5)
yearOpenStyle       = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '25', group = g5)
yearHighCol         = input.color(color.red, 'Yearly High Color', inline = '26', group = g5)
yearHighStyle       = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '26', group = g5)
yearLowCol          = input.color(color.lime, 'Yearly Low Color', inline = '27', group = g5)
yearLowStyle        = input.string('Solid', '', options = ['Solid', 'Dashed', 'Dotted'], inline = '27', group = g5)

var g6              = 'Misc Options'
extendLines         = input.bool(false, 'Extend Lines', group = g6)

//methods switches and functions
extended = switch extendLines
    true => extend.right
    false => extend.none

method styler(string this) =>
    switch this
        'Solid' => line.style_solid
        'Dashed' => line.style_dashed
        'Dotted' => line.style_dotted
       
getHTF(htf,x,idx,oCol,oStyle,hCol,hStyle,lCol,lStyle,oStr,hStr,lStr) =>
    [o,h,l] = request.security(syminfo.tickerid, htf, [open[x], high[x], low[x]])
    line.new(idx,o,chart.right_visible_bar_time,o,xloc=xloc.bar_time,extend=extended,color=oCol,style=oStyle.styler())
    line.new(idx,h,chart.right_visible_bar_time,h,xloc=xloc.bar_time,extend=extended,color=hCol,style=hStyle.styler())
    line.new(idx,l,chart.right_visible_bar_time,l,xloc=xloc.bar_time,extend=extended,color=lCol,style=lStyle.styler())
    label.new(bar_index+5,o,oStr,style=label.style_none,textcolor=oCol)
    label.new(bar_index+5,h,hStr,style=label.style_none,textcolor=hCol)
    label.new(bar_index+5,l,lStr,style=label.style_none,textcolor=lCol)

// index variables
var dayIndex        = 0 
var weekIndex       = 0 
var monthIndex      = 0 
var qtrIndex        = 0
var prevDayIndex    = 0 
var prevWeekIndex   = 0 
var prevMonthIndex  = 0 
var prevQtrIndex    = 0
var yearIndex       = 0 
var prevYearIndex   = 0

// new times
newDay      = ta.change(time('D'))
newWeek     = ta.change(time('W'))
newMonth    = ta.change(time('M'))
newQtr      = ta.change(time('3M'))
newYear     = ta.change(time('12M'))

// set the starting points for lines
if newDay
    prevDayIndex := dayIndex
    dayIndex := time
if newWeek
    prevWeekIndex := weekIndex
    weekIndex := time
if newMonth
    prevMonthIndex := monthIndex
    monthIndex := time
if newQtr
    prevQtrIndex := qtrIndex
    qtrIndex := time
if newYear
    prevYearIndex := yearIndex
    yearIndex := time

//delete previous lines
lines = line.all
for li in lines
    li.delete()
labels = label.all
for la in labels
    la.delete()

//run the code
if showDaily
    getHTF('D',0,dayIndex,dayOpenCol,dayOpenStyle,dayHighCol,dayHighStyle,dayLowCol,dayLowStyle,'Open','High','Low')
if showPrevDay
    getHTF('D',1,prevDayIndex,prevDayOpenCol,prevDayOpenStyle,prevDayHighCol,prevDayHighStyle,prevDayLowCol,prevDayLowStyle,'PDO','PDH','PDL')
if showWeekly
    getHTF('W',0,weekIndex,weekOpenCol,weekOpenStyle,weekHighCol,weekHighStyle,weekLowCol,weekLowStyle,'Week Open','Week High','Week Low')
if showPrevWeek
    getHTF('W',1,prevWeekIndex,prevWeekOpenCol,prevWeekOpenStyle,prevWeekHighCol,prevWeekHighStyle,prevWeekLowCol,prevWeekLowStyle,'PWO','PWH','PWL')
if showMonthly
    getHTF('M',0,monthIndex,monthOpenCol,monthOpenStyle,monthHighCol,monthHighStyle,monthLowCol,monthLowStyle,'Month Open','Month High','Month Low')
if showPrevMonth
    getHTF('M',1,prevMonthIndex,prevMonthOpenCol,prevMonthOpenStyle,prevMonthHighCol,prevMonthHighStyle,prevMonthLowCol,prevMonthLowStyle,'PMO','PMH','PML')
if showQtr
    getHTF('3M',0,qtrIndex,qtrOpenCol,qtrOpenStyle,qtrHighCol,qtrHighStyle,qtrLowCol,qtrLowStyle,'Quarter Open','Quarter High','Quarter Low')
if showPrevQtr
    getHTF('3M',1,prevQtrIndex,prevQtrOpenCol,prevQtrOpenStyle,prevQtrHighCol,prevQtrHighStyle,prevQtrLowCol,prevQtrLowStyle,'PQO','PQH','PQL')
if showYear
    getHTF('12M',0,yearIndex,yearOpenCol,yearOpenStyle,yearHighCol,yearHighStyle,yearLowCol,yearLowStyle,'Year Open','Year High','Year Low')
