S2BU2 Volume Oscillator (Improved)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sucks2bu2

//@version=5
indicator(title="S2BU2 Volume Oscillator", shorttitle="S2BU2 VolO", format=format.percent, precision=2, overlay = false, timeframe="", timeframe_gaps=false)

//==================================define Inputs==================================//
periodShort = input.int(defval = 5,  minval = 1, title = 'Period Short', group = 'Oscillator Settings')
periodLong  = input.int(defval = 10, minval = 1, title = 'Period Long',  group = 'Oscillator Settings')
periodFast  = input.int(defval = 10, minval = 1, title = 'Period Fast',  group = 'Moving Average Settings')
periodSlow  = input.int(defval = 26, minval = 1, title = 'Period Slow',  group = 'Moving Average Settings')



//===============================define Helper Vars================================//
var float cumulativeVol = 0
cumulativeVol += nz(volume)

var highs = array.new_float(0)
var lows  = array.new_float(0)

int lookback = 2
int lookbackCont = lookback + 1



//==========================calculate Plot Variables===============================//
//get Oscillator Values
float shortOsc = ta.ema(volume, periodShort)
float longOsc  = ta.ema(volume, periodLong)

//convert to percentage
float oscillator = 100 * (shortOsc - longOsc) / longOsc

//Get Peaks
if nz(oscillator[lookback]) < nz(oscillator[0]) and  nz(oscillator[lookback+1]) < nz(oscillator[lookback-1]) and nz(oscillator[lookbackCont]) < nz(oscillator[0]) and  nz(oscillator[lookbackCont+1]) < nz(oscillator[lookbackCont-1])
    array.push(highs, oscillator)
float avgHigh = array.avg(highs)

//Get Bottoms
if nz(oscillator[lookback]) > nz(oscillator[0]) and  nz(oscillator[lookback+1]) > nz(oscillator[lookback-1]) and nz(oscillator[lookbackCont]) > nz(oscillator[0]) and  nz(oscillator[lookbackCont+1]) > nz(oscillator[lookbackCont-1])
    array.push(lows, oscillator)
float avgLow = array.avg(lows)

//Get MAs
float fastMA = ta.ema(oscillator, periodFast)
float slowMA = ta.ema(oscillator, periodSlow)



//====================================plot=========================================//
//lines
hline(0,      color = color.white, linestyle = hline.style_dashed, title = 'Zero Line')
plot(avgHigh, color = color.white, linewidth = 1, title = 'Average Highs')
plot(avgLow,  color = color.white, linewidth = 1, title = 'Average Lows')

//MAs
plot(fastMA,  color = color.orange, linewidth = 1, title = 'Moving Average')
plot(slowMA,  color = color.gray,   linewidth = 1, title = 'Singal')