Distance from the High/Low price
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Seungdori_

//@version=5
indicator('Distance from the High/Low price', precision=3, overlay=false)

// Input//
length = input.int(defval = 400, title = 'Lengths', minval = 1, group = 'High/Low')
src = input.string(defval = 'High/Low', options = ['Close', 'High/Low'], group = 'High/Low')
show_maxmin = input.bool(defval = false, title = 'Show Maximum High Line ?', group = 'High/Low')
show_table = input.bool(defval = true, title = 'Show table?')
show_retracement = input.bool(defval = false, title = 'Show Retracement Ratio?', group = 'Retracement')
reverse_retracement = input.bool(defval = false, title = 'Reverse Retracement?', group = 'Retracement')

hi_source = close
lo_source = close
if src == 'Close'
    hi_source := close
    lo_source := close
if src == 'High/Low'
    hi_source := low
    lo_source := high

//Var//
Distance_from_PH = 0.
Distance_to_PH = 0.
PH_reached = false

Distance_from_PL = 0.
Distance_to_PL = 0.
PL_reached = false

//Pivots//
highest_1 = ta.highest(high, length)
lowest_1 = ta.lowest(low, length)

if bar_index >= 1
    Distance_from_PH := math.round(100 * (1 - (lo_source / highest_1)),2)
    Distance_from_PL := math.round(100 * ((hi_source/lowest_1)-1),2)

if Distance_from_PH == 0
    PH_reached := true
    PH_reached
plotshape(PH_reached, title='Top renew', color=color.new(#94ee6d, 10), style=shape.circle, location = location.top)

if Distance_from_PL == 0
    PL_reached := true
    PL_reached
plotshape(PL_reached, title='Bottom renew', color=color.new(#f18080, 10), style=shape.circle,  location = location.top)

max_up = ta.highest(Distance_from_PL, length)

//Retracement//

Retracement = 1-math.round((close-lowest_1)/(highest_1-lowest_1),2)

if reverse_retracement
    Retracement := math.round((close-lowest_1)/(highest_1-lowest_1),2)

//Plots//
plot(Distance_from_PH, 'Distance from Bottom (%)', color=color.new(color.maroon, 0), linewidth=1)
plot(Distance_from_PL, 'Distance from Top (%)', color=color.new(color.green, 0), linewidth=1)
plot(show_retracement ? Retracement*100 : na, 'REt', color=color.new(color.white, 0), linewidth=1)

plot(show_maxmin ? max_up : na, title = 'Maximum High from Bottom', color=color.new(#4f884b, 0), style=plot.style_line, trackprice = true)

hline(show_retracement ? 75 : na, '0.75', color=color.new(color.white, 60), linewidth=1)
hline(show_retracement ? 50 : na, '0.5', color=color.new(color.white, 60), linewidth=1)
hline(show_retracement ? 25 : na, '0.25', color=color.new(color.white, 60), linewidth=1)


h_0 = hline(0, '0% line', color=color.new(color.white, 80))

if (barstate.islast)
    line.new(bar_index[length], 0, bar_index[length], 0 * 1.01, extend = extend.both, color=color.new(color.white, 30), style = line.style_dashed)


if show_table
    var Table = table.new(position = position.top_right, columns = 4, rows = 4, frame_width = 1, bgcolor = color.rgb(0,0,0, 50), border_width = 1, border_color = color.gray, frame_color = color.gray)

    table.cell(table_id = Table, column = 0, row = 1,  text = 'From Bottom :', text_color = color.white)
    table.cell(table_id = Table, column = 0, row = 2,  text = 'From Top:', text_color = color.white)
    if show_retracement
        table.cell(table_id = Table, column = 0, row = 3,  text = 'Retrace Ratio:', text_color = color.white)

    table.cell(table_id = Table, column = 1, row = 1,  text =  str.tostring(Distance_from_PL)+'%', text_color =color.new(color.green, 0))
    table.cell(table_id = Table, column = 1, row = 2,  text = str.tostring(Distance_from_PH)+'%', text_color =color.new(color.maroon, 0))
    if show_retracement
        table.cell(table_id = Table, column = 1, row = 3,  text = str.tostring(Retracement), text_color =color.new(color.white, 10))

