Volume+
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified

//@version=5
indicator("Volume+")
import Electrified/Color/9

MEASUREMENT = "Measurement"
len = input.int(400, "Length", group = MEASUREMENT, tooltip = "The number of bars to measure the weighted average.")
smooting = input.int(50, "Smoothing", group = MEASUREMENT, tooltip = "The number of bars to smooth out the averages.")

getMean(float source) =>
    ta.sma(ta.wma(source, len), smooting)

mean = getMean(volume)

diff = nz(volume - mean)
diff2 = diff * diff
variance = getMean(diff2)
standardDeviation = math.sqrt(variance)

plus1 = mean + standardDeviation
plus2 = plus1 + standardDeviation
plus3 = plus2 + standardDeviation

HIGHLIGHT = "Bar Highlight"
BRIGHTEN = "Brighten", DARKEN = "Darken", NONE = "None"
highLight = input.string(BRIGHTEN, "Mode", group = HIGHLIGHT, options = [BRIGHTEN, DARKEN, NONE], tooltip = "How to affect the bar color when the volume exeeds one standard deviation.")
warmRange = plus3 - plus1
warmth = 100 * (volume - plus1) / warmRange

THEME = "Theme"
barTheme = input.color(color.gray, "Bar", group = THEME, inline = THEME)
meanColor = input.color(color.orange, "Average", group = THEME, inline = THEME)
devColor = input.color(color.yellow, "Deviation", group = THEME, inline = THEME)

barColor = switch highLight
    NONE => barTheme
    BRIGHTEN => barTheme.brighten(math.max(0, warmth))
    DARKEN =>  barTheme.darken(math.max(0, warmth))



// Always lag by one bar to ensure that the current bar doesn't influence the value.
plot(plus1[1], "+1 Standard Deviation", color.new(devColor, 50), offset = 1, display = display.pane)
plot(plus2[1], "+2 Standard Deviations", color.new(devColor, 70), offset = 1, display = display.pane)
plot(plus3[1], "+3 Standard Deviations", color.new(devColor, 90), offset = 1, display = display.pane)
plot(volume, "Volume", barColor, style = plot.style_columns)
plot(mean[1], "Average (WMA)", color.new(meanColor, 30), offset = 1)


