MTF previous high and low quarter levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © geneclash
//
// Acknowledgement:
//
// @HeWhoMustNotBeNamed - Previous High/Low MTF
// https://www.tradingview.com/script/DNqSoMlx-Previous-High-Low-MTF/
//
//@version=5
indicator('MTF previous high and low quarter levels', overlay=true, max_lines_count=500, max_labels_count=500)
tf1 = input.timeframe(defval='D', title='Timeframe for High/Low')
showhist = input(defval=false, title='Show Historical Lines')
showlabel = input(defval=false, title='Show Price Labels')

linestyle2 = input.string(defval='Solid', options=['Solid', 'Dotted', 'Dashed'], group='Lines')
linestyle1 = linestyle2 == 'Solid' ? line.style_solid : linestyle2 == 'Dotted' ? line.style_dotted : line.style_dashed
lwidth = input.int(defval=1, minval=1, maxval=4, title='Line Width', group='Lines')
color1 = input.color(defval=color.orange, title='Color (Lines & Labels)', group='Lines')


hhigh = request.security(syminfo.ticker, tf1, high[1], lookahead=barmerge.lookahead_on)
hlow = request.security(syminfo.ticker, tf1, low[1], lookahead=barmerge.lookahead_on)
hbar = request.security(syminfo.ticker, tf1, barstate.islast)


// hline(42500, color = color.white)
// plot(hhigh,color = hhigh==hhigh[1] and showhist ? color.green : color.new(color.green,100))
// plot(hlow,color = hlow==hlow[1] and showhist ? color.red : color.new(color.red,100))

range1 = (hhigh - hlow) / 4
zerolevel = hlow

var line highline = na
var line lowline = na

// var label highlabel = na
// var label lowlabel = na

var lines = array.new_line()
var labels = array.new_label()

var label highlabel = label.new(bar_index + 1, hhigh, style=label.style_none, text=str.tostring(hhigh), textcolor=color1)
var label lowlabel = label.new(bar_index + 1, hlow, style=label.style_none, text=str.tostring(hlow), textcolor=color1)

if hhigh != hhigh[1] or hlow != hlow[1]
    if not showhist
        line.delete(highline)
        line.delete(lowline)
        label.delete(highlabel)
        label.delete(lowlabel)



    highline := line.new(bar_index, hhigh, bar_index + 1, hhigh, color=color1, style=linestyle1, width=lwidth)
    lowline := line.new(bar_index, hlow, bar_index + 1, hlow, color=color1, style=linestyle1, width=lwidth)
    if showlabel
        highlabel := label.new(bar_index + 1, hhigh, style=label.style_none, text=str.tostring(hhigh), textcolor=color1)
        lowlabel := label.new(bar_index + 1, hlow, style=label.style_none, text=str.tostring(hlow), textcolor=color1)
        label.set_y(highlabel, hhigh)
        label.set_y(lowlabel, hlow)
        label.set_text(highlabel, str.tostring(hhigh) + '-(1.0)')
        label.set_text(lowlabel, str.tostring(hlow) + '-(0.0)')

label.set_x(highlabel, bar_index + 2)
label.set_x(lowlabel, bar_index + 2)

line.set_x2(highline, bar_index + 1)
line.set_x2(lowline, bar_index + 1)

if not showlabel
    label.delete(highlabel)
    label.delete(lowlabel)

if hhigh != hhigh[1] or hlow != hlow[1]
    if not showhist and array.size(lines) > 0
        for i = 0 to array.size(lines) - 1 by 1
            line.delete(array.get(lines, i))
            label.delete(array.get(labels, i))
    array.clear(lines)
    array.clear(labels)
    for i = 1 to 10 by 1
        shigh = zerolevel + i * range1
        slow = zerolevel - i * range1

            // line.delete(lowline)


        if i != 4
            array.push(lines, line.new(bar_index, shigh, bar_index + 1, shigh, color=color1, style=linestyle1, width=lwidth))
            array.push(labels, label.new(bar_index, shigh, text=str.tostring(shigh), textcolor=showlabel ? color1 : color.new(color1, 100), style=label.style_none))

        array.push(lines, line.new(bar_index, slow, bar_index + 1, slow, color=color1, style=linestyle1, width=lwidth))
        array.push(labels, label.new(bar_index, slow, text=str.tostring(slow), textcolor=showlabel ? color1 : color.new(color1, 100), style=label.style_none))
        // label.set_y(highlabel,hhigh)
        // label.set_y(lowlabel,hlow)
        // label.set_text(highlabel,tostring(hhigh) + "(1)")
        // label.set_text(lowlabel,tostring(hlow) + "(0)")

    // label.set_x(highlabel,bar_index+2)
    // label.set_x(lowlabel,bar_index+2)
if array.size(lines) > 0
    for i = 0 to array.size(lines) - 1 by 1
        line.set_x2(array.get(lines, i), bar_index + 1)
        label.set_x(array.get(labels, i), bar_index + 2)

// if array.size(labels)>0 and not showlabel
//     for i = 0 to (array.size(labels)-1) 
//         label.delete(array.get(labels,i))
//     array.clear(labels)    
// label.new(bar_index,high,text = tostring(array.size(lines)))  
// line.set_x2(highline,bar_index+1)
// line.set_x2(lowline,bar_index+1)

// if not showlabel
//     label.delete(highlabel)
//     label.delete(lowlabel)





