TheATR: Fisher Oscillator.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thealgorithmictrader

//@version=5
indicator("TheATR: Fisher Oscillator.")

//User has to agree to have the indi loading on the chart.
//{
var user_consensus = input.string(defval="", title="By typing 'agree' you acknowledge the understanding about the fact 'TheATR: Fisher Oscillator.' doesn't aim and/or intend to provide any financial advice, as of its nature of being an educational tool.\n © TheATR", confirm = true, group="consensus")
var valid = false
if user_consensus == "agree"
    valid := true
else
    runtime.error("Type 'agree' first in order to show the algo!")
    valid := false


//Fisher Oscillator Settings
//{
g_fish = "Fisher Indicator"

i_addf_fish_sign = input.bool(true, "",group=g_fish,inline="fish0")
i_addf_fish_signt = input.string("Classic Lines Crosses", "| Signals : Type",["0-Line Crosses", "Classic Lines Crosses"],group=g_fish,inline="fish0")
i_addf_fish_src = input.source(hl2, "Src",group=g_fish,inline="addf3")
i_addf_fish_l = input.int(13, "Len",group=g_fish,inline="addf3",minval=2)
i_addf_fish_alerts = input.bool(true, "Alerts",group=g_fish,inline="fishal")

g_fish_sty = "Fisher Indicator Style"

i_bull_col = input.color(color.blue, "Colors: Bull", group=g_fish_sty, inline = "sty0")
i_bear_col = input.color(color.purple, "Bear", group=g_fish_sty, inline = "sty0")

//}


//Fisher Oscillator Calculations
//{

fish_h = ta.highest(i_addf_fish_src, i_addf_fish_l)
fish_l = ta.lowest(i_addf_fish_src, i_addf_fish_l)

fish_rounding(input) =>
    input > 0.99 ? 0.999 : input < -0.99 ? -0.999 : input

value = 0.0

var fisher_v = 0.0
fisher_v:=fish_rounding(0.66 * ((hl2 - fish_l) / math.max(fish_h - fish_l, 0.001) - 0.5) + 0.67 * nz(fisher_v[1]))

var fisher_vf = 0.0
fisher_vf:= 0.5 * math.log((1 + fisher_v) / math.max(1 - fisher_v, 0.001)) + 0.5 * nz(fisher_vf[1])
fisher_vfp = fisher_vf[1]

//}

//Fisher Oscillator Plots & Signals
//{

//FO Plots
hline(0.0, "Fisher Oscillator", color.gray)
plot(valid?fisher_vf:na, "Fisher V1", fisher_vf>fisher_vfp?i_bull_col:i_bear_col,2,style=plot.style_stepline)
plot(valid?fisher_vfp:na, "Fisher V1", color.gray, 2)

//FO Signals
fish_bull = i_addf_fish_signt== "Classic Lines Crosses"?ta.crossover(fisher_vf,fisher_vfp):ta.crossover(fisher_vf,0.0)
fish_bear = i_addf_fish_signt== "Classic Lines Crosses"?ta.crossunder(fisher_vf,fisher_vfp):ta.crossunder(fisher_vf,0.0)

var long_sign = array.new_label()
var short_sign = array.new_label()

//FO Signals Plots
plotshape(i_addf_fish_sign and fish_bull and valid, "Fisher Long Signals", shape.circle,location.bottom,i_bull_col,text="Long",textcolor=i_bull_col)
plotshape(i_addf_fish_sign and fish_bear and valid, "Fisher Short Signals", shape.circle,location.top,i_bear_col,text="Short",textcolor=i_bear_col)

//FO Alets
if i_addf_fish_sign and fish_bull and valid
    alert("Fisher Long Alert")
if i_addf_fish_sign and fish_bear and valid
    alert("Fisher Short Alert")
//}

