// OBV Oscillator2
//
// On Balance Volume Oscillator
//
// https://www.tradingview.com/script/Ox9gyUFA-Indicator-OBV-Oscillator/
// https://www.prorealcode.com/topic/conversion-of-pine-script-obv-osc-0-1/
//
// It makes easier to detect DIVERGENCES
//
// Periods = 20
// Periods2 = 14
Periods = max(1,min(999,Periods))
Periods2 = max(1,min(999,Periods2))
//
MyOBV = OBV(close)
ma1 = 0
IF BarIndex > max(Periods,Periods2) THEN
Sma = Average[Periods,0](MyOBV)
Diff = MyOBV - Sma
// --- added later:
// (https://www.prorealcode.com/topic/conversion-of-pine-script-obv-osc-0-1/#post-177572)
Sma = Average[Periods,0](MyOBV)
Diff = MyOBV - Sma
//////////////////////////////////
ONCE wmaPERC = 1 / Periods2
ONCE maP = max(1,round(exp(0.5*log(Periods2))))
//
ma1 = (Diff * wmaPERC) + (ma1[1] * (1 - wmaPERC)) //Wilder Average 1
//ma1 = ((ma1[1] * (Periods2 - 1)) + Diff) / Periods2 //Wilder Average 2
//
ma2 = average[maP,0](ma1)
r = 255 //Orange
g = 100
b = 10
IF ma1 > ma2 THEN
r = 20 //Cyan
g = 150
b = 255
ENDIF
ma3 = ma1 * exp(0.15*log(Periods2))
//DrawText("•",BarIndex,Ma3,Dialog,Bold,10) coloured(r,g,b,255)
// --- end additional code
ENDIF
RETURN Diff AS "Difference",ma3 AS "ma3"