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

//@version=5
indicator(title = 'smoothed_rsi', shorttitle = 'rsi', overlay = false)


// input
length = input.int(defval = 14, title = 'Length', group = 'Value', tooltip = 'Determines length of RSI lookback period')
left = input.int(defval = 30, title = 'Pivot Left', group = 'Value', tooltip = 'Determines length of left pivot')
right = input.int(defval = 20, title = 'Pivot Right', group = 'Value', tooltip = 'Determines length of right pivot')
src = input.source(defval = ohlc4, title = 'Source', group = 'Options', tooltip = 'Determines input source')
trendline = input.bool(defval = true, title = 'Trendline', group = 'Options', tooltip = 'Determines whether to display rsi trendline')



// variable
a1 = 0.0
b1 = 0.0
c1 = 0.0 
c2 = 0.0 
c3 = 0.0
smoothed_rsi = 0.0
pivot_high = 0.0
pivot_low = 0.0


// relative strength index (rsi)
rsi = ta.rsi(src, length)

// smoothed with super smoother
pi = 2 * math.asin(1)
a1 := math.exp( -1.414*pi/10) 
b1 := 2 * a1 * math.cos( 1.414*2*pi/10) 
c2 := b1
c3 := -a1 * a1
c1 := 1 - c2 - c3
smoothed_rsi := c1*(rsi+rsi[1])/2+c2*nz(smoothed_rsi[1])+c3*nz(smoothed_rsi[2])


// Visualize
plot(smoothed_rsi, color = color.blue, linewidth = 2)
hline(70, color = color.new(color.blue, 50))
hline(30, color = color.new(color.blue, 50))

// rsi trendline
pivot_high := ta.pivothigh(smoothed_rsi, left, right)
pivot_low := ta.pivotlow(smoothed_rsi, left, right)

for int i = 0 to 1
    // array to store x, y value, and line
    var x1 = array.new_int(length)
    var x2 = array.new_int(length)
    var y1 = array.new_float(length)
    var y2 = array.new_float(length)
    var xl1 = array.new_int(length)
    var xl2 = array.new_int(length)
    var yl1 = array.new_float(length)
    var yl2 = array.new_float(length)
    var line_ = array.new_line(length)

    // downtrend
    array.set(x1, i, ta.valuewhen(pivot_high, bar_index, 1)-right)
    array.set(x2, i, ta.valuewhen(pivot_high, bar_index, 0)-left)
    array.set(y1, i, ta.valuewhen(pivot_high, pivot_high, 1))
    array.set(y2, i, ta.valuewhen(pivot_high, pivot_high, 0))

    // uptrend
    array.set(xl1, i, ta.valuewhen(pivot_low, bar_index, 1)-right)
    array.set(xl2, i, ta.valuewhen(pivot_low, bar_index, 0)-left)
    array.set(yl1, i, ta.valuewhen(pivot_low, pivot_low, 1))
    array.set(yl2, i, ta.valuewhen(pivot_low, pivot_low, 0))

    // trendline
    if nz(array.get(y2, i)) < nz(array.get(y1, i))
        array.push(line_, line.new(nz(array.get(x1, i)), nz(array.get(y1, i)), nz(array.get(x2, i)), nz(array.get(y2, i)), color = trendline ? color.red : na))
    if nz(array.get(yl2, i)) > nz(array.get(yl1, i))
        array.push(line_, line.new( nz(array.get(xl1, i)), nz(array.get(yl1, i)), nz(array.get(xl2, i)), nz(array.get(yl2, i)), color = trendline ? color.green : na))
