MW Volume Impulse
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MWRIGHT, INC
//
// Credit to @Ankit_1618 for the Cumulative Volume Delta indicator that served as the inspiration
// https://www.tradingview.com/script/lsDHdn0H-Cumulative-Volume-Delta/

//@version=5
indicator('MW Volume Impulse')
i_period            = input.int(14, "Moving Average Period", maxval=3600, minval=1)
i_dotSize           = input.int(10, "Dot Size", minval=1, step=1)
i_dotTransparency   = input.int(50, "Dot Transparency", maxval=100, minval=0, step=10)
i_ema               = input(7,"EMA")

ema = ta.sma(close,i_ema)

upperWick = close > open ? high - close : high - open
lowerWick = close > open ? open - low : close - low
spread = high - low
bodyLength = spread - (upperWick + lowerWick)

percentUpperWick = upperWick / spread
percentLowerWick = lowerWick / spread
percentBodyLength = bodyLength / spread

buyingVolume = close > open ? (percentBodyLength + (percentUpperWick + percentLowerWick) / 2) * volume : (percentUpperWick + percentLowerWick) / 2 * volume
sellingVolume = close < open ? (percentBodyLength + (percentUpperWick + percentLowerWick) / 2) * volume : (percentUpperWick + percentLowerWick) / 2 * volume

accumulationLength = input(14)
cumulativeBuyingVolume = ta.ema(buyingVolume, accumulationLength)
cumulativeSellingVolume = ta.ema(sellingVolume, accumulationLength)
cumulativeVolumeDelta = cumulativeBuyingVolume - cumulativeSellingVolume

// Functions
flippedUp(_series,_period=1) =>
     _series > 0 and _series[_period] < 0

flippedDown(_series,_period=1) =>
     _series < 0 and _series[_period] > 0


cvdMaDifference = cumulativeVolumeDelta - ta.ema(cumulativeVolumeDelta,i_period)
cvdMaDiffFlipUp = flippedUp(cvdMaDifference)
cvdMaDiffFlipDown = flippedDown(cvdMaDifference)

flipUpPlusMA = cvdMaDiffFlipUp and close > ta.ema(close,7) and (close > open[1] or close > open[2])
flipDownPlusMA = cvdMaDiffFlipDown and close < ta.ema(close,7) and (close < open[1] or close < open[2])

plot(cumulativeVolumeDelta, color = cumulativeVolumeDelta > 0 ? color.green : color.red, style=plot.style_columns)
plot(flipUpPlusMA ? cvdMaDifference : na, color = flipUpPlusMA ? color.new(color.green, i_dotTransparency) : na, style=plot.style_circles, linewidth=i_dotSize)
plot(flipDownPlusMA ? cvdMaDifference : na, color = flipDownPlusMA ? color.new(color.red, i_dotTransparency) : na, style=plot.style_circles, linewidth=i_dotSize)
plot(flipUpPlusMA ? cvdMaDifference : na, color = flipUpPlusMA ? color.new(color.white, i_dotTransparency) : na, style=plot.style_circles, linewidth=i_dotSize/2)
plot(flipDownPlusMA ? cvdMaDifference : na, color = flipDownPlusMA ? color.new(color.white, i_dotTransparency) : na, style=plot.style_circles, linewidth=i_dotSize/2)
plot(ta.ema(cumulativeVolumeDelta,i_period), color=color.new(color.white,0))

alertcondition(cvdMaDiffFlipUp, title="LONG! MW - CVD went above its MA", message="LONG: Cumulative Volume Delta just went above its Moving Average!\nPrice: {{close}}")
alertcondition(cvdMaDiffFlipDown, title="SHORT! MW - CVD went below its MA", message="SHORT: Cumulative Volume Delta just went below its Moving Average!\nPrice: {{close}}")
alertcondition(flipUpPlusMA, title="LONG! MW - CVD above its MA/Price in Zone", message="LONG: Cumulative Volume Delta just went above its Moving Average, and its in the green zone of the selected EMA and SMA!\nPrice: {{close}}")
alertcondition(flipDownPlusMA, title="SHORT! MW - CVD below its MA/Price in Zone", message="SHORT: Cumulative Volume Delta just went below its Moving Average, and its in the red zone of the selected EMA and SMA!\nPrice: {{close}}")










