// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © julzen2

//@version=5
indicator("CyAn 1 FT", overlay=false)

// === Input Parameters ===
lenth = input.int(5, title="Length")
maxbars = input.int(2000, title="Max Bars (not used in Pine)")

// === Recursive Buffers ===
var float aux_prev = 0.0
var float fish_prev = 0.0

// === Stochastic %K Calculation ===
k = ta.stoch(close, high, low, lenth)

// === Safe recursive update ===
var float aux = na
var float fish = na

if bar_index > lenth
    aux := 0.5 * ((k / 100.0 - 0.5) * 2) + 0.5 * aux_prev
    fish := 0.25 * math.log((1 + aux) / (1 - aux)) + 0.5 * fish_prev
    aux_prev := aux
    fish_prev := fish

// === Signal Line (1 bar lag) ===
signal = nz(fish[1])

// === Plotting ===
plot(aux, title="Aux", color=color.yellow, linewidth=2)
//plot(signal, title="Signal", color=color.red, linewidth=1)
hline(0.8, "Upper Level", color=color.yellow, linestyle=hline.style_dotted)
hline(-0.8, "Lower Level", color=color.yellow, linestyle=hline.style_dotted)

