Jurik-Filtered Kase Permission Stochastic [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("Jurik-Filtered Kase Permission Stochastic [Loxx]",
     shorttitle="JFKPS [Loxx]",
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

import loxx/loxxjuriktools/1 as jf

greencolor = #2DD204
redcolor = #D2042D

pstLength = input.int(9, "Period", group = "Basic Settings")
pstX = input.int(5, "Synthetic Multiplier", group = "Basic Settings")
pstSmooth = input.int(3, "Stochasitc Smoothing Period", group = "Basic Settings")
smoothPeriod = input.int(10, "Jurik Smoothing Period", group = "Basic Settings")
jphs = input.float(0., "Jurik Phase", group = "Basic Settings")

colorbars = input.bool(true, "Color bars?", group = "UI Options")
showSigs = input.bool(true, "Show signals?", group = "UI Options")

lookBackPeriod = pstLength * pstX
alpha = 2.0 / (1.0 + pstSmooth)

TripleK = 0., TripleDF = 0., TripleDS = 0., TripleDSs = 0., TripleDFs = 0.

fmin = ta.lowest(low, lookBackPeriod)  
fmax = ta.highest(high, lookBackPeriod) - fmin

if (fmax > 0)
    TripleK := 100.0 * (close - fmin) / fmax
else 
    TripleK := 0.0

TripleDF := nz(TripleDF[pstX]) + alpha * (TripleK - nz(TripleDF[pstX]))
TripleDS := (nz(TripleDS[pstX]) * 2.0 + TripleDF) / 3.0

TripleDSs := ta.sma(TripleDS, 3)
pssBuffer = jf.jurik_filt(TripleDSs, smoothPeriod, jphs) 

TripleDFs := ta.sma(TripleDF, 3)
pstBuffer = jf.jurik_filt(TripleDFs, smoothPeriod, jphs)

trend = 0
trend := nz(trend[1])

if (pstBuffer > pssBuffer) 
    trend := 1
if (pstBuffer < pssBuffer) 
    trend := -1

colorout = trend == 1 ? greencolor : trend == -1 ? redcolor : color.gray

plot(pssBuffer, color = color.white, linewidth = 1)
plot(pstBuffer, color = colorout, linewidth = 3)

barcolor(colorbars ? colorout : na)

goLong = ta.crossover(pstBuffer, pssBuffer)  
goShort =  ta.crossunder(pstBuffer, pssBuffer)  

plotshape(showSigs and goLong, title = "Long", color = color.yellow, textcolor = color.yellow, text = "L", style = shape.triangleup, location = location.bottom, size = size.auto)
plotshape(showSigs and goShort, title = "Short", color = color.fuchsia, textcolor = color.fuchsia, text = "S", style = shape.triangledown, location = location.top, size = size.auto)

alertcondition(goLong, title = "Long", message = "Jurik-Filtered Kase Permission Stochastic [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title = "Short", message = "Jurik-Filtered Kase Permission Stochastic [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

