[_ParkF]RSI+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ParkF

//@version=5
indicator('[_ParkF]RSI+', overlay=false, max_bars_back=1500)

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// RSI
// rsi configuration
group1 = 'RSI'
rsrc = input.source(close, 'RSI Source', group=group1)
len = input.int(14, ' RSI Length', minval=1, group=group1)
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
sreb = hline(plot_bands ? 90 : na, 'Super Extreme Bought', color.new(#787b86, 0), linewidth=1, linestyle=hline.style_dashed)
reb = hline(plot_bands ? 70 : na, 'Extreme Bought', color.new(#787b86, 0), linewidth=1, linestyle=hline.style_dashed)
rmb = hline(plot_bands ? 50 : na, 'Middle Line', color.new(#c19f4c, 30), linewidth=3, linestyle=hline.style_solid)
res = hline(plot_bands ? 30 : na, 'Extreme Sold', color.new(#787b86, 0), linewidth=1, linestyle=hline.style_dashed)
sres = hline(plot_bands ? 10 : na, 'Super Extreme Sold', color.new(#787b86, 0), linewidth=1, linestyle=hline.style_dashed)

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// RSI DIVERGENCE
// input
group2 = 'RSI DIV SETTING'
divsw = input(true, 'Divergence On / Off Switch', group=group2)
lb = input(15, 'Pivot Leftbars', group=group2)
rb = input(2, 'Pivot Rightbars', group=group2)
bear_src = input(close, 'Bearish Divs_Pivot Source', group=group2)
bull_src = input(close, 'Bullish Divs_Pivot Source', group=group2)
lvl = input.int(5, 'Lookback Level_Divs', options=[1, 2, 3, 4, 5], group=group2)

// pivot
bar = bar_index[rb]
ph = ta.pivothigh(bear_src, lb, rb)
pl = ta.pivotlow(bull_src, lb, rb)

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

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

dh0 = ta.valuewhen(ph, rsia[rb], 0), dh1 = ta.valuewhen(ph, rsia[rb], 1)
dh2 = ta.valuewhen(ph, rsia[rb], 2), dh3 = ta.valuewhen(ph, rsia[rb], 3)
dh4 = ta.valuewhen(ph, rsia[rb], 4), dh5 = ta.valuewhen(ph, rsia[rb], 5)

dl0 = ta.valuewhen(pl, rsia[rb], 0), dl1 = ta.valuewhen(pl, rsia[rb], 1)
dl2 = ta.valuewhen(pl, rsia[rb], 2), dl3 = ta.valuewhen(pl, rsia[rb], 3)
dl4 = ta.valuewhen(pl, rsia[rb], 4), dl5 = ta.valuewhen(pl, rsia[rb], 5)

bear_div1 = hi0 > hi1
     and dh1 > dh0
bear_div2 = hi0 > hi1 and hi0 > hi2
     and dh2 > dh0 and dh2 > dh1 and lvl >= 2
bear_div3 = hi0 > hi1 and hi0 > hi2 and hi0 > hi3
     and dh3>dh0 and dh3>dh1 and dh3>dh2 and lvl >= 3
bear_div4 = hi0 > hi1 and hi0 > hi2 and hi0 > hi3 and hi0 > hi4
     and dh4 > dh0 and dh4 > dh1 and dh4 > dh2 and dh4 > dh3 and lvl >= 4
bear_div5 = hi0>hi1 and hi0>hi2 and hi0>hi3 and hi0 > hi4 and hi0 > hi5
     and dh5 > dh0 and dh5 > dh1 and dh5 > dh2 and dh5 > dh3 and dh5 > dh4 and lvl >= 5
     
bull_div1 = lo0 < lo1
     and dl1 < dl0
bull_div2 = lo0<lo1 and lo0<lo2
     and dl2 < dl0 and dl2 < dl1 and lvl >= 2
bull_div3 = lo0 < lo1 and lo0 < lo2 and lo0 < lo3
     and dl3 < dl0 and dl3 < dl1 and dl3 < dl2 and lvl >= 3
bull_div4 = lo0 < lo1 and lo0 < lo2 and lo0 < lo3 and lo0 < lo4
     and dl4 < dl0 and dl4 < dl1 and dl4 < dl2 and dl4 < dl3 and lvl >= 4
bull_div5 = lo0 < lo1 and lo0 < lo2 and lo0 < lo3 and lo0 < lo4 and lo0 < lo5
      and dl5 < dl0 and dl5 < dl1 and dl5 < dl2 and dl5 < dl3 and dl5 < dl4 and lvl >= 5

_bear1 = bear_div1 and not bear_div1[1], _bear2 = bear_div2 and not bear_div2[1]
_bear3 = bear_div3 and not bear_div3[1], _bear4 = bear_div4 and not bear_div4[1]
_bear5 = bear_div5 and not bear_div5[1]

_bull1 = bull_div1 and not bull_div1[1], _bull2 = bull_div2 and not bull_div2[1]
_bull3 = bull_div3 and not bull_div3[1], _bull4 = bull_div4 and not bull_div4[1]
_bull5 = bull_div5 and not bull_div5[1]

// plotshape
plotshape(title='bear_div1', series=_bear1 and divsw ? 90 : na, style=shape.triangledown, color=#b22833, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div2', series=_bear2 and divsw ? 90 : na, style=shape.triangledown, color=#b22833, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div3', series=_bear3 and divsw ? 90 : na, style=shape.triangledown, color=#b22833, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div4', series=_bear4 and divsw ? 90 : na, style=shape.triangledown, color=#b22833, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bear_div5', series=_bear5 and divsw ? 90 : na, style=shape.triangledown, color=#b22833, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div1', series=_bull1 and divsw ? 10 : na, style=shape.triangleup, color=#388e3c, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div2', series=_bull2 and divsw ? 10 : na, style=shape.triangleup, color=#388e3c, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div3', series=_bull3 and divsw ? 10 : na, style=shape.triangleup, color=#388e3c, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div4', series=_bull4 and divsw ? 10 : na, style=shape.triangleup, color=#388e3c, location=location.absolute, size=size.tiny, offset=-2)
plotshape(title='bull_div5', series=_bull5 and divsw ? 10 : na, style=shape.triangleup, color=#388e3c, location=location.absolute, size=size.tiny, offset=-2)

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// linear regression
// input
lrg = 'Linear Regression'
linreg1 = input(true, 'Longterm Linear Regression On / Off', group=lrg)
linreg2 = input(true, 'Shorterm Linear Regression On / Off', group=lrg)
periodTrend = input.int(100, 'Longterm Linear Regression Period', minval=4, group=lrg)
periodTrend2 = input.int(25, 'Shorterm Linear Regression 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 := linreg1 ? line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, width=2) : na
    line.delete(Line[1])
drawLine2(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) =>
    var line Line = na
    Line := linreg2 ? 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(rsia[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(rsia[i]) - (intercept - bar_index[i] * slope), 2.0)
        deviation
    deviation := Deviations * math.sqrt(deviation / devDenominator)
    correlate = ta.correlation(rsia, 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
endPointBar2 = bar_index - periodTrend2 + 1

// 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)
drawLine2(endPointBar2, endPointY + deviation, bar_index, linReg + deviation, extendType, color.blue, line.style_solid)
drawLine2(endPointBar2, endPointY, bar_index, linReg, extendType, color.blue, line.style_dotted)
drawLine2(endPointBar2, endPointY - deviation, bar_index, linReg - deviation, extendType, color.blue, line.style_solid)

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// TRENDLINES
// input
group3 = 'TREND LINES'
o = rsia[1]
h = rsih
l = rsil
c = rsia
log_chart = input(true, title='Use Log Chart', group=group3)
a_Color_Type = input.string(defval='Monochrome', title='Use Trendlines Color Scheme', options=['Colored', 'Monochrome'], group=group3)

group4 = 'PRIMARY TRENDLINES'
a_Show_Primary = input(false, title='Primary Trendlines On / Off', group=group4)
a_len = input(20, title='Primary Lookback Length', group=group4)
a_Extensions = input.string(title='Primary Trendlines Extensions', defval='25', options=["Infinate", "25", "50", "75", "100", "150", "200", "300", "400", "500", "750", "1000"], group=group4)
a_width = input.int(2, title='Primary Line Width', minval=0, maxval=10, group=group4)
a_LineStyle_Type = input.string(title='Primary Line Type', defval='Solid', options=['Dashed', 'Dotted', 'Solid'], group=group4)
a_Line_Type = a_LineStyle_Type == 'Dashed' ? line.style_dashed : a_LineStyle_Type == 'Dotted' ? line.style_dotted : line.style_solid
a_Rising_Upper_Falling_Lower = false

group5 = 'SECONDARY TRENDLINES'
b_Show_Secondary = input(false, title='Secondary Trendlines On / Off', group=group5)
b_len = input(10, title='Secondary Lookback Length', group=group5)
b_Extensions = input.string(title='Secondary Trendlines Extensions', defval='25', options=["Infinate", "25", "50", "75", "100", "150", "200", "300", "400", "500", "750", "1000"], group=group5)
b_LineStyle_Type = input.string(title='Secondary Line Type', defval='Dashed', options=['Dashed', 'Dotted', 'Solid'], group=group5)
b_Line_Type = b_LineStyle_Type == 'Dashed' ? line.style_dashed : b_LineStyle_Type == 'Dotted' ? line.style_dotted : line.style_solid
b_width = input.int(1, title='Secondary Line Width', minval=0, maxval=10, group=group5)
b_Rising_Upper_Falling_Lower = false

group6 = 'TRENDLINES COLOR'
a_Line_Color = input.color(#ffffff, title="Primary Trendline Color", group=group6)
b_Line_Color = input.color(#ffffff, title="Secondary Trendline Color", group=group6)

a_bar_time = time - time[1]
b_bar_time = time - time[1]

//primary trendline
// trendline extension
a_Extension_Multiplier=
 a_Extensions=="25"? 1 :
 a_Extensions=="50"? 2 :
 a_Extensions=="75"? 3 :
 a_Extensions=="100"? 4 :
 a_Extensions=="150"? 6 :
 a_Extensions=="200"? 8 :
 a_Extensions=="300"? 12 :
 a_Extensions=="400"? 16 :
 a_Extensions=="500"? 20 :
 a_Extensions=="750"? 30 :
 a_Extensions=="1000"? 40 :
 a_Extensions=="Infinate"? 0 : na
 
// trendline function
a_f_trendline(a__input_function, a__delay, a__only_up, a__extend) =>
    var int a_Ax = 1
    var int a_Bx = 1
    var float a_By = 0
    var float a_slope = 0
    a_Ay = fixnan(a__input_function)
    if ta.change(a_Ay) != 0
        a_Ax := time[a__delay]
        a_By := a_Ay[1]
        a_Bx := a_Ax[1]
        a_slope := log_chart ? (math.log(a_Ay) - math.log(a_By)) / (a_Ax - a_Bx) : (a_Ay - a_By) / (a_Ax - a_Bx)
        a_slope
    else
        a_Ax := a_Ax[1]
        a_Bx := a_Bx[1]
        a_By := a_By[1]
        a_By
    // draw trendline
    var line a_trendline = na
    var int a_Axbis = 0
    var float a_Aybis = 0
    var bool a__xtend = true
    a_extension_time = a_Extension_Multiplier * a_bar_time * 25
    a_Axbis := a_Ax + a_extension_time
    a_Aybis := log_chart ? a_Ay * math.exp(a_extension_time * a_slope) : a_Ay + a_extension_time * a_slope
    if a_Extension_Multiplier != 0
        a__xtend := false
        a__xtend
    if ta.change(a_Ay) != 0
        a_line_color_Rising_Falling = a_slope * time < 0 ? a__only_up ? a_Rising_Upper_Falling_Lower ? a_Color_Type == 'Colored' ? color.gray : color.teal : na : a_Color_Type == 'Colored' ? #cf0a83 : color.teal : a__only_up ? a_Color_Type == 'Colored' ? #027521 : color.teal : a_Rising_Upper_Falling_Lower ? a_Color_Type == 'Colored' ? color.gray : color.teal : na
        a_line_color_Not_Rising_Falling = a_slope * time < 0 ? a__only_up ? na : a_Color_Type == 'Colored' ? #cf0a83 : color.teal : a__only_up ? a_Color_Type == 'Colored' ? #027521 : color.teal : na
        a_line_color = a_Show_Primary and not a_Rising_Upper_Falling_Lower ? a_line_color_Not_Rising_Falling : a_Show_Primary and a_Rising_Upper_Falling_Lower ? a_line_color_Rising_Falling : na
        if not na(a_line_color)
            a_trendline := line.new(a_Bx, a_By, a_Axbis, a_Aybis, xloc.bar_time, extend=a__xtend ? extend.right : extend.none, color=a_Line_Color, style=a_Line_Type, width=a_width)
            a_trendline
    [a_Bx, a_By, a_Axbis, a_Aybis, a_slope]

// calc pivot points 
a_high_point = ta.pivothigh(c > o ? h : l, a_len, a_len / 2)
a_low_point = ta.pivotlow(c > o ? l : h, a_len, a_len / 2)

// call trendline function
[a_phx1, a_phy1, a_phx2, a_phy2, a_slope_high] = a_f_trendline(a_high_point, a_len / 2, false, true)
[a_plx1, a_ply1, a_plx2, a_ply2, a_slope_low] = a_f_trendline(a_low_point, a_len / 2, true, true)

// secondary trendline
// trendline extension
b_Extension_Multiplier=
 b_Extensions=="25"? 1 :
 b_Extensions=="50"? 2 :
 b_Extensions=="75"? 3 :
 b_Extensions=="100"? 4 :
 b_Extensions=="150"? 6 :
 b_Extensions=="200"? 8 :
 b_Extensions=="300"? 12 :
 b_Extensions=="400"? 16 :
 b_Extensions=="500"? 20 :
 b_Extensions=="750"? 30 :
 b_Extensions=="1000"? 40 :
 b_Extensions=="Infinate"? 0 : na
 
// trendline function
b_f_trendline(b__input_function, b__delay, b__only_up, b__extend) =>
    var int b_Ax = 1
    var int b_Bx = 1
    var float b_By = 0
    var float b_slope = 0
    b_Ay = fixnan(b__input_function)
    if ta.change(b_Ay) != 0
        b_Ax := time[b__delay]
        b_By := b_Ay[1]
        b_Bx := b_Ax[1]
        b_slope := log_chart ? (math.log(b_Ay) - math.log(b_By)) / (b_Ax - b_Bx) : (b_Ay - b_By) / (b_Ax - b_Bx)
        b_slope
    else
        b_Ax := b_Ax[1]
        b_Bx := b_Bx[1]
        b_By := b_By[1]
        b_By
    // draw trendlines
    var line b_trendline = na
    var int b_Axbis = 0
    var float b_Aybis = 0
    var bool b__xtend = true
    b_extension_time = b_Extension_Multiplier * b_bar_time * 25
    b_Axbis := b_Ax + b_extension_time
    b_Aybis := log_chart ? b_Ay * math.exp(b_extension_time * b_slope) : b_Ay + b_extension_time * b_slope
    if b_Extension_Multiplier != 0
        b__xtend := false
        b__xtend
    if ta.change(b_Ay) != 0
        b_line_color_Rising_Falling = b_slope * time < 0 ? b__only_up ? b_Rising_Upper_Falling_Lower ? a_Color_Type == 'Colored' ? color.gray : color.teal : na : a_Color_Type == 'Colored' ? color.red : color.teal : b__only_up ? a_Color_Type == 'Colored' ? color.green : color.teal : b_Rising_Upper_Falling_Lower ? a_Color_Type == 'Colored' ? color.gray : color.teal : na
        b_line_color_Not_Rising_Falling = b_slope * time < 0 ? b__only_up ? na : a_Color_Type == 'Colored' ? color.red : color.teal : b__only_up ? a_Color_Type == 'Colored' ? color.green : color.teal : na
        b_line_color = b_Show_Secondary and not b_Rising_Upper_Falling_Lower ? b_line_color_Not_Rising_Falling : b_Show_Secondary and b_Rising_Upper_Falling_Lower ? b_line_color_Rising_Falling : na
        if not na(b_line_color)
            b_trendline := line.new(b_Bx, b_By, b_Axbis, b_Aybis, xloc.bar_time, extend=b__xtend ? extend.right : extend.none, color=b_Line_Color, style=b_Line_Type, width=b_width)
            b_trendline
    [b_Bx, b_By, b_Axbis, b_Aybis, b_slope]

// calc pivot points
b_high_point = ta.pivothigh(c > o ? h : l, b_len, b_len / 2)
b_low_point = ta.pivotlow(c > o ? l : h, b_len, b_len / 2)

// call trendline function
[b_phx1, b_phy1, b_phx2, b_phy2, b_slope_high] = b_f_trendline(b_high_point, b_len / 2, false, true)
[b_plx1, b_ply1, b_plx2, b_ply2, b_slope_low] = b_f_trendline(b_low_point, b_len / 2, true, true)

// plot
b_color_high = b_slope_high * time < 0 ? color.green : na
b_color_low = b_slope_low * time > 0 ? color.red : na

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// ICHIMOKU
// input
group7 = 'Ichimoku'
ichi_sw = input(true, 'Ichimoku On / Off Switch', group=group7)
conversionPeriods = input.int(13, minval=1, title='Conversion Line Periods', group=group7)
basePeriods = input.int(33, minval=1, title='Base Line Periods', group=group7)
laggingSpan2Periods = input.int(84, minval=1, title='Lagging Span 2 Periods', group=group7)
displacement = input.int(33, minval=1, title='Displacement', group=group7)

// 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(ichi_sw ? leadLine1 : na, offset = displacement- 1, color=color.new(#1b5e20, 50), title="LeadLine A", linewidth=2)
p2 = plot(ichi_sw ? leadLine2 : na, offset = displacement - 1, color=color.new(#801922, 50), title="LeadLine B", linewidth=2)
fill(p1, p2, color = leadLine1 > leadLine2 ? color.new(#1b5e20, 80) : color.new(#801922, 80), title='Ichimoku Cloud')

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// MACD
// input
group8 = 'MACD'
Histo_Switch_MACD = input(false, 'MACD Histogram On / Off Switch', group=group8)
Fast_Len = input(title="Fast Length", defval=12, group=group8)
Slow_Len = input(title="Slow Length", defval=26, group=group8)
MACD_Source = input.source(close, 'MACD Source', group=group8)
MACD_Signal_Len = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9, group=group8)
SMA_Source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"], group=group8)
SMA_Signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group=group8)

// calc
Fast_MA = SMA_Source == "SMA" ? ta.sma(MACD_Source, Fast_Len) : ta.ema(MACD_Source, Fast_Len)
Slow_MA = SMA_Source == "SMA" ? ta.sma(MACD_Source, Slow_Len) : ta.ema(MACD_Source, Slow_Len)
MACD = Fast_MA - Slow_MA
MACD_Signal = SMA_Signal == "SMA" ? ta.sma(MACD, MACD_Signal_Len) : ta.ema(MACD, MACD_Signal_Len)
Histogram_MACD = MACD - MACD_Signal

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// TSI
// input
group9 = 'TSI'
Histo_Switch_TSI = input(true, 'TSI Histogram On / Off Switch', group=group9)
Short_Len = input(title="Short Period", defval=5, group=group9)
Long_Len = input(title="Long Period", defval=20, group=group9)
TSI_Signal_Len = input(title="Signal Line Period)", defval=5, group=group9)

// calc
TSI = ta.tsi(close, Short_Len, Long_Len)
TSI_Signal = ta.ema(TSI, TSI_Signal_Len)
Histogram_TSI = TSI - TSI_Signal

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// HISTOGRAM
group10= 'HISTOGRAM COLOR'
Color_Grow_Above = input(#2b4b46, "Above   Grow", inline="Above", group=group10)
Color_Fall_Above = input(#8ca18d, "Fall", inline="Above", group=group10)
Color_Grow_Below = input(#b49c79, "Below Grow", inline="Below", group=group10)
Color_Fall_Below = input(#8b3d44, "Fall", inline="Below", group=group10)

// plotshape histogram
plotshape(Histogram_MACD and Histo_Switch_MACD ? 5 : na, title="MACD Histogram", style=shape.square, color=(Histogram_MACD>=0 ? (Histogram_MACD[1] < Histogram_MACD ? Color_Grow_Above : Color_Fall_Above) : (Histogram_MACD[1] < Histogram_MACD ? Color_Grow_Below : Color_Fall_Below)), location=location.absolute, size=size.tiny)
plotshape(Histogram_TSI and Histo_Switch_TSI ? 5 : na, title="TSI Histogram", style=shape.square, color=(Histogram_TSI>=0 ? (Histogram_TSI[1] < Histogram_TSI ? Color_Grow_Above : Color_Fall_Above) : (Histogram_TSI[1] < Histogram_TSI ? Color_Grow_Below : Color_Fall_Below)), location=location.absolute, size=size.tiny)


//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// MULTI TIME FRAME RSI
// input
group11 = 'MTF_RSI'
MTF_RSI = input(true, 'MTF_RSI On / Off Switch', group=group11)
TF_Len_1 = input.int(14, 'Lookback 1H ', group=group11)
TF_Len_2 = input.int(14, 'Lookback 4H', group=group11)
TF_Len_3 = input.int(14, 'Lookback 1D', group=group11)

// tf function
TF_1 = input.timeframe(defval='60', title='1H', inline=group11, group=group11)
TF_2 = input.timeframe(defval='240', title='4H', inline=group11, group=group11)
TF_3 = input.timeframe(defval='D', title='1D', inline=group11, group=group11)

S_1 = MTF_RSI ? request.security(syminfo.tickerid, TF_1, ta.rsi(rsrc, TF_Len_1)) : na
S_2 = MTF_RSI ? request.security(syminfo.tickerid, TF_2, ta.rsi(rsrc, TF_Len_2)) : na
S_3 = MTF_RSI ? request.security(syminfo.tickerid, TF_3, ta.rsi(rsrc, TF_Len_3)) : na

// plot
plot(series=S_1, title='TF_1H', color=color.new(#ffeb3b, 30), linewidth=4)
plot(series=S_2, title='TF_4H', color=color.new(#ff9800, 40), linewidth=4)
plot(series=S_3, title='TF_1D', color=color.new(#b22833, 50), linewidth=4)

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// PLOT CANDLE(with wick), PLOT LINE
color1 = color.new(color.white, 0)
color2 = color.new(color.black, 0)
wickcolor = color.new(color.black, 0)
bordercolor = color.new(color.black, 0)
plotcandle(rsia[1], rsih, rsil, rsia, 'RSI_Candle', color=ta.change(rsia) > 0 ? color1 : color2, wickcolor=wickcolor, bordercolor=bordercolor)
plot(rsia, 'RSI_Line', color= ta.change(rsia) > 0 ? color.black : color.black, display=display.none, linewidth=2)

//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //
//  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //  [_ParkF]  //

// alert and label
alertcondition(_bear1 or _bear2 or _bear3 or _bear4 or _bear5, title='Bear', message='Bear')
alertcondition(_bull1 or _bull2 or _bull3 or _bull4 or _bull5, title='Bull', message='Bull')