Moving Average of Volume for Up and Down Closes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mfgfund

//@version=5
indicator("Moving Average of Volume for Up and Down Closes", overlay = false)

// Input for moving average length
length = input.int(7, title = "MA Length")

// Calculate the moving average of volume for bars with an up close
upVolume = close > open ? volume : 0
maUp = ta.sma(upVolume, length)

// Calculate the moving average of volume for bars with a down close
downVolume = close < open ? volume : 0
maDown = ta.sma(downVolume, length)

// Plot the moving averages in a separate pane from the price chart
plot(maUp, color = color.green, title = "MA Up")
plot(maDown, color = color.red, title = "MA Down")
