True Trend Oscillator [wbburgin]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wbburgin

//@version=5
indicator("True Trend Oscillator [wbburgin]",overlay=false)
import jdehorty/KernelFunctions/2 as k

threshold = input.int(20,"Range Threshold",minval=0,maxval=100,
 tooltip="Stochastic threshold (0-100) to identify ranging markets. A higher threshold will identify more ranges but will be slower at catching the trend.",
  group="General")
l = input.int(14,"ATR Length",group="Stoch ATR")
lengthStoch = input.int(14, "Stochastic Length", minval=1,group="Stoch ATR")
lb = input.int(25,"Lookback",group="Stoch ATR",tooltip="The number of bars used for the estimation of the rational quadratic kernel.")
rw = input.int(1,"Relative Weighting",group="Stoch ATR")
smoothK = input.int(3, "Stochastic Smoothing", minval=1,group="Stoch ATR")

rsisrc = input.source(close,"RSI Source",group="RSI")
rsilen = input.int(14,"RSI Length",group="RSI")

stochatr(atr,smoothK,lookback,weighting,lengthStoch)=>
    y = k.rationalQuadratic(ta.stoch(atr,atr,atr,lengthStoch),lookback,weighting,smoothK)
    y

atr = ta.atr(l)
k = stochatr(atr,smoothK,lb,rw,lengthStoch)
rsi = ta.rsi(rsisrc,rsilen)
bull = math.sqrt(rsi * k)
bear = math.sqrt((100 - rsi) * k)

barcolor(bull > bear and math.min(bull,bear) > threshold ? color.lime : bear > bull and math.min(bull,bear) > threshold ? color.red : color.gray)
plot(bull, color= color.lime, title="Bull Trend")
plot(bear, color= color.red, title= "Bear Trend")

top = hline(80)
bottom = hline(threshold,linestyle=hline.style_solid,color=color.green)
base = hline(0,linestyle=hline.style_solid,color=color.red)

fill(top,bottom,color=color.new(color.blue,90))
fill(bottom,base,color=color.new(color.red,90))