QFL Drop %
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AHMEDABDELAZIZZIZO


//@version=5
indicator("QFL Drop %",overlay = true)
left = input(10)
right = input(10)
p = ta.pivotlow(low,left,right)
v = ta.valuewhen(p,low[left],0)

textcolor = input.color(color.white)

var float [] lows = array.new_float()
var float chn = na
if v < v[1]
    chn :=  (v[1] -v )/ v[1] * 100
    if array.size(lows) < 4000
        array.push(lows, chn)
    else
        array.shift(lows)
        array.push(lows, chn)
mediandrop = array.avg(lows)
maxdrop = array.max(lows)
mindrop = array.min(lows)
tabl = table.new(position = position.top_right, columns = 4, rows = 4)
table.cell(table_id = tabl , column = 1 , row = 1, text = "Avg Drop %" , width = 15 , text_color = textcolor)
table.cell(table_id = tabl , column = 2 , row = 1, text = "Min Drop %", width = 15 , text_color =textcolor)
table.cell(table_id = tabl , column = 3 , row = 1, text = "Max Drop %", width = 15 , text_color = textcolor)

table.cell(table_id = tabl , column = 1 , row = 2 , text = str.tostring(mediandrop), width = 10 , text_color = textcolor)
table.cell(table_id = tabl , column = 2 , row = 2 , text = str.tostring(mindrop), width = 10 , text_color = textcolor)
table.cell(table_id = tabl , column = 3 , row = 2 , text = str.tostring(maxdrop), width = 10 , text_color = textcolor)

t = fixnan(ta.pivotlow(low,left,right))
plot(t, color=ta.change(t) ? na : #03f590b6, linewidth=3, offset=-(right), title="Support")