RSI Profile
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AtifRizwan

//@version=5
indicator("RSI Profile")
profileType =   input.string("RSI Values","Profile Type",options=["RSI Values","RSI Pivots High","RSI Pivots Low"])
rsilen      =   input.int(14,"RSI Length")
rsisrc      =   input.source(close,"RSI Source")
leftLenH    =   input.int(14,"Pivot High ",group="Pivot Length Left/Right",inline="ph")
rightLenH   =   input.int(14,"/",group="Pivot Length Left/Right",inline="ph")
showPivotH  =   input.bool(false,"Show?",group="Pivot Length Left/Right",inline="ph")
leftLenL    =   input.int(14,"Pivot Low",group="Pivot Length Left/Right",inline="pl")
rightLenL   =   input.int(14,"/",group="Pivot Length Left/Right",inline="pl")
showPivotL  =   input.bool(false,"Show?",group="Pivot Length Left/Right",inline="pl")
lineColor   =   input.color(color.purple,"Histogram",group="Visuals",inline="line")
lineWidth   =   input.int(1,"Width",group="Visuals",inline="line")
maxLineColor=   input.color(color.red,"POC Line",group="Visuals",inline="maxline")
maxlineWidth=   input.int(1,"Width",group="Visuals",inline="maxline")
maxlineEx   =   input.bool(true,"Extend POC Line",group="Visuals",inline="maxline")
showMax     =   input.bool(true,"Show POC Values?",group="Visuals",inline="showmax")
_offset     =   input.int(5,"Histogram Offset",group="Visuals")
maxWidth    =   input.int(55,"Width of Histogram",group="Visuals")
maxRSI      =   input.int(70,"Max RSI",group="Visuals")
minRSI      =   input.int(30,"Min RSI",group="Visuals")

rsi         =   ta.rsi(rsisrc,rsilen)
ph          =   ta.pivothigh(rsi,leftLenH,rightLenH)
pl          =   ta.pivotlow(rsi,leftLenL,rightLenL)

plot(rsi,color=color.purple,linewidth=2,title="RSI")
rsilow=hline(30,title="RSI Low Band")
rsihigh=hline(70,title="RSI High Band")
fill(rsilow,rsihigh,color=color.new(color.purple,90),title = "RSI Band Background")

var rsi_array   =   array.new_int(100)
var li          =   array.new_line(0)
var line li1    =   na
var label la    =   na
float pivotH =   na
float pivotL =   na

if bar_index==1
    for i = 0 to 99
        array.set(rsi_array,i,0)
if bar_index > 1 and profileType=="RSI Values"
    array.set(rsi_array,math.floor(rsi),array.get(rsi_array,math.floor(rsi))+1)
if bar_index > 1 and profileType=="RSI Pivots High" and not na(ph)
    array.set(rsi_array,math.floor(ph),array.get(rsi_array,math.floor(ph))+1)
if bar_index > 1 and profileType=="RSI Pivots Low" and not na(pl)
    array.set(rsi_array,math.floor(pl),array.get(rsi_array,math.floor(pl))+1)
 
if showPivotH and not na(ph)
    pivotH:=ph
if showPivotL and not na(pl)
    pivotL:=pl

plotshape(pivotH,"Pivot High",offset=-rightLenH,location=location.absolute,style=shape.diamond,color=color.new(color.red,30),size=size.small)
plotshape(pivotL,"Pivot Low",offset=-rightLenL,location=location.absolute,style=shape.diamond,color=color.new(color.green,30),size=size.small)

if barstate.islast
    if array.size(li) > 0
        for i = 0 to array.size(li)-1
            line.delete(array.get(li,i))
    array.clear(li)

    label.delete(la)
    line.delete(li1)

    color c=na
    int w=1
    bool isMax=na

    max=array.max(rsi_array)

    li1:=line.new(bar_index+_offset,minRSI,bar_index+_offset,maxRSI,color=color.gray)
    for i = minRSI to maxRSI
        thisWidth=math.ceil((array.get(rsi_array,i))/max*maxWidth)
        isMax:=thisWidth==maxWidth
        w:=isMax?maxlineWidth:lineWidth
        c:=isMax?maxLineColor:lineColor
        array.push(li,line.new(bar_index+_offset,i,bar_index+_offset+thisWidth,i,width=w,color=c,extend=isMax?extend.left:extend.none))
        if isMax and showMax
            la:=label.new(bar_index+_offset+maxWidth+5,i,str.tostring(i),style=label.style_label_left,color=color.red,textcolor=color.white)