TURK RSI+ICHIMOKU
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © turk_shariq

//@version=5
indicator('TURK RSI+ICHIMOKU', overlay=false, max_bars_back=1500)

// rsi divergence
// input
rsig = 'RSI'
rb = input(2, 'How many Right Bars for Pivots', group=rsig)
lb = input(15, 'How many Left Bars for Pivots', group=rsig)
sph = input(close, 'Pivot source for Bear Divs', group=rsig)
spl = input(close, 'Pivots Source for Bull Divs', group=rsig)
len = input.int(14, ' RSI Length', minval=1, group=rsig)
lvl = input.int(5, 'Lookback Level for Divs', options=[1, 2, 3, 4, 5], group=rsig)

// pivot
ph = ta.pivothigh(sph, lb, rb)
pl = ta.pivotlow(spl, lb, rb)

hi0 = ta.valuewhen(ph, sph[rb], 0)
hi1 = ta.valuewhen(ph, sph[rb], 1)
hi2 = ta.valuewhen(ph, sph[rb], 2)
hi3 = ta.valuewhen(ph, sph[rb], 3)
hi4 = ta.valuewhen(ph, sph[rb], 4)
hi5 = ta.valuewhen(ph, sph[rb], 5)

lo0 = ta.valuewhen(pl, spl[rb], 0)
lo1 = ta.valuewhen(pl, spl[rb], 1)
lo2 = ta.valuewhen(pl, spl[rb], 2)
lo3 = ta.valuewhen(pl, spl[rb], 3)
lo4 = ta.valuewhen(pl, spl[rb], 4)
lo5 = ta.valuewhen(pl, spl[rb], 5)

lox0 = ta.valuewhen(pl, bar_index[rb], 0)
lox1 = ta.valuewhen(pl, bar_index[rb], 1)
lox2 = ta.valuewhen(pl, bar_index[rb], 2)
lox3 = ta.valuewhen(pl, bar_index[rb], 3)
lox4 = ta.valuewhen(pl, bar_index[rb], 4)
lox5 = ta.valuewhen(pl, bar_index[rb], 5)

hix0 = ta.valuewhen(ph, bar_index[rb], 0)
hix1 = ta.valuewhen(ph, bar_index[rb], 1)
hix2 = ta.valuewhen(ph, bar_index[rb], 2)
hix3 = ta.valuewhen(ph, bar_index[rb], 3)
hix4 = ta.valuewhen(ph, bar_index[rb], 4)
hix5 = ta.valuewhen(ph, bar_index[rb], 5)

rsi = ta.rsi(close, len)

rh0 = ta.valuewhen(ph, rsi[rb], 0)
rh1 = ta.valuewhen(ph, rsi[rb], 1)
rh2 = ta.valuewhen(ph, rsi[rb], 2)
rh3 = ta.valuewhen(ph, rsi[rb], 3)
rh4 = ta.valuewhen(ph, rsi[rb], 4)
rh5 = ta.valuewhen(ph, rsi[rb], 5)

rl0 = ta.valuewhen(pl, rsi[rb], 0)
rl1 = ta.valuewhen(pl, rsi[rb], 1)
rl2 = ta.valuewhen(pl, rsi[rb], 2)
rl3 = ta.valuewhen(pl, rsi[rb], 3)
rl4 = ta.valuewhen(pl, rsi[rb], 4)
rl5 = ta.valuewhen(pl, rsi[rb], 5)

// bull & bear divergence logic
bull_div_1= lo0<lo1
     and rl1<rl0
bull_div_2= lo0<lo1 and lo0<lo2
     and rl2<rl0 and rl2<rl1 and lvl>=2
bull_div_3= lo0<lo1 and lo0<lo2 and lo0<lo3
     and rl3<rl0 and rl3<rl1 and rl3<rl2 and lvl>=3
bull_div_4= lo0<lo1 and lo0<lo2 and lo0<lo3 and lo0<lo4
     and rl4<rl0 and rl4<rl1 and rl4<rl2 and rl4<rl3 and lvl>=4
bull_div_5= lo0<lo1 and lo0<lo2 and lo0<lo3 and lo0<lo4 and lo0<lo5
      and rl5<rl0 and rl5<rl1 and rl5<rl2 and rl5<rl3 and rl5<rl4 and lvl>=5

