Klinger Oscillator Advanced
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © schmidtkemanfred1956

// The Klinger Oscillator is not fully implemented in Tradeview. While the description at 
// https://de.tradingview.com/chart/?solution=43000589157 is complete, the implementation is limited to the 
// pure current volume movement. This results in no difference compared to the On Balance Volume indicator.
//However, Klinger's goal was to incorporate the trend as volume force in its strength and duration into the 
//calculation. The expression ((V x [2 x ((dm/cm) - 1)] x T x 100)) for volume force only makes sense as an 
//absolute value, which should probably be expressed as 
//((V x abs(2 x ((dm/cm) - 1)) x T x 100)). Additionally, there is a need to handle the theoretical possibility 
//of cm == 0.
//Since, in general, significantly more trading volume occurs at the closing price than during the day, an 
//additional parameter for weighting the closing price is implemented. In intraday charts, considering the 
//closing price, in my opinion, does not make sense.

//@version=5
indicator( title = "Klinger Oscillator Advanced", shorttitle = "KOA", overlay = false)

fastper = input.int( title = "Fast Period"
  , tooltip = "Fast period for volumeforce"
  , inline = "PERIOD"
  , defval = 34
  , minval = 1 
  ) 
slowper = input.int( title = "Slow"
  , tooltip = "Slow period for volumeforce"
  , inline = "PERIOD"
  , defval = 55
  , minval = 2
  )
signalper = input.int( title = "Signal Period"
  , tooltip = "Period for signal line"
  , defval = 13
  , minval = 1
  )
closeweight = input.int( title = "Close weight"
  , tooltip = "Weighting of closing price. 0 = hl2, 1 = hlc3, 2 = hlcc4, >2 feel free."
  , defval = 1
  , minval = 0
  , maxval = 10
  )


//Function for Klinger Oscillator
KlingerOsc (simple int fastper = 34, simple int slowper = 55, simple int signalper = 13, simple int closeweight = 1) =>
    series int   trend = na
    series float cm = na
    series float dm = na
    series float ko = na
    series float signal = na
    series float price = na
    if bar_index > slowper - 1
        dm := high - low
        price := switch closeweight 
            0 => hl2
            1 => hlc3
            2 => hlcc4
            //hlccx
            => (high+low+close*closeweight) / (2+closeweight)
        trend := price > price[1] ? 1 : -1
        cm := trend > 0 and trend[1] > 0 ? cm[1] + dm : dm + dm[1]
        vf = volume * trend * 100 * cm > 0 ? math.abs (2 * dm / cm - 1) : 0
        kofast = ta.ema (vf, fastper)
        koslow = ta.ema (vf, slowper)
        ko := kofast - koslow
        //Signalline
        signal := ta.ema (ko, signalper)
    [ko, signal]

[ko, signal] = KlingerOsc (fastper, slowper, signalper,closeweight)
plot(ko, color = #2962FF, title="Klinger Oscillator")
plot(signal, color = #43A047, title="Signal")
plot(0, color=color.gray)