Oscillator Extremes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo
//@version=5
indicator('Oscillator Extremes', shorttitle = 'OSC EXTREME', overlay= false)

//Inputs//
src = input.source(close)
LengthInput = input.int(14, minval=1, title='Oscillator Length')
type = input.string('RSI', title='Oscillator', options=['RSI', 'MOM','CCI','CMO','ROC'])

//Oscillator Selection//
Multi_OSC(type, src, LengthInput) =>
    float result = 0
    if type == "RSI"
        result := ta.rsi(src, LengthInput)
    if type == "MOM"
        result := ta.mom(src, LengthInput)
    if type == "CCI"
        result := ta.cci(src, LengthInput)
    if type == 'CMO'  
        result := ta.cmo(src,LengthInput)
    if type == 'ROC'  
        result := ta.roc(src,LengthInput)
    result

//BB//
OscMax = ta.max(Multi_OSC(type,src, LengthInput))
OscMin= ta.min(Multi_OSC(type,src, LengthInput))
BBsrc = (((Multi_OSC(type,src, LengthInput))-OscMin)/(OscMax-OscMin))*100
length = input.int(20, minval=1, title='BB Length')
mult = input.float(2.0, minval=0.001, maxval=50, title='BB Mult')
float basis = ta.sma(BBsrc, length)
float dev = mult * ta.stdev(BBsrc, length)
upper = basis + dev
lower = basis - dev

//Fplus/Fminus//
Fplus = upper - BBsrc
Fminus= BBsrc-lower
hline=(0)

//ADX//
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
dirmov(len) =>
	up = ta.change(high)
	down = -ta.change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
	minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = ta.rma(ta.tr, len)
	plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
	[plus, minus]
adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)

sigmax = ta.max(adx(dilen,adxlen))
sigmin= ta.min(adx(dilen, adxlen))
sig = ((adx(dilen, adxlen)-sigmin)/(sigmax-sigmin))*100

//Plots//
AdxSig= plot(sig, title='ADX', color= sig >= sig[1] ? color.rgb(57, 16, 65): color.white, linewidth=2)
Upper = plot(Fplus, title='Bullish', color= Fplus < Fminus ? color.rgb(16, 49, 17) : color.gray, linewidth= 3)
Lower = plot(Fminus, title= 'Bearish', color= Fminus < Fplus ? color.rgb(59, 16, 16) : color.gray, linewidth= 3)
ZeroLine= plot(hline, title='Zero Line', color=color.white)