VWAP with Characterization
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AleSaira

//============///////==========================/////////////////===============}
//@version=5
indicator("VWAP with Characterization", overlay = true)
//============///////==========================/////////////////===============}
sma_length          =   input   (14, title="SMA Length")

// Input per la scelta del calcolo del VWAP
useCustomVWAP       =   input   (true, title="Use Custom VWAP Calculation")
//============///////==========================/////////////////===============}
// Calcola il VWAP in base alla scelta dell'utente
vwap = useCustomVWAP ? ta.sma(close * volume, sma_length) / ta.sma(volume, sma_length) : ta.sma(request.security(syminfo.tickerid, "1D", close * volume) / request.security(syminfo.tickerid, "1D", volume), sma_length)

char(vwap_value, length) =>
    sum = 0.0
    for i = 0 to length - 1
        sum := sum + vwap_value[i]
    sum / length

char_vwap = char(vwap, sma_length)
//============///////==========================/////////////////===============}
isUptrend = ta.highestbars(high, 10) < ta.highestbars(low, 10)
isDowntrend = ta.highestbars(high, 10) > ta.highestbars(low, 10)
//============///////==========================/////////////////===============}
linecolor = isUptrend ? color.blue : isDowntrend ? color.rgb(255, 82, 238) : color.rgb(243, 254, 144)
plot(char_vwap, title="Characterized VWAP", color=linecolor, linewidth=2)
//============///////==========================/////////////////===============}
// Set alarm when the 200 period EMA rises above or below the characterized VWAP
vwapThreshold = input.float(200, title="VWAP Threshold")
vwapAboveThreshold = ta.crossover(char_vwap, vwapThreshold)
vwapBelowThreshold = ta.crossunder(char_vwap, vwapThreshold)

alertcondition(vwapAboveThreshold, title="VWAP Above Threshold Alert", message="VWAP has crossed above the specified threshold.")
alertcondition(vwapBelowThreshold, title="VWAP Below Threshold Alert", message="VWAP has crossed below the specified threshold.")
//============///////==========================/////////////////===============}

