OnChart_RSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@Shuttle_Trader
//@version=5

indicator(title="OnChart_RSI", overlay=true, shorttitle="OnChart_RSI")
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="==== RSI SETTINGS ====")
rsiSourceInput = input.source(close, "Source", group="==== RSI SETTINGS ====")
up_level = input.int(75, title='UP', minval=51, maxval=99, inline='s3', group='==== RANGE FOR RSI ====')
dn_level = input.int(25, title='DN', minval=1, maxval=49, inline='s3', group='==== RANGE FOR RSI ====')
show_trend = input.bool(true, title = 'Show TREND')
use_take = input.bool(false, title='', inline='t1', group='==== TAKE CONTROL ====')
take = input.float(1.0,minval=0,title='Take level control   ', inline='t1', group='==== TAKE CONTROL ====')

maValue = ta.rma(ohlc4, rsiLengthInput)
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
up_lev_line = ta.rma(ta.highest(rsiLengthInput)[1], rsiLengthInput)
dn_lev_line = ta.rma(ta.lowest(rsiLengthInput)[1], rsiLengthInput)
rsi_range = (up_lev_line-dn_lev_line)/(up_level-dn_level)*100
rsi_line_chart = maValue+(rsi-50)/100*rsi_range
plot(rsi_line_chart, "RSI", color=#e4c200, linewidth=2)
rsiUpperBand = plot(up_lev_line, "RSI Upper Band", color=#ee0e3f)
rsiLowerBand = plot(dn_lev_line, "RSI Lower Band", color=#08f108)

buy = ta.crossover(rsi,dn_level)
sell = ta.crossunder(rsi,up_level)

l=0, l:=nz(l[1])
s=0, s:=nz(s[1])

var prev_buy = float(na)
var prev_sell = float(na)

if buy
    l+=1, s:=0
if sell
    l:=0, s+=1

plotshape(buy, title='UP', style=shape.triangleup, location=location.belowbar, color=#08f108, text='', textcolor=#08f108, size=size.tiny)
plotshape(sell, title='DN', style=shape.triangledown, location=location.abovebar, color=#ee0e3f, text='', textcolor=#ee0e3f, size=size.tiny)

ltake = float(na)
stake = float(na)

long_price = float(na), long_price := nz(long_price[1])
short_price = float(na), short_price := nz(short_price[1])

if buy and l==1
    long_price := close
    prev_buy := close

if buy and l>1
    long_price := (long_price*(l-1)+close)/l
    prev_buy := close

if sell and s==1
    short_price := close
    prev_sell := close

if sell and s>1
    short_price := (short_price*(s-1)+close)/s
    prev_sell := close

ltake := math.round_to_mintick(long_price*(1+take/100))
stake := math.round_to_mintick(short_price*(1-take/100))

plot(use_take and l>0 ? ltake : na, title='Long tp-line', style=plot.style_steplinebr)
plot(use_take and s>0 ? stake : na, title='Short tp-line', style=plot.style_steplinebr)

ltake_profit = high>=ltake and l>0 and use_take and not buy
stake_profit = low<=stake and s>0 and use_take and not sell

ltake_profit_add = l>0 and (high>=up_lev_line or high>=up_lev_line[1]) and ((high>=long_price and use_take) or not use_take) and not buy
stake_profit_add = s>0 and (low<=dn_lev_line or low<=dn_lev_line[1]) and ((low<=short_price and use_take) or not use_take) and not sell

plotshape(ltake_profit or ltake_profit_add, style=shape.square, location=location.abovebar, color=#005cf1, size=size.tiny, editable=false)
plotshape(stake_profit or stake_profit_add, style=shape.square, location=location.belowbar, color=#005cf1, size=size.tiny, editable=false)

if ltake_profit or ltake_profit_add
    l:=0
if stake_profit or stake_profit_add
    s:=0

UP = ta.crossover(close, up_lev_line)
DN = ta.crossunder(close, dn_lev_line)

rsi_channel_trend = 0
rsi_channel_trend := UP ? 1 : DN ? -1 : nz(rsi_channel_trend[1])

top_color1 = rsi>=up_level or (rsi>=up_level)[1] ? color.rgb(255, 82, 82, 40) : na
bottom_color1 = color.rgb(255, 82, 82, 100)

top_color2 = color.rgb(76, 175, 79, 100)
bottom_color2 = rsi<=dn_level or (rsi<=dn_level)[1] ? color.rgb(76, 175, 79, 40) : na

pp1_line = math.max(ohlc4,rsi_line_chart,rsi_line_chart[1])
pp2_line = math.min(ohlc4,rsi_line_chart,rsi_line_chart[1])
price = plot(ohlc4, display=display.none, editable=false)
pp1 = plot(pp1_line, display=display.none, editable=false)
pp2 = plot(pp2_line, display=display.none, editable=false)

fill(pp1, rsiUpperBand, pp1_line, up_lev_line, top_color1, bottom_color1)
fill(rsiLowerBand, pp2, dn_lev_line, pp2_line, top_color2, bottom_color2)
fill(rsiUpperBand,rsiLowerBand,color=rsi_channel_trend>0?color.rgb(76, 175, 79, 80):rsi_channel_trend<0?color.rgb(255, 82, 82, 80):na,title='TREND')

if buy
    alert('🟢 '+ syminfo.ticker + '  >> Good buying chance!\n\n  RSI = ' + str.tostring(rsi),alert.freq_once_per_bar_close)
if sell
    alert('🔴 '+ syminfo.ticker + '  >> Good selling chance!\n\n  RSI = ' + str.tostring(rsi),alert.freq_once_per_bar_close)

if UP
    alert('🟢 '+ syminfo.ticker + '  >> UP-TREND',alert.freq_once_per_bar_close)
if DN
    alert('🔴 '+ syminfo.ticker + '  >> DOWN-TREND',alert.freq_once_per_bar_close)

if ltake_profit or ltake_profit_add
    alert('🟢 '+ syminfo.ticker + '  >> Good point to take profit!\n\n  RSI = ' + str.tostring(rsi),alert.freq_once_per_bar)
if stake_profit or stake_profit_add
    alert('🔴 '+ syminfo.ticker + '  >> Good point to take profit!\n\n  RSI = ' + str.tostring(rsi),alert.freq_once_per_bar)
