[MAD] almost RSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © djmad
//@version=5

indicator('[MAD MBS] almost RSI', shorttitle='[MAD MBS] almost RSI', format=format.price, precision=2)

// Inputs {
int n1                          = input.int(10, 'RSI Length')
string TF                       = input.timeframe('', 'Timeframe 1')
float POWFACTOR                 = input.float(1.0, "POW - Faktor",group="Wave compression", tooltip='0 < compress < 1 < expand < 10',step=0.1)
bool triggersignal_use_avg      = input(false, 'Activate Smoothing',group="final averaging")
int sma                         = input.int(20, 'sma',group="final averaging",inline='1a')
int ema                         = input.int(5, 'ema',group="final averaging",inline='1a', tooltip='disabled with "1"')
float obLevel1                  = input.float(30, 'Over Bought Level',group='Triggerlines')
float osLevel1                  = input.float(-30, 'Over Sold Level',group='Triggerlines')
string Alertmode                = input.string('ChangeDir-Outside', title='Alert mode', options=['Outside', 'CrossIn', 'CrossOut', 'ChangeDir-All','ChangeDir-Outside'],group='Triggerlines',inline='00a')
bool showalerts_L1              = input.bool(true,'Show Alerts')
//}

////////// GET AND MAP ALL DATA ////////////////
//The code calculates the relative strength index (RSI) of a security in 
//TradingView and processes the data to obtain a modified version of the RSI for 
//further analysis. The modified RSI is either the average of the RSI over a 
//specified period of time, or the RSI itself, depending on the value of the "triggersignal_use_avg" input.

// Get RSI {
f_rsi(_src, _n1) =>
    rsi = ta.rsi(_src, _n1)
    rsi

rsi_TF1 = request.security(syminfo.tickerid, TF,f_rsi(close, n1), barmerge.gaps_on, barmerge.lookahead_off)
// }

// compression and sloothing {
rsi_mtf = (rsi_TF1 -50) > 0 ? math.pow(rsi_TF1 -50,POWFACTOR) : - math.pow(math.abs(rsi_TF1 -50),POWFACTOR)
rsi_mtf_average = ta.ema(ta.sma(rsi_mtf, sma), ema)
rsi_work = triggersignal_use_avg ? rsi_mtf_average : rsi_mtf
// }

// Alarms {
var signalout = 0
bool signal_buy = na, bool signal_sell = na

if Alertmode == 'CrossIn'
    signal_buy      :=  ta.crossover(rsi_work, osLevel1)
    signal_sell     :=  ta.crossunder(rsi_work, obLevel1)
if Alertmode == 'CrossOut'
    signal_buy      :=  ta.crossunder(rsi_work, osLevel1)
    signal_sell     :=  ta.crossover(rsi_work, obLevel1)
if Alertmode == 'ChangeDir-Outside'
    signal_buy     :=  rsi_work[5] > rsi_work[4] and rsi_work[4] > rsi_work[3] and rsi_work[3] > rsi_work[2] and rsi_work[2] > rsi_work[1] and rsi_work[1] < rsi_work[0] and rsi_work < osLevel1
    signal_sell    :=  rsi_work[5] < rsi_work[4] and rsi_work[4] < rsi_work[3] and rsi_work[3] < rsi_work[2] and rsi_work[2] < rsi_work[1] and rsi_work[1] > rsi_work[0] and rsi_work > obLevel1
if Alertmode == 'ChangeDir-All'
    signal_buy      :=  rsi_work[5] > rsi_work[4] and rsi_work[4] > rsi_work[3] and rsi_work[3] > rsi_work[2] and rsi_work[2] > rsi_work[1] and rsi_work[1] < rsi_work[0]
    signal_sell     :=  rsi_work[5] < rsi_work[4] and rsi_work[4] < rsi_work[3] and rsi_work[3] < rsi_work[2] and rsi_work[2] < rsi_work[1] and rsi_work[1] > rsi_work[0]

filter_sell = (rsi_work) > obLevel1
filter_buy = (rsi_work) < osLevel1
// }

// plotting all the data {
trl = plot(rsi_work, 'Triggerline', color=color.new(color.yellow, 0))
hline(0, title='Middle Level', linestyle=hline.style_dotted, color=#606060)
osl = plot(osLevel1, 'Oversell-Line', color=color.gray)
obl = plot(obLevel1, 'Overbuy-Line', color=color.gray)

fill(osl, trl, color=(rsi_work) < osLevel1 ? color.new(color.green, 40) : na)
fill(obl, trl, color=(rsi_work) > obLevel1 ? color.new(color.red, 40) : na)

plotshape(showalerts_L1? signal_buy? rsi_work+rsi_work*0.6:    na  :na,
 style=shape.triangleup, location=location.absolute, color=color.new(#44ff44, 0), size=size.small)
plotshape(showalerts_L1? signal_sell? rsi_work+rsi_work*0.6:    na  :na,
 style=shape.triangledown, location=location.absolute, color=color.new(#ff4444, 0), size=size.small)

//}


//*** MULTIBIT Inputs ***{
string  inputtype           =   input.string("NoInput" , title="Signal Type", group='Signal transmission config', options=["MultiBit", "MultiBit_pass", "NoInput"], tooltip='Multibit Daisychain with and without infusing\nMutlibit is the Signal-Type used in my Backtestsystem',inline='3a')
float   inputModule         =   input(title='Select L1 Indicator Signal', group='Signal transmission config', defval=close, inline='3a')
int     Signal_Channel_1    =   input.int(2,   "Channel long trend",   minval=-1,   maxval=15,group='Signal transmission config',inline='1a')
int     Signal_Channel_2    =   input.int(3,   "Channel short trend",  minval=-1,   maxval=15,group='Signal transmission config',inline='1a')
int     Signal_Channel_3    =   input.int(4,   "Channel long signal",   minval=-1,   maxval=15,group='Signal transmission config',inline='2a')
int     Signal_Channel_4    =   input.int(5,   "Channel short signal",  minval=-1,   maxval=15,group='Signal transmission config',inline='2a')
// }

//*** MULTIBIT LIBARY AND INFUSE *** {
import djmad/Signal_transcoder_library/7 as transcode
bool [] Multibit = array.new<bool>(16,false)
if inputtype == "MultiBit" or inputtype == "MultiBit_pass"
    Multibit := transcode._16bit_decode(inputModule)

if inputtype != "MultiBit_pass"
    transcode.f_infuse_signal(Signal_Channel_1, filter_buy, Signal_Channel_2, filter_sell, Signal_Channel_3, signal_buy, Signal_Channel_4, signal_sell, Multibit)

float plot_output = transcode._16bit_encode(Multibit)
plot(plot_output,title='MultiBit Signal',display=display.none)
// }