source code import TradingView/ta/5 // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © wbburgin //@version=5 strategy("Volume-Weighted Supertrend Strategy [wbburgin]", overlay=true, pyramiding=5,default_qty_type = strategy.percent_of_equity, default_qty_value = 5, commission_value = 0.08, initial_capital = 100000) import wbburgin/wbburgin_utils/2 as u vwsupertrend(src, length, factor, atrPeriod) => atr = ta.atr(atrPeriod) sumPriceVolume = 0.0 sumVolume = 0.0 for i = 0 to length - 1 sumPriceVolume += src[i] * volume[i] sumVolume += volume[i] vwap = sumPriceVolume / sumVolume upperBand = vwap + factor * atr lowerBand = vwap - factor * atr prevLowerBand = nz(lowerBand[1]) prevUpperBand = nz(upperBand[1]) lowerBand := lowerBand > prevLowerBand or src[1] < prevLowerBand ? lowerBand : prevLowerBand upperBand := upperBand < prevUpperBand or src[1] > prevUpperBand ? upperBand : prevUpperBand int direction = na float superTrend = na prevSuperTrend = superTrend[1] if na(atr[1]) direction := 1 else if prevSuperTrend == prevUpperBand direction := close > upperBand ? -1 : 1 else direction := close < lowerBand ? 1 : -1 superTrend := direction == -1 ? lowerBand : upperBand [superTrend, direction] showStrategy = input(false, title="Show Strategy Entries and Exits") src = input.source(close,"Source") atrPeriod = input.int(10,"ATR Period") volPeriod = input.int(10,"Volume Length") factor = input.float(3.0,"ATR Multiplier") highlighting = input.bool(true,"Highlighting") labels = input.bool(true,"Labels") [supertrend,dir] = vwsupertrend(src, volPeriod, factor, atrPeriod) supertrend_up = dir == -1 ? supertrend : na supertrend_dn = dir == 1 ? supertrend : na supertrend_up_start = dir == -1 and dir[1] == 1 ? supertrend : na supertrend_dn_start = dir == 1 and dir[1] == -1 ? supertrend : na in_rising_trend = supertrend < src [volume_supertrend,volume_direction] = u.supertrend_anysource(volume,factor,volPeriod) volume_supertrend_change_up = volume_direction[1] == 1 and volume_direction == -1 volume_supertrend_change_dn = volume_direction[1] == -1 and volume_direction == 1 in_rising_volume = volume_supertrend < volume buyconditions = (in_rising_volume and supertrend_up_start) or (volume_supertrend_change_up and in_rising_trend) sellconditions = not in_rising_volume and supertrend_dn_start or (volume_supertrend_change_dn and not in_rising_trend) if buyconditions and showStrategy strategy.entry("Long",strategy.long) if sellconditions and showStrategy strategy.close("Long") changeColor = volume_supertrend_change_dn ? color.orange : volume_supertrend_change_up ? color.yellow : na linecolor = switch dir == -1 and in_rising_volume => color.green dir == 1 and in_rising_volume => color.red dir == -1 and not in_rising_volume => color.new(color.blue,50) dir == 1 and not in_rising_volume => color.new(color.orange,50) fillcolor = switch dir == -1 and in_rising_volume => color.new(color.green,88) dir == 1 and in_rising_volume => color.new(color.red,88) dir == -1 and not in_rising_volume => color.new(color.blue,94) dir == 1 and not in_rising_volume => color.new(color.orange,94) sup = plot(supertrend_up,title="Up Supertrend",color=linecolor,style=plot.style_linebr,linewidth=2) sdn = plot(supertrend_dn,title="Down Supertrend",color=linecolor,style=plot.style_linebr,linewidth=2) mid = plot(hl2,color=na,title="HL2 (for Highlighting)") plotshape(supertrend_up_start,title="Supertrend Up Start",style=shape.circle,location=location.absolute, color=linecolor, size=size.tiny) plotshape(supertrend_dn_start,title="Supertrend Down Start",style=shape.circle,location=location.absolute, color=linecolor, size=size.tiny) plotshape(labels==false?na:buyconditions ? supertrend : na,title="Supertrend Buy Label",style=shape.labelup,location=location.absolute, color=color.green,textcolor=color.white,text = "Buy") plotshape(labels==false?na:sellconditions ? supertrend : na,title="Supertrend Sell Label",style=shape.labeldown,location=location.absolute, color=color.red,textcolor=color.white,text = "Sell") fill(sup,mid,highlighting==true ? fillcolor : na) fill(sdn,mid,highlighting==true ? fillcolor : na)