//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains
indicator('Period VWAP', overlay=true)
// @keywords: period-vwap, vwap, cumulative-vwap, typical-price, volume-weighted, rolling-period, trending-vwap, price-average, overlay-vwap, vwap-line, volume-sum, cumulative-volume, period-length, vwap-direction, anchor-free-vwap, rolling-vwap, price-volume, weighted-average, vwap-plot, period-setting
cumulativePeriod = input(100, 'Period')
typicalPrice = (high + low + close) / 3
typicalPriceVolume = typicalPrice * volume
cumulativeTypicalPriceVolume = math.sum(typicalPriceVolume, cumulativePeriod)
cumulativeVolume = math.sum(volume, cumulativePeriod)
vwapValue = cumulativeTypicalPriceVolume / cumulativeVolume
use_trending_color = input.bool(true, title = 'Use Trending Color')
var vwap_color = #000000
vwap_color := vwapValue >= vwapValue[1] ? color.new(#00ffd5,0) : vwapValue < vwapValue[1] and high <= vwapValue ? color.new(#ff0000,0) : vwap_color[1]
plot(vwapValue, title = 'Period VWAP', color = use_trending_color ? vwap_color : color.gray)