Customisable Stoch RSI [10 PRESETS INCLUDED]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © crypteisfuture
//@version=5
indicator("Customisable Stoch RSI", shorttitle = 'Custom Stoch RSI [PRESETS INCLUDED]')

// StochRSI Set
// --------------------
// 5 -- 5 -- 3
// 8 -- 8 -- 5
// 13 -- 13 -- 13
// 21 -- 15 -- 13
// 21 -- 21 -- 13
// 34 -- 34 -- 13
// 55 -- 55 -- 21
// 89 -- 13 -- 34
// 89 -- 89 -- 21
// 233 -- 233 -- 34

group_preset = 'Presets'
group_custom = 'Custom'
group_plot   = 'Plot Levels'

usePreset   = input.string('Preset', title = 'Settings from', options = ['Preset', 'Custom'], group = group_preset) 
preset      = input.int(defval = 10, minval = 1, maxval = 10, group = group_preset)

mp1 = input.int(defval = 233, minval = 2, title = "RSI Period",      group = group_custom)
mp2 = input.int(defval = 233, minval = 2, title = "Stoch Period",    group = group_custom)
mp3 = input.int(defval = 34,  minval = 2, title = "Lowest Period",   group = group_custom)
mp4 = input.int(defval = 5,   minval = 2, title = "Smooth",          group = group_custom)

if usePreset == 'Preset'
    switch preset
        1 => mp1 := 5, mp2 := 5, mp3 := 3
        2 => mp1 := 8, mp2 := 8, mp3 := 5
        3 => mp1 := 13, mp2 := 13, mp3 := 13
        4 => mp1 := 21, mp2 := 15, mp3 := 13
        5 => mp1 := 21, mp2 := 21, mp3 := 13
        6 => mp1 := 34, mp2 := 34, mp3 := 13
        7 => mp1 := 55, mp2 := 55, mp3 := 21
        8 => mp1 := 89, mp2 := 13, mp3 := 34
        9 => mp1 := 89, mp2 := 89, mp3 := 21
        10 => mp1 := 233, mp2 := 233, mp3 := 34

// formula by Nicholas Kormanik
custom_stoch_rsi = ta.ema( math.sum( ( ta.rsi( close, mp1) - ta.lowest( ta.rsi(close, mp1) , mp2) ) , mp3) / math.sum( ( .0000001+ ( ta.highest( ta.rsi(close, mp1) , mp2) - (ta.lowest( ta.rsi(close, mp1) , mp2) ) ) ) , mp3) , mp4) * 100


// < Plot >

ON      = 'ON'
OFF     = 'OFF'
bg      = input.string(ON, 'Dark Background', options=[OFF, ON]) == ON
bgcolor(bg ? color.new(#000000, 20) : na, title='Dark Background')

s_upper = input.int(70, title = 'Standart Upper', group = group_plot)
s_lower = input.int(30, title = 'Standart Lower', group = group_plot)

ex_upper = input.int(95, title =  'Extra Upper', group = group_plot)
ex_lower = input.int(5, title  =  'Extra Lower', group = group_plot)

st_upper = input.int(80, title =  'Semi-Standart Upper', group = group_plot)
st_lower = input.int(20, title  = 'Semi-Standart Lower', group = group_plot)

plot(custom_stoch_rsi, title = 'Customisable Stoch',    linewidth = 2, style = plot.style_stepline, color = color.new(color.from_gradient(1337, 228, 1488, color.new(color.yellow, 0), color.new(color.yellow, 0)), 50))
plot(math.avg(custom_stoch_rsi[1], custom_stoch_rsi[2], custom_stoch_rsi[3]), title = 'Average Stoch', linewidth = 1, style = plot.style_stepline, color = color.new(color.aqua, 60))

standart_upper = plot(s_upper, title = 'Standart Upper', linewidth = 1, style = plot.style_cross, color = color.new(color.red, 30))
standart_lower = plot(s_lower, title = 'Standart Lower', linewidth = 1, style = plot.style_cross, color = color.new(color.green, 30))

extra_upper = plot(ex_upper, title = 'Extra Upper', linewidth = 2, style = plot.style_line, color = color.new(color.red, 50))
upper       = plot(st_upper, title = 'Upper', linewidth = 2, style = plot.style_line, color = color.new(color.red, 50))

extra_lower = plot(ex_lower, title = 'Extra Lower', linewidth = 2, style = plot.style_line, color = color.new(color.green, 50))
lower       = plot(st_lower, title = 'Lower', linewidth = 2, style = plot.style_line, color = color.new(color.green, 50))

fill(upper, extra_upper, color = color.new(color.from_gradient(1337, 228, 1488, color.new(color.red, 0), color.new(color.maroon, 0)), 70))
fill(lower, extra_lower, color = color.new(color.from_gradient(1337, 228, 1488, color.new(color.green, 0), color.new(color.lime, 0)), 70))

plot(math.avg(st_upper, ex_upper), linewidth = 1, color = color.new(color.white, 45), style = plot.style_cross)
plot(math.avg(st_lower, ex_lower), linewidth  = 1,  color = color.new(color.white, 45), style = plot.style_cross)