Fisher Cycle Adaptive, Fisher Transform [loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5

indicator(title='Fisher Cycle Adaptive, Fisher Transform [loxx]', shorttitle='FCAFT [loxx]', 
     timeframe="",
     timeframe_gaps=true, 
     max_bars_back = 5000)

src = input.source(hl2, title='Source', group = "Basic Settings")
lenr = input.int(7, minval=2, step = 1, title='Length', group = "Basic Settings")
cycle_selection = input.string("Fixed", title = "Calculation Method", options = ["Fixed", "Fisher Cycle"], group = "Advanced Settings")

wave_selector = input.int(50, title = "Percent of Wave (%)", minval = 10, maxval = 100, group = "Advanced Settings")/100

fisherOb1 = input.float(2, minval=1, step = 0.1, title='Custom Overbought Level', group = "Thresholds")
fisherOs1 = input.float(-2, maxval=-1, step = 0.1,  title='Custom Oversold Level', group = "Thresholds")

showbarcolor = input.bool(true, title = "Color Bars?", group = "UI Options")

greencolor = #2DD204
redcolor = #D2042D

round_(val) =>
    val > .99 ? .999 : val < -.99 ? -.999 : val

counter_inner = 0

len_new = math.round(nz(counter_inner[1], counter_inner) * wave_selector) 
len_new_mod = len_new < 2 ? 2 : len_new

len_final = cycle_selection == "Fixed" ? lenr : len_new_mod

high_ = ta.highest(src, len_final)
low_ = ta.lowest(src, len_final)
value = 0.0
value := round_(.66 * ((src - low_) / (high_ - low_) - .5) + .67 * nz(value[1]))
fish1_pre = 0.0
fish1_pre := .5 * math.log((1 + value) / (1 - value)) + .5 * nz(fish1_pre[1])
fish2_pre = fish1_pre[1]

fish1 = nz(fish1_pre, 0)
fish2 = nz(fish2_pre, 0)

ff = 0.001
ff1 = -0.001

if (fish1 > 0.0)
    while ff > 0.00 and counter_inner < 4000
        ff := fish1[counter_inner]
    	counter_inner := counter_inner + 1

if (fish1 < 0.0)
    while ff1 < 0.00 and counter_inner < 4000
        ff1 := fish1[counter_inner]
    	counter_inner := counter_inner + 1
    	
barcolor(showbarcolor ? fish1> 0 ? greencolor : redcolor : na)

plot(fisherOb1+2.5, color = color.new(color.white, 100), title = "Upper UI Constraint")
plot(fisherOs1-2.5, color = color.new(color.white, 100), title = "Lower UI Constraint")

tl = plot(fisherOb1+1, color=color.rgb(0, 255, 255, 100), style=plot.style_line, title = "OB Level 2")
tl1 = plot(fisherOb1, color=color.rgb(0, 255, 255, 100), style=plot.style_line, title = "OB Level 1")

bassisF = plot(0, color=color.new(color.gray, 30), linewidth=1, style=plot.style_circles, title = "Zero")

bl1 = plot(fisherOs1, color=color.rgb(255, 255, 255, 100), style=plot.style_line, title = "OS Level 1")
bl = plot(fisherOs1-1, color=color.rgb(255, 255, 255, 100), style=plot.style_line, title = "OS Level 2")

fill(tl, tl1, color=color.new(color.gray, 85), title = "Top Boundary Fill Color")
fill(bl, bl1, color=color.new(color.gray, 85), title = "Bottom Boundary Fill Color")

fp1 = plot(fish1, color =fish2 > 0 and fish1 > fish2 ? greencolor : (fish2 > 0 or fish1 > fish2) and fish2 > 0 ? color.gray : fish2 < 0 and fish1 < fish2 ? redcolor : color.gray, 
     title='Fisher',
     linewidth = 3)

fill(fp1, bassisF, color = fish2 > 0 ? color.new(greencolor, 70) : color.new(redcolor, 70))




alertcondition(ta.crossover(fish1, fish2), title="Crossover Generic", message="Fisher: Crossover Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(ta.crossunder(fish1, fish2), title="Crossunder Generic", message="Fisher: Crossunder Short\nSymbol: {{ticker}}\nPrice: {{close}}")






alertcondition(ta.crossover(fish1, 0), title="Zero Crossover", message="Fisher: Zero Crossover Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(ta.crossunder(fish1, 0), title="Zero Crossunder", message="Fisher: Zero Crossunder Short\nSymbol: {{ticker}}\nPrice: {{close}}")



































































