RSI is in Normal Distribution?
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dicargo_Beam

//@version=5
indicator("RSI is in Normal Distribution?")

len = input.int(14)
_rsi = ta.rsi(close,len)
modify = input.bool(false, "Use 'Dicargo_Beam's RSI modified Function", tooltip="used in 'RSI Candle Advanced V2'")


// this function is used in 'RSI-Candle-Advanced-V2'
// https://www.tradingview.com/script/kvP8BO4N-RSI-Candle-Advanced-V2/
rsi_modified_function(rsi, len) =>
    f = -math.pow(math.abs(math.abs(rsi - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
    modified = if rsi > 50
        f + 50
    else
        -f + 50
    modified




rsi = modify? rsi_modified_function(_rsi,len) : _rsi

var rsi_range = array.new_int(50,0)

get_samples()=>
    for i = 0 to 49
        if rsi >= i*2 and rsi < (i+1)*2
            array.set(rsi_range, i, array.get(rsi_range,i) + 1)
    sample_num = array.sum(rsi_range) //sum of number of samples

    var rsi_stdev = 0.0
    variance_sum = 0.0
    mean = 50
    for i = 0 to 49
        variance_sum += math.pow(i*2+1 - mean,2) * array.get(rsi_range,i)
    variance = variance_sum/sample_num
    rsi_stdev := math.pow(variance,0.5)

    var _30_to_70_percent = 0.0
    _30_to_70_nums = 0.0
    for i = 14 to 34
        _30_to_70_nums += array.get(rsi_range,i)
    _30_to_70_percent := _30_to_70_nums/sample_num*100
    [sample_num, rsi_stdev, _30_to_70_percent, mean]

[sample_num, rsi_stdev, _30_to_70_percent, mean] = get_samples()

var lines = array.new_line()
var labels = array.new_label()
var box rangebox = na

if barstate.islast
    rangebox := box.new(bar_index+5, array.max(rsi_range)/sample_num*100, bar_index+54, 0, bgcolor=color.new(color.green,90),
      text = "Samples = "+str.tostring(sample_num)+"\nRSI length = " + str.tostring(len)+"\nstdev = "+str.tostring(rsi_stdev,"#.##")+"\n30 to 70 covers "+ str.tostring(_30_to_70_percent,"#.0")+" %",
      text_size = size.normal, text_color = color.rgb(170, 154, 5),text_halign = text.align_right, text_valign = text.align_top)
    for i = 0 to 49 
        array.push(lines, line.new(bar_index+5 + i, 0, bar_index+5 + i, array.get(rsi_range, i)/sample_num*100 ) )
        if i == 14 or i == 24 or i == 34
            array.push(labels, label.new(bar_index+5 + i, 0, text = " "+str.tostring(i*2+2)+ "\nZ="+str.tostring((i*2+2-mean)/rsi_stdev,"#.##"),
              style=label.style_label_up, color=color.new(color.black,99), textcolor=color.rgb(150, 180, 13)))
        if array.size(lines) > 49
            line_del = array.shift(lines)
            line.delete(line_del)
        if array.size(labels) > 3
            label_del = array.shift(labels)
            label.delete(label_del)
box.delete(rangebox[1])


plot(rsi/10, color=color.aqua)
plot(5, color=color.gray)
plot(3, color=color.gray)
plot(7, color=color.gray)
plot(0, color=color.gray)
plot(10, color=color.gray)



