HL-D Close Fraction Oscillator | 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("HL-D Close Fraction Oscillator | Adulari",precision=2)

// Inputs
length = input.int(20,minval=2,title='Length',group='General Settings')
smoothing = input.int(20,minval=2,title='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')
lookback = input.int(1,minval=1,title='Lookback',group='Advanced Settings')
rescaleBarsBack = input.int(200,minval=20,title='Rescale Bars Back',group='Advanced Settings')

// Functions
rescale(float value, float oldMin, float oldMax, float newMin, float newMax) =>
    newMin + (newMax - newMin) * (value - oldMin) / math.max(oldMax - oldMin, 10e-10)
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
value = close>close[lookback] ? (high-low)/close : (low-high)/close
value := ma(value,length,maType)
value := rescale(value,ta.lowest(value,rescaleBarsBack),ta.highest(value,rescaleBarsBack),0,100)
signal = ma(value,smoothing,smoothingType)

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

// Plots
pValue = plot(value,color=buColor,title='Value',linewidth=1)
pSignal = plot(signal,color=beColor,title='Signal',linewidth=1)
fill(pValue,pSignal,color=value>signal ? 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(value,20) ? 20 : na : na,'Bullish Signal',style=shape.circle,location=location.absolute,size=size.tiny,color=buColor)
plotshape(signals ? ta.crossover(value,80) ? 80 : na : na,'Bearish Signal',style=shape.xcross,location=location.absolute,size=size.tiny,color=beColor)