Regime Filter [CHE]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chervolino

//@version=5
indicator("Regime Filter [CHE]", "Regime Filter [CHE]", timeframe="", overlay = true)


//------------------------------------------------------------------------------
//					Settings
//-----------------------------------------------------------------------------{
length     = input(50)
sig_length = input(9,'Signal Length')

//-----------------------------------------------------------------------------}
//				Exponential Envelopes
//-----------------------------------------------------------------------------{
var a1 = 2/(length+1)

var u1_1 = 0.,var u1_2 = 0.
var d1_1 = 0.,var d1_2 = 0.

u1_1 := nz(math.max(close, open, u1_1[1] - (u1_1[1] - close) * a1), close)
u1_2 := nz(math.max(close * close, open * open, u1_2[1] - (u1_2[1] - close * close) * a1), close * close)

d1_1 := nz(math.min(close, open, d1_1[1] + (close - d1_1[1]) * a1), close)
d1_2 := nz(math.min(close * close, open * open, d1_2[1] + (close * close - d1_2[1]) * a1), close * close)

//Components
bl = math.sqrt(d1_2 - d1_1 * d1_1)
br = math.sqrt(u1_2 - u1_1 * u1_1)

signal = ta.ema(math.max(bl, br), sig_length)
osc_color = (100 * (bl - br) / br) > 0 ?color.yellow :color.blue

plotshape(true , 'line', shape.circle  , location.bottom, osc_color, show_last=233, size=size.tiny)