VWAP Trendfollow Strategy [wbburgin]

wbburgin · strategy · 83 行 · 点赞 4,552 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=5
strategy(title="VWAP Trendfollow Strategy [wbburgin]", shorttitle="VWAP Trendfollow [wbburgin]", overlay=true,
 default_qty_type = strategy.percent_of_equity,default_qty_value = 50,initial_capital=1000,pyramiding=2,commission_value=0.005)
 
import wbburgin/wbburgin_utils/2 as utils
 
hideonDWM = input(false, title="Hide VWAP on 1D or Above", group="VWAP Settings")
var anchor = input.string(defval = "Session", title="Anchor Period",
 options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group="VWAP Settings")
src = input(title = "Source", defval = hlc3, group="VWAP Settings")
offset = input(0, title="Offset", group="VWAP Settings")
 
showBand_1 = input(true, title="", group="Standard Deviation Bands Settings", inline="band_1")
stdevMult_1 = input(1.0, title="Standard Deviation Multiplier", group="Standard Deviation Bands Settings", inline="band_1")
 
matchedTrends = input.bool(true,"Only Enter on Matched Trends",tooltip = "This setting will only enter a long trade"+
 " when the VWAP is trending up and will only enter a short trade when the VWAP is trending down.",group="Strategy")
 
displaybars = input.bool(true,"Barcolors",group="Display")
 
if barstate.islast and ta.cum(volume) == 0
    runtime.error("No volume is provided by the data vendor.")
 
new_earnings = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_dividends = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_split = request.splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
 
isNewPeriod = switch anchor
	"Earnings"  => not na(new_earnings)
	"Dividends" => not na(new_dividends)
	"Splits"    => not na(new_split)
	"Session"   => timeframe.change("D")
	"Week"      => timeframe.change("W")
	"Month"     => timeframe.change("M")
	"Quarter"   => timeframe.change("3M")
	"Year"      => timeframe.change("12M")
	"Decade"    => timeframe.change("12M") and year % 10 == 0
	"Century"   => timeframe.change("12M") and year % 100 == 0
	=> false
 
isEsdAnchor = anchor == "Earnings" or anchor == "Dividends" or anchor == "Splits"
if na(src[1]) and not isEsdAnchor
	isNewPeriod := true
 
float vwapValue = na
float upperBandValue1 = na
float lowerBandValue1 = na
 
if not (hideonDWM and timeframe.isdwm)
    [_vwap, _stdevUpper, _] = ta.vwap(src, isNewPeriod, 1)
	vwapValue := _vwap
    stdevAbs = _stdevUpper - _vwap
	upperBandValue1 := _vwap + stdevAbs * stdevMult_1
	lowerBandValue1 := _vwap - stdevAbs * stdevMult_1
 
vTopBreak = ta.crossover(close,upperBandValue1)
vBottomBreak = ta.crossunder(close,lowerBandValue1)
 
inLongTrade = ta.barssince(vTopBreak) < ta.barssince(vBottomBreak)
 
upV = utils.trendUp(vwapValue)
 
matchedTrendsFilter_long = not matchedTrends ? true : upV
matchedTrendsFilter_short = not matchedTrends ? true : not upV
 
if vTopBreak and matchedTrendsFilter_long
	strategy.entry("Long",strategy.long)
if vBottomBreak and matchedTrendsFilter_short
	strategy.entry("Short",strategy.short)
 
 
barcolor(vTopBreak ? color.green : vBottomBreak ? color.red : na)
 
plot(vwapValue, title="VWAP", color=color.white, offset=offset)
upperBand_1 = plot(upperBandValue1, title="Upper Band #1", color=na, offset=offset)
lowerBand_1 = plot(lowerBandValue1, title="Lower Band #1", color=na, offset=offset)
hl2_plot = plot(hl2,color=na,title="HL2")
 
fill(hl2_plot,upperBand_1,color = hl2 > upperBandValue1 ? color.new(color.green,50) : na)
fill(hl2_plot,lowerBand_1,color = hl2 < lowerBandValue1 ?  color.new(color.red,50) : na)
fill(upperBand_1, lowerBand_1, title="Bands Fill", color= upV ? color.new(color.lime,88) : color.new(color.red,88))
 
barcolor(displaybars == true ? inLongTrade ? color.lime : color.red : na)

← 返回列表