//DEFPARAM CalculateOnLastBars = 5000
//ONCE Periods = 50
srcH = high //or CLOSE
srcL = low //or CLOSE
// indicator BEGINNING
tw = high - max(open, close)
bw = min(open, close) - low
body = abs(close - open)
if volume then
if open<=close then
deltaup = volume*(0.5 * (tw + bw + (2 * body)) / (tw + bw + body))
endif
if open > close then
deltadown = volume*(0.5 * (tw + bw + (2 * body)) / (tw + bw + body))
endif
endif
if close >= open then
delta= deltaup
else
delta= -deltadown
endif
cumdelta = cumsum(delta)
//drawcandle(cumdelta[1],max(cumdelta, cumdelta[1]),min(cumdelta, cumdelta[1]),cumdelta)
Cmax = max(cumdelta, cumdelta[1])
Cmin = min(cumdelta, cumdelta[1])
// indicator END
//
IF BarIndex > Periods THEN
Diverg = 0
HHbar = HighestBars[Periods](srcH[1])
LLbar = lowestBars[Periods](srcL[1])
// BULLISH divergence
IF high[HHbar] > high THEN
IF Cmax[HHbar] <= Cmax THEN
Diverg = 1
ENDIF
ENDIF
//
// BEARISH divergence
IF low[HHbar] < low THEN
IF Cmin[HHbar] >= Cmin THEN
Diverg = -1
ENDIF
ENDIF
//
ENDIF
x = Diverg
IF Diverg = Diverg[1] THEN
x = 0
ENDIF
return x AS "Divergence",0 AS "Zero"