//@version=4
study(title="CakmaUstad RSI Strategy by Hassonya", shorttitle="CakmaUstad-RSI", format=format.price, precision=2, max_bars_back = 4000)
src = input(close, "Source", type = input.source)
len1 = input(22, minval=1, title="RSI Length")
len2 = input(66, minval=1, title="RSI-EMA Length")
prd = input(defval = 10, title="Pivot Point Period", minval = 5, maxval = 50)
PPnum = input(defval = 10, title="Number of Pivot Point to check", minval = 2, maxval = 10)
linestylei= input(defval = 'Solid', title = "Line Style", options =['Solid', 'Dashed'])
linewidth = input(defval = 1, title = "Line Width", minval = 1, maxval = 4)
line1_color = input(title = "TopLine-Color", defval = "red", type = input.string, options = ['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'orange', 'purple', 'red', 'silver', 'teal', 'white', 'yellow'])
line2_color = input(title = "BottomLine-Color", defval = "blue", type = input.string, options = ['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'orange', 'purple', 'red', 'silver', 'teal', 'white', 'yellow'])
showbroken = input(defval = true, title="Show Broken Trend Lines")
extendlines = input(defval = false, title="Extend Lines")
showpivot = input(defval = false, title="Show Pivot Points")
up = rma(max(change(src), 0), len1)
down = rma(-min(change(src), 0), len1)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiema = ema(rsi, len2)
band1 = hline(70, "Upper Band", color=#000000)
band0 = hline(30, "Lower Band", color=#000000)//
fill(band1, band0, color=#C0C0C0, transp=75, title="Background")
Prsi = plot(rsi, "RSI-22", color=#8E1599)
Prsiema = plot(rsiema, "RSI-66", color=#ff2cff)
fill(Prsi, Prsiema, color=rsi > rsiema ? color.green : color.red, transp=85, editable=true)
float ph = na, float pl = na
ph := pivothigh(rsi, prd, prd)
pl := pivotlow(rsi, prd, prd)
plotshape(ph and showpivot ? ph : na, text="H", style=shape.labeldown, color=na, textcolor=color.blue, location=location.absolute, transp=0, offset = -prd)
plotshape(pl and showpivot ? pl - 5 : na, text="L", style=shape.labeldown, color=na, textcolor=color.blue, location=location.absolute, transp=0, offset = -prd)
getloc(bar_i)=>
_ret = bar_index + prd - bar_i
//get last 10 pivot highs - (hope to have Array in Pine version 5)
t1pos = valuewhen(ph, bar_index, 0)
t1val = nz(rsi[getloc(t1pos)])
t2pos = valuewhen(ph, bar_index, 1)
t2val = nz(rsi[getloc(t2pos)])
t3pos = valuewhen(ph, bar_index, 2)
t3val = nz(rsi[getloc(t3pos)])
t4pos = valuewhen(ph, bar_index, 3)
t4val = nz(rsi[getloc(t4pos)])
t5pos = valuewhen(ph, bar_index, 4)
t5val = nz(rsi[getloc(t5pos)])
t6pos = valuewhen(ph, bar_index, 5)
t6val = nz(rsi[getloc(t6pos)])
t7pos = valuewhen(ph, bar_index, 6)
t7val = nz(rsi[getloc(t7pos)])
t8pos = valuewhen(ph, bar_index, 7)
t8val = nz(rsi[getloc(t8pos)])
t9pos = valuewhen(ph, bar_index, 8)
t9val = nz(rsi[getloc(t9pos)])
t10pos = valuewhen(ph, bar_index, 9)
t10val = nz(rsi[getloc(t10pos)])
//get last 10 pivot lows /
b1pos = valuewhen(pl, bar_index, 0)
b1val = nz(rsi[getloc(b1pos)])
b2pos = valuewhen(pl, bar_index, 1)
b2val = nz(rsi[getloc(b2pos)])
b3pos = valuewhen(pl, bar_index, 2)
b3val = nz(rsi[getloc(b3pos)])
b4pos = valuewhen(pl, bar_index, 3)
b4val = nz(rsi[getloc(b4pos)])
b5pos = valuewhen(pl, bar_index, 4)
b5val = nz(rsi[getloc(b5pos)])
b6pos = valuewhen(pl, bar_index, 5)
b6val = nz(rsi[getloc(b6pos)])
b7pos = valuewhen(pl, bar_index, 6)
b7val = nz(rsi[getloc(b7pos)])
b8pos = valuewhen(pl, bar_index, 7)
b8val = nz(rsi[getloc(b8pos)])
b9pos = valuewhen(pl, bar_index, 8)
b9val = nz(rsi[getloc(b9pos)])
b10pos = valuewhen(pl, bar_index, 9)
b10val = nz(rsi[getloc(b10pos)]) //
getloval(l1, l2)=>
_ret1 = l1 == 1 ? b1val : l1 == 2 ? b2val : l1 ==3 ? b3val : l1 == 4 ? b4val : l1 == 5 ? b5val : l1 == 6 ? b6val : l1 == 7 ? b7val : l1 == 8 ? b8val : l1 == 9 ? b9val : l1 == 10 ? b10val : 0
_ret2 = l2 == 1 ? b1val : l2 == 2 ? b2val : l2 ==3 ? b3val : l2 == 4 ? b4val : l2 == 5 ? b5val : l2 == 6 ? b6val : l2 == 7 ? b7val : l2 == 8 ? b8val : l2 == 9 ? b9val : l2 == 10 ? b10val : 0
[_ret1, _ret2]
getlopos(l1, l2)=>
_ret1 = l1 == 1 ? b1pos : l1 == 2 ? b2pos : l1 ==3 ? b3pos : l1 == 4 ? b4pos : l1 == 5 ? b5pos : l1 == 6 ? b6pos : l1 == 7 ? b7pos : l1 == 8 ? b8pos : l1 == 9 ? b9pos : l1 == 10 ? b10pos : 0
_ret2 = l2 == 1 ? b1pos : l2 == 2 ? b2pos : l2 ==3 ? b3pos : l2 == 4 ? b4pos : l2 == 5 ? b5pos : l2 == 6 ? b6pos : l2 == 7 ? b7pos : l2 == 8 ? b8pos : l2 == 9 ? b9pos : l2 == 10 ? b10pos : 0
[_ret1, _ret2]
gethival(l1, l2)=>
_ret1 = l1 == 1 ? t1val : l1 == 2 ? t2val : l1 ==3 ? t3val : l1 == 4 ? t4val : l1 == 5 ? t5val : l1 == 6 ? t6val : l1 == 7 ? t7val : l1 == 8 ? t8val : l1 == 9 ? t9val : l1 == 10 ? t10val : 0
_ret2 = l2 == 1 ? t1val : l2 == 2 ? t2val : l2 ==3 ? t3val : l2 == 4 ? t4val : l2 == 5 ? t5val : l2 == 6 ? t6val : l2 == 7 ? t7val : l2 == 8 ? t8val : l2 == 9 ? t9val : l2 == 10 ? t10val : 0
[_ret1, _ret2]
gethipos(l1, l2)=>
_ret1 = l1 == 1 ? t1pos : l1 == 2 ? t2pos : l1 ==3 ? t3pos : l1 == 4 ? t4pos : l1 == 5 ? t5pos : l1 == 6 ? t6pos : l1 == 7 ? t7pos : l1 == 8 ? t8pos : l1 == 9 ? t9pos : l1 == 10 ? t10pos : 0
_ret2 = l2 == 1 ? t1pos : l2 == 2 ? t2pos : l2 ==3 ? t3pos : l2 == 4 ? t4pos : l2 == 5 ? t5pos : l2 == 6 ? t6pos : l2 == 7 ? t7pos : l2 == 8 ? t8pos : l2 == 9 ? t9pos : l2 == 10 ? t10pos : 0
[_ret1, _ret2]
// line definitions
var line l1 = na, var line l2 = na, var line l3 = na, var line l4 = na, var line l5 = na, var line l6 = na, var line l7 = na, var line l8 = na, var line l9 = na, var line l10 = na
var line l11 = na, var line l12 = na, var line l13 = na, var line l14 = na, var line l15 = na, var line l16 = na, var line l17 = na, var line l18 = na, var line l19 = na, var line l20 = na
var line t1 = na, var line t2 = na, var line t3 = na, var line t4 = na, var line t5 = na, var line t6 = na, var line t7 = na, var line t8 = na, var line t9 = na, var line t10 = na
var line t11 = na, var line t12 = na, var line t13 = na, var line t14 = na, var line t15 = na, var line t16 = na, var line t17 = na, var line t18 = na, var line t19 = na, var line t20 = na
//delete old lines
line.delete(l1), line.delete(l2), line.delete(l3), line.delete(l4), line.delete(l5), line.delete(l6), line.delete(l7), line.delete(l8), line.delete(l9), line.delete(l10)
line.delete(l11), line.delete(l12), line.delete(l13), line.delete(l14), line.delete(l15), line.delete(l16), line.delete(l17), line.delete(l18), line.delete(l19), line.delete(l20)
line.delete(t1), line.delete(t2), line.delete(t3), line.delete(t4), line.delete(t5), line.delete(t6), line.delete(t7), line.delete(t8), line.delete(t9), line.delete(t10)
line.delete(t11), line.delete(t12), line.delete(t13), line.delete(t14), line.delete(t15), line.delete(t16), line.delete(t17), line.delete(t18), line.delete(t19), line.delete(t20)
// there is limitation for number of lines on chart (<=50), and we use 20 uptrend lines + 20 downtrend lines (total 40)
maxline = 20
//Line coloring
u_line_color = line1_color == 'aqua' ? color.aqua : line1_color == 'black' ? color.black : line1_color == 'blue' ? color.blue : line1_color == 'fuchsia' ? color.fuchsia : line1_color == 'gray' ? color.gray : line1_color == 'green' ? color.green : line1_color == 'lime' ? color.lime : line1_color == '.maroon' ? color.maroon : line1_color == 'navy' ? color.navy : line1_color == 'olive' ? color.olive : line1_color == 'orange' ? color.orange : line1_color == 'purple' ? color.purple : line1_color == 'red' ? color.red : line1_color == 'silver' ? color.silver : line1_color == 'teal' ? color.teal : line1_color == 'white' ? color.white : color.yellow
d_line_color = line2_color == 'aqua' ? color.aqua : line2_color == 'black' ? color.black : line2_color == 'blue' ? color.blue : line2_color == 'fuchsia' ? color.fuchsia : line2_color == 'gray' ? color.gray : line2_color == 'green' ? color.green : line2_color == 'lime' ? color.lime : line2_color == '.maroon' ? color.maroon : line2_color == 'navy' ? color.navy : line2_color == 'olive' ? color.olive : line2_color == 'orange' ? color.orange : line2_color == 'purple' ? color.purple : line2_color == 'red' ? color.red : line2_color == 'silver' ? color.silver : line2_color == 'teal' ? color.teal : line2_color == 'white' ? color.white : color.yellow
ulcolor = u_line_color
dlcolor = d_line_color
//Line style
linestyle = linestylei == 'Solid' ? line.style_solid : line.style_dashed
brokenstyle = line.style_dotted
// extend lines
extln = extendlines ? extend.right : extend.none
// loop for pivot points to check if there is possible trend line
countlinelo = 0
countlinehi = 0
for p1 = 1 to PPnum - 1
uv1 = 0.0
uv2 = 0.0
up1 = 0
up2 = 0
if countlinelo <= maxline
for p2 = PPnum to p1 + 1
[val1, val2] = getloval(p1, p2)
[pos1, pos2] = getlopos(p1, p2)
if val1 > val2
diff = (val1 - val2) / (pos1 - pos2)
hline = val2 + diff
lloc = bar_index
lval = rsi
valid = true
brokentrend = false
for x = pos2 + 1 - prd to bar_index
if nz(rsi[getloc(x + prd)]) < hline
valid := false
if x + prd >= pos1
brokentrend := true
break
lloc := x
lval := hline
hline := hline + diff
if valid
uv1 := hline
uv2 := val2
up1 := lloc
up2 := pos2
break
if showbroken and not valid and countlinelo < maxline and brokentrend
countlinelo := countlinelo + 1
l1 := countlinelo == 1 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l1
l2 := countlinelo == 2 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l2
l3 := countlinelo == 3 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l3
l4 := countlinelo == 4 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l4
l5 := countlinelo == 5 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l5
l6 := countlinelo == 6 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l6
l7 := countlinelo == 7 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l7
l8 := countlinelo == 8 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l8
l9 := countlinelo == 9 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l9
l10 := countlinelo == 10 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l10
l11 := countlinelo == 11 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l11
l12 := countlinelo == 12 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l12
l13 := countlinelo == 13 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l13
l14 := countlinelo == 14 ? line.new(pos2 - prd, val2, lloc, lval, style = brokenstyle, color = dlcolor, extend = extln) : l14
l15 := countlinelo == 15 ? line.new(pos2 -