bear_div_1= hi0>hi1
     and rh1>rh0
bear_div_2= hi0>hi1 and hi0>hi2
     and rh2>rh0 and rh2>rh1 and lvl>=2
bear_div_3= hi0>hi1 and hi0>hi2 and hi0>hi3
     and rh3>rh0 and rh3>rh1 and rh3>rh2 and lvl>=3
bear_div_4= hi0>hi1 and hi0>hi2 and hi0>hi3 and hi0>hi4
     and rh4>rh0 and rh4>rh1 and rh4>rh2 and rh4>rh3 and lvl>=4
bear_div_5= hi0>hi1 and hi0>hi2 and hi0>hi3 and hi0>hi4 and hi0>hi5
     and rh5>rh0 and rh5>rh1 and rh5>rh2 and rh5>rh3 and rh5>rh4 and lvl>=5

new_bull1= bull_div_1 and not bull_div_1[1]
new_bull2= bull_div_2 and not bull_div_2[1]
new_bull3= bull_div_3 and not bull_div_3[1]
new_bull4= bull_div_4 and not bull_div_4[1]
new_bull5= bull_div_5 and not bull_div_5[1]

new_bear1= bear_div_1 and not bear_div_1[1]
new_bear2= bear_div_2 and not bear_div_2[1]
new_bear3= bear_div_3 and not bear_div_3[1]
new_bear4= bear_div_4 and not bear_div_4[1]
new_bear5= bear_div_5 and not bear_div_5[1]

recall(x) =>
    ta.barssince(not na(x))

