Laguerre Stochastic | Adulari
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// ©Adulari
// @version=5

indicator('Laguerre Stochastic | Adulari', timeframe='',precision=2)

// Inputs
alpha = input.float(title='Alpha', minval=0, maxval=1, step=0.1, defval=0.2,group='General Settings')
kSmoothing = input.int(1,minval=1,title='%K Smoothing',group='General Settings')
dSmoothing = input.int(3,minval=2,title='%D Smoothing',group='General Settings')
maType = input.string('SMA',options=['SMA','EMA','RMA','WMA','VWMA','HMA'],title='MA Type',group='General Settings')
smoothingType = input.string('SMA',options=['SMA','EMA','RMA','WMA','VWMA','HMA'],title='Smoothing Type',group='General Settings')
signals = input.bool(false,title='Signals',tooltip='Show signals when value crosses below oversold line or crosses above overbought line.',group='General Settings')

// Functions
ma(float source, simple int length, string maType) =>
    switch maType
        'SMA' => ta.sma(source, length)
        'EMA' => ta.ema(source, length)
        'RMA' => ta.rma(source, length)
        'WMA' => ta.wma(source, length)
        'VWMA' => ta.vwma(source, length)
        'HMA' => ta.hma(source, length)

// Calculations
k = ma(ta.stoch(close, high, low, 1),kSmoothing,maType)
gamma = 1 - alpha
L0 = 0.0
L0 := (1 - gamma) * k + gamma * nz(L0[1])
L1 = 0.0
L1 := -gamma * L0 + nz(L0[1]) + gamma * nz(L1[1])
L2 = 0.0
L2 := -gamma * L1 + nz(L1[1]) + gamma * nz(L2[1])
L3 = 0.0
L3 := -gamma * L2 + nz(L2[1]) + gamma * nz(L3[1])
cu = (L0 > L1 ? L0 - L1 : 0) + (L1 > L2 ? L1 - L2 : 0) + (L2 > L3 ? L2 - L3 : 0)
cd = (L0 < L1 ? L1 - L0 : 0) + (L1 < L2 ? L2 - L1 : 0) + (L2 < L3 ? L3 - L2 : 0)
temperature = cu + cd == 0 ? -1 : cu + cd
k := (temperature == -1 ? 0 : cu / temperature)*100
d = ma(k, dSmoothing, smoothingType)

// Colors
beColor = #675F76
buColor = #a472ff

// Plots
kPlot = plot(k,color=buColor,title='%K',linewidth=1)
dPlot = plot(d,color=beColor,title='%D',linewidth=1)
fill(kPlot,dPlot,color=k>d ? color.new(buColor,95) : color.new(beColor,95),title='Trend Fill')
hline(80,title='Upper Line',color=beColor)
hline(50,title='Middle Line',color=color.new(color.gray,50))
hline(20,title='Lower Line',color=buColor)
plotshape(signals ? ta.crossunder(k,20) ? 20 : na : na,'Bullish Signal',style=shape.circle,location=location.absolute,size=size.tiny,color=buColor)
plotshape(signals ? ta.crossover(k,80) ? 80 : na : na,'Bearish Signal',style=shape.xcross,location=location.absolute,size=size.tiny,color=beColor)