Distance from Vwap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MensaTrader

//@version=5
indicator("Distance from Vwap", overlay=false, timeframe="", timeframe_gaps=false)
 
//Titles
vwapTitle   = "=== VWAP Settings ==="
plots       = "=== Plots ==="

tool        = "Distance to lookback and compare relative values with"


//Inputs
vwapRes     = input.timeframe("W", title="VWAP Resolution", group=vwapTitle)
threshold   = input.int(3, title="Distance Threshold", group=vwapTitle, tooltip=tool)*100

bullColor   = input.color(#00E600, title="Bull", group=plots, inline="1")
bearColor   = input.color(#FF0000, title="Bear", group=plots, inline="1")


//Calculations
v           = ta.vwap
vwap        = request.security(syminfo.tickerid, vwapRes, v, barmerge.gaps_off)
distance    = (close>vwap ? (close-vwap)/close : (vwap-close)/vwap) * 100

recentH     = ta.highest(distance, threshold)
recentL     = ta.lowest(distance, threshold)

c           = distance>=recentH ? close>vwap ? bearColor : bullColor : distance <= recentL ? color.orange : color.gray


//Plot
plot(distance, style=plot.style_histogram, color=c, title="VWAP Distance")