// bull divergence line plot
rbull1 = line(na)
rbull1 := new_bull1 and not new_bull2 and not new_bull3 and not new_bull4 and not new_bull5 ? line.new(lox0, rl0, lox1, rl1, color=#ff9800, width=2) : na
rbull2 = line(na)
rbull2 := new_bull2 and not new_bull3 and not new_bull4 and not new_bull5 ? line.new(lox0, rl0, lox2, rl2, color=#ff9800, width=2) : na
rbull3 = line(na)
rbull3 := new_bull3 and not new_bull4 and not new_bull5 ? line.new(lox0, rl0, lox3, rl3, color=#ff9800, width=2) : na
rbull4 = line(na)
rbull4 := new_bull4 and not new_bull5 ? line.new(lox0, rl0, lox4, rl4, color=#ff9800, width=2) : na
rbull5 = line(na)
rbull5 := new_bull5 ? line.new(lox0, rl0, lox5, rl5, color=#ff9800, width=2) : na

xbull21 = ta.valuewhen(recall(rbull2) == 0, bar_index, 0) - ta.valuewhen(recall(rbull1) == 0, bar_index, 0)
xbull31 = ta.valuewhen(recall(rbull3) == 0, bar_index, 0) - ta.valuewhen(recall(rbull1) == 0, bar_index, 0)
xbull41 = ta.valuewhen(recall(rbull4) == 0, bar_index, 0) - ta.valuewhen(recall(rbull1) == 0, bar_index, 0)
xbull51 = ta.valuewhen(recall(rbull5) == 0, bar_index, 0) - ta.valuewhen(recall(rbull1) == 0, bar_index, 0)
xbull32 = ta.valuewhen(recall(rbull3) == 0, bar_index, 0) - ta.valuewhen(recall(rbull2) == 0, bar_index, 0)
xbull42 = ta.valuewhen(recall(rbull4) == 0, bar_index, 0) - ta.valuewhen(recall(rbull2) == 0, bar_index, 0)
xbull52 = ta.valuewhen(recall(rbull5) == 0, bar_index, 0) - ta.valuewhen(recall(rbull2) == 0, bar_index, 0)
xbull43 = ta.valuewhen(recall(rbull4) == 0, bar_index, 0) - ta.valuewhen(recall(rbull3) == 0, bar_index, 0)
xbull53 = ta.valuewhen(recall(rbull5) == 0, bar_index, 0) - ta.valuewhen(recall(rbull3) == 0, bar_index, 0)
xbull54 = ta.valuewhen(recall(rbull5) == 0, bar_index, 0) - ta.valuewhen(recall(rbull4) == 0, bar_index, 0)

if new_bull2 and lo2 == ta.valuewhen(new_bull1, lo1, 0) and xbull21 >= 0
    line.delete(rbull1[xbull21])
if new_bull3 and lo3 == ta.valuewhen(new_bull1, lo1, 0) and xbull31 >= 0
    line.delete(rbull1[xbull31])
if new_bull4 and lo4 == ta.valuewhen(new_bull1, lo1, 0) and xbull41 >= 0
    line.delete(rbull1[xbull41])
if new_bull5 and lo5 == ta.valuewhen(new_bull1, lo1, 0) and xbull51 >= 0
    line.delete(rbull1[xbull51])
if new_bull3 and lo3 == ta.valuewhen(new_bull2, lo2, 0) and xbull32 >= 0
    line.delete(rbull2[xbull32])
if new_bull4 and lo4 == ta.valuewhen(new_bull2, lo2, 0) and xbull42 >= 0
    line.delete(rbull2[xbull42])
if new_bull5 and lo5 == ta.valuewhen(new_bull2, lo2, 0) and xbull52 >= 0
    line.delete(rbull2[xbull52])
if new_bull4 and lo4 == ta.valuewhen(new_bull3, lo3, 0) and xbull43 >= 0
    line.delete(rbull3[xbull43])
if new_bull5 and lo5 == ta.valuewhen(new_bull3, lo3, 0) and xbull53 >= 0
    line.delete(rbull3[xbull53])
if new_bull5 and lo5 == ta.valuewhen(new_bull4, lo4, 0) and xbull54 >= 0
    line.delete(rbull4[xbull54])

// bear divergence line plot
rbear1 = line(na)
rbear1 := new_bear1 and not new_bear2 and not new_bear3 and not new_bear4 and not new_bear5 ? line.new(hix0, rh0, hix1, rh1, color=#ff9800, width=2) : na
rbear2 = line(na)
rbear2 := new_bear2 and not new_bear3 and not new_bear4 and not new_bear5 ? line.new(hix0, rh0, hix2, rh2, color=#ff9800, width=2) : na
rbear3 = line(na)
rbear3 := new_bear3 and not new_bear4 and not new_bear5 ? line.new(hix0, rh0, hix3, rh3, color=#ff9800, width=2) : na
rbear4 = line(na)
rbear4 := new_bear4 and not new_bear5 ? line.new(hix0, rh0, hix4, rh4, color=#ff9800, width=2) : na
rbear5 = line(na)
rbear5 := new_bear5 ? line.new(hix0, rh0, hix5, rh5, color=#ff9800, width=2) : na

xbear21 = ta.valuewhen(recall(rbear2) == 0, bar_index, 0) - ta.valuewhen(recall(rbear1) == 0, bar_index, 0)
xbear31 = ta.valuewhen(recall(rbear3) == 0, bar_index, 0) - ta.valuewhen(recall(rbear1) == 0, bar_index, 0)
xbear41 = ta.valuewhen(recall(rbear4) == 0, bar_index, 0) - ta.valuewhen(recall(rbear1) == 0, bar_index, 0)
xbear51 = ta.valuewhen(recall(rbear5) == 0, bar_index, 0) - ta.valuewhen(recall(rbear1) == 0, bar_index, 0)
xbear32 = ta.valuewhen(recall(rbear3) == 0, bar_index, 0) - ta.valuewhen(recall(rbear2) == 0, bar_index, 0)
xbear42 = ta.valuewhen(recall(rbear4) == 0, bar_index, 0) - ta.valuewhen(recall(rbear2) == 0, bar_index, 0)
xbear52 = ta.valuewhen(recall(rbear5) == 0, bar_index, 0) - ta.valuewhen(recall(rbear2) == 0, bar_index, 0)
xbear43 = ta.valuewhen(recall(rbear4) == 0, bar_index, 0) - ta.valuewhen(recall(rbear3) == 0, bar_index, 0)
xbear53 = ta.valuewhen(recall(rbear5) == 0, bar_index, 0) - ta.valuewhen(recall(rbear3) == 0, bar_index, 0)
xbear54 = ta.valuewhen(recall(rbear5) == 0, bar_index, 0) - ta.valuewhen(recall(rbear4) == 0, bar_index, 0)

if new_bear2 and hi2 == ta.valuewhen(new_bear1, hi1, 0) and xbear21 >= 0
    line.delete(rbear1[xbear21])
if new_bear3 and hi3 == ta.valuewhen(new_bear1, hi1, 0) and xbear31 >= 0
    line.delete(rbear1[xbear31])
if new_bear4 and hi4 == ta.valuewhen(new_bear1, hi1, 0) and xbear41 >= 0
    line.delete(rbear1[xbear41])
if new_bear5 and hi5 == ta.valuewhen(new_bear1, hi1, 0) and xbear51 >= 0
    line.delete(rbear1[xbear51])
if new_bear3 and hi3 == ta.valuewhen(new_bear2, hi2, 0) and xbear32 >= 0
    line.delete(rbear2[xbear32])
if new_bear4 and hi4 == ta.valuewhen(new_bear2, hi2, 0) and xbear42 >= 0
    line.delete(rbear2[xbear42])
if new_bear5 and hi5 == ta.valuewhen(new_bear2, hi2, 0) and xbear52 >= 0
    line.delete(rbear2[xbear52])
if new_bear4 and hi4 == ta.valuewhen(new_bear3, hi3, 0) and xbear43 >= 0
    line.delete(rbear3[xbear43])
if new_bear5 and hi5 == ta.valuewhen(new_bear3, hi3, 0) and xbear53 >= 0
    line.delete(rbear3[xbear53])
if new_bear5 and hi5 == ta.valuewhen(new_bear4, hi4, 0) and xbear54 >= 0
    line.delete(rbear4[xbear54])

plotshape(title='bull_div_1', series=new_bull1 ? 13 : na, style=shape.triangleup, color=#089981, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div_2', series=new_bull2 ? 13 : na, style=shape.triangleup, color=#089981, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div_3', series=new_bull3 ? 13 : na, style=shape.triangleup, color=#089981, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div_4', series=new_bull4 ? 13 : na, style=shape.triangleup, color=#089981, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div_5', series=new_bull5 ? 13 : na, style=shape.triangleup, color=#089981, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div_1', series=new_bear1 ? 87 : na, style=shape.triangledown, color=#f23645, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div_2', series=new_bear2 ? 87 : na, style=shape.triangledown, color=#f23645, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div_3', series=new_bear3 ? 87 : na, style=shape.triangledown, color=#f23645, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div_4', series=new_bear4 ? 87 : na, style=shape.triangledown, color=#f23645, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div_5', series=new_bear5 ? 87 : na, style=shape.triangledown, color=#f23645, location=location.absolute, size=size.tiny, offset=-2)


// rsi candle (with wick)
// rsi configuration
rsrc = close
ad = true

// rsi function
pine_rsi(rsrc, len) =>
    u = math.max(rsrc - rsrc[1], 0)
    d = math.max(rsrc[1] - rsrc, 0)
    rs = ta.rma(u, len) / ta.rma(d, len)
    res = 100 - 100 / (1 + rs)
    res
pine_rma(rsrc, length) =>
    b = 1 / length
    sum = 0.0
    sum := na(sum[1]) ? ta.sma(rsrc, length) : b * rsrc + (1 - b) * nz(sum[1])
u = math.max(rsrc - rsrc[1], 0)
d = math.max(rsrc[1] - rsrc, 0)
b = 1 / len
ruh = b * math.max(high - close[1], 0) + (1 - b) * ta.rma(u, len)[1]
rdh = (1 - b) * ta.rma(d, len)[1]
rul = (1 - b) * ta.rma(u, len)[1]
rdl = b * math.max(close[1] - low, 0) + (1 - b) * ta.rma(d, len)[1]
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
    rsiadvanced = if rsi > 50
        f + 50
    else
        -f + 50
    rsiadvanced
rsiha = 100 - 100 / (1 + ruh / rdh)
rsila = 100 - 100 / (1 + rul / rdl)
rsia = ta.rsi(rsrc, len)
rsih = if ad
    function(rsiha, len)
else
    rsiha
rsil = if ad
    function(rsila, len)
else
    rsila

// rsi bought & sold zone
plot_bands = true

reb = hline(plot_bands ? 70 : na, 'Extreme Bought', color.new(#b2b5be, 50), linewidth=4, linestyle=hline.style_solid)
rmb = hline(plot_bands ? 50 : na, 'Middle Line', color.new(#fbc02d, 80), linewidth=4, linestyle=hline.style_solid)
res = hline(plot_bands ? 30 : na, 'Extreme Sold', color.new(#b2b5be, 50), linewidth=4, linestyle=hline.style_solid)

// candle
plotcandle(rsi[1], rsih, rsil, rsi, 'RSI_Candle', color=ta.change(rsi) > 0 ? #ffffff : #000000, wickcolor=#000000, bordercolor=#2a2e39)
plot(rsi, 'RSI_Line', color= ta.change(rsi) > 0 ? color.black : color.black, display=display.none, linewidth=2)


// linear regression
// input
lrg = 'Linear Regression'
linreg = input(true, 'Linear Regression On / Off')
periodTrend = input.int(100, 'Trend Period', minval=4, group=lrg)
deviationsAmnt = input.float(2, 'Deviation', minval=0.1, step=0.1, group=lrg)
estimatorType = input.string('Unbiased', 'Estimator', options=['Biased', 'Unbiased'], group=lrg)
var extendType = input.string('Right', 'Extend', options=['Right', 'Segment'], group=lrg) == 'Right' ? extend.right : extend.none

// drawline configuration
drawLine(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) =>
    var line Line = na
    Line := linreg ? line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, width=2) : na
    line.delete(Line[1])

rsdcr2(PeriodMinusOne, Deviations, Estimate) =>
    var period = PeriodMinusOne + 1
    var devDenominator = Estimate == 'Unbiased' ? PeriodMinusOne : period
    Ex = 0.0
    Ex2 = 0.0
    Exy = 0.0
    Ey = 0.0
    for i = 0 to PeriodMinusOne by 1
        closeI = nz(rsi[i])
        Ex := Ex + i
        Ex2 := Ex2 + i * i
        Exy := Exy + closeI * i
        Ey := Ey + closeI
        Ey
    ExEx = Ex * Ex
    slope = Ex2 == ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
    linearRegression = (Ey - slope * Ex) / period
    intercept = linearRegression + bar_index * slope
    deviation = 0.0
    for i = 0 to PeriodMinusOne by 1
        deviation := deviation + math.pow(nz(rsi[i]) - (intercept - bar_index[i] * slope), 2.0)
        deviation
    deviation := Deviations * math.sqrt(deviation / devDenominator)
    correlate = ta.correlation(rsi, bar_index, period)
    r2 = math.pow(correlate, 2.0)
    [linearRegression, slope, deviation, correlate, r2]

periodMinusOne = periodTrend - 1
[linReg, slope, deviation, correlate, r2] = rsdcr2(periodMinusOne, deviationsAmnt, estimatorType)
endPointBar = bar_index - periodTrend + 1
endPointY = linReg + slope * periodMinusOne

// drawline plot
drawLine(endPointBar, endPointY + deviation, bar_index, linReg + deviation, extendType, #e91e63, line.style_solid)
drawLine(endPointBar, endPointY, bar_index, linReg, extendType, #e91e63, line.style_dotted)
drawLine(endPointBar, endPointY - deviation, bar_index, linReg - deviation, extendType, #e91e63, line.style_solid)


// ichimoku
// input
ichig = 'Ichimoku'
conversionPeriods = input.int(9, minval=1, title='Conversion Line Periods', group=ichig)
basePeriods = input.int(26, minval=1, title='Base Line Periods', group=ichig)
laggingSpan2Periods = input.int(52, minval=1, title='Lagging Span 2 Periods', group=ichig)
displacement = input.int(26, minval=1, title='Displacement', group=ichig)

// calc
donchian(len) => math.avg(ta.lowest(rsil, len), ta.highest(rsih, len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)

// plot
p1 = plot(leadLine1, offset = displacement- 1, color=color.new(#4caf50, 100), title="LeadLine A", linewidth=3)
p2 = plot(leadLine2, offset = displacement - 1, color=color.new(#f23645, 100), title="LeadLine B", linewidth=3)
fill(p1, p2, color = leadLine1 > leadLine2 ? color.new(#4caf50, 70) : color.new(#f23645, 70), title='Ichimoku Cloud')