VWAP Bollinger Band Crossover Breakout with Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RickVBlack 
// credit to // © Jesus_Salvatierra for VWAP script

//@version=4
study(title = "VWAP Bollinger Band Crossover", shorttitle = "VWAP BB Cross", overlay = true)
// Inputs
price =  input(close, title='Price')
bb_length = input(20, title='Bollinger length')
bb_multiplier = input(2, title='Bollinger Multiplier')
// BB
bb_basis = sma(price, bb_length)
bb_dev = bb_multiplier * stdev(price, bb_length)
bb_upper = bb_basis + bb_dev
bb_lower = bb_basis - bb_dev
// Plots
bbc = crossover(price, bb_upper)
plotshape(bbc, title = "BB Cross", location = location.belowbar, style = shape.triangleup, color = #ff2f00, size = size.small)
bbu = crossunder(bb_upper, bb_basis)
plotshape(bbu, title = "BB Under", location = location.belowbar, style = shape.triangledown, color = #160b2f, size = size.small)





computeVWAP(src, isNewPeriod, stDevMultiplier) =>
	var float sumSrcVol = na
	var float sumVol = na
    var float sumSrcSrcVol = na
    
	sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]
	sumVol := isNewPeriod ? volume : volume + sumVol[1]
	// sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
	sumSrcSrcVol := isNewPeriod ? volume * pow(src, 2) : volume * pow(src, 2) + sumSrcSrcVol[1]
	
	_vwap = sumSrcVol / sumVol
	variance = sumSrcSrcVol / sumVol - pow(_vwap, 2)
	variance := variance < 0 ? 0 : variance
	stDev = sqrt(variance)
	
	lowerBand = _vwap - stDev * stDevMultiplier
	upperBand = _vwap + stDev * stDevMultiplier
	
	[_vwap, lowerBand, upperBand]

hideonDWM = input(false, title="Hide VWAP on 1D or Above", group="VWAP Settings")
var anchor = input(defval = "Session", title="Anchor Period", type=input.string, 
 options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group="VWAP Settings")
src = input(title = "Source", type = input.source, defval = hlc3, group="VWAP Settings")
offset = input(0, title="Offset", group="VWAP Settings")

showBands = input(true, title="Calculate Bands", group="Standard Deviation Bands Settings")
stdevMult = input(2.0, title="Bands Multiplier", group="Standard Deviation Bands Settings")

timeChange(period) =>
	change(time(period))

new_earnings = earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on)
new_dividends = dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on)
new_split = splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on)

isNewPeriod = anchor == "Earnings" ? new_earnings :
 anchor == "Dividends" ? new_dividends :
 anchor == "Splits" ? new_split :
 na(src[1]) ? true :
 anchor == "Session" ? timeChange("D") :
 anchor == "Week" ? timeChange("W") :
 anchor == "Month" ? timeChange("M") :
 anchor == "Quarter" ? timeChange("3M") :
 anchor == "Year" ? timeChange("12M") :
 anchor == "Decade" ? timeChange("12M") and year % 10 == 0 :
 anchor == "Century" ? timeChange("12M") and year % 100 == 0 :
 false
	
float vwapValue = na
float std = na
float upperBandValue = na
float lowerBandValue = na

if not (hideonDWM and timeframe.isdwm)
    [_vwap, bottom, top] = computeVWAP(src, isNewPeriod, stdevMult)
    vwapValue := _vwap
    upperBandValue := showBands ? top : na
    lowerBandValue := showBands ? bottom : na

plot(vwapValue, title="VWAP", color=#a229ff, offset=offset)

upperBand = plot(upperBandValue, title="Upper Band", color=color.green, offset=offset)
lowerBand = plot(lowerBandValue, title="Lower Band", color=color.green, offset=offset)

fill(upperBand, lowerBand, title="Bands Fill", color= showBands ? color.new(#c2dac3, 95) : na)

//SAR

start = input(title = "Start", defval = 0.02, step = 0.001)
increment = input(title = "Increment", defval = 0.02, step = 0.001)
maximum = input(title = "Max Value", defval = 0.2, step = 0.01)
putlabel = input(title = "Sell", defval = true)
colup = input(title = "Colors", defval = color.rgb(230, 138, 0), inline = "col")
coldn = input(title = "", defval = color.rgb(19, 16, 21), inline = "col")

int trend = 0
float sar = 0.0
float ep = 0.0
float af = 0.0

trend := nz(trend[1])
ep := nz(ep[1])
af :=nz(af[1])
sar := sar[1]

if trend == 0 and not na(high[1])
    trend := high >= high[1] or low >= low[1] ? 1 : -1
    sar := trend > 0 ? low[1] : high[1]
    ep := trend > 0 ? high[1] : low[1]
    af := start
else 
    nextsar = sar
    if trend > 0
        if high[1] > ep
            ep := high[1]
            af := min(maximum, af + increment)
            
        nextsar := sar + af * (ep - sar)
        nextsar := min(min(low[1], low[2]), nextsar)
        
        //Reversal
        if nextsar > low
            trend := -1
            nextsar := ep
            ep := low
            af := start
    else 
        if low[1] < ep
            ep := low[1]
            af := min(maximum, af + increment)
        
        nextsar := sar + af * (ep - sar)
        nextsar := max(max(high[1], high[2]), nextsar)
        
        //Reversal
        if nextsar < high
            trend := 1
            nextsar := ep
            ep := high
            af := start
    sar := nextsar

//plot(sar, title = "Parabolic SAR", color = trend > 0 ? colup : coldn, linewidth = 2, style = plot.style_circles)

alertcondition(change(trend) > 0, title='PSAR Trend UP', message='PSAR Trend UP')
alertcondition(change(trend) < 0, title='PSAR Trend DOWN', message='PSAR Trend DOWN')


if change(trend) < 0 and putlabel
	label.new(bar_index, sar, text = "", color = coldn, style=label.style_labeldown, size = size.tiny)