可以使用pine脚本中的line.new()函数来手动绘制固定均价交易量加权平均线。例如,下面的代码片段展示了如何绘制固定均价交易量加权平均线并在一定条件下开始绘制:
//@version=4
study("Anchored VWAP Manual Plotting", overlay=true)
//定义条件
cond = close > open
//手动计算和绘制固定均价交易量加权平均线
float cumulativeVolume = 0.0
float cumulativeTP = 0.0
float cumulativeVWAP = 0.0
float totalVolume = 0.0
float totalTP = 0.0
int anchorPeriod = input(20, title="Anchor period")
period = input("D", title="Period")
res = security(syminfo.tickerid, period, resolution)
cumulativeVolume := cum(res * volume)
cumulativeTP := cum(res * tp)
totalVolume := cumulativeVolume - cumulativeVolume[anchorPeriod]
totalTP := cumulativeTP - cumulativeTP[anchorPeriod]
cumulativeVWAP := totalTP / totalVolume
plot(cumulativeVWAP, title="Anchored VWAP", color=color.blue, linewidth=2, trackprice=true, offset=-anchorPeriod)
//只在条件为真时开始绘制
if (cond)
line.new(bar_index-offset, cumulativeVWAP, bar_index, cumulativeVWAP, extend=extend.none, color=color.yellow)
在此示例中,line.new()函数在条件为真时开始绘制固定均价交易量加权平均线。然后,代码使用plot()函数绘制曲线。在line.new()函数中,bar_index表示当前柱子的索引,offset表示线条绘制的时间偏移,extend表示线条的延伸方式,color表示线条的颜色。