我正在按部分计算VWAP,每次收盘价与运行中的VWAP的差异大于偏差时,它将反转趋势并开始新的VWAP计数。应当在每个趋势内汇总交易量。
到目前为止,交易量聚集在上升趋势上,但没有聚集在下降趋势上。同样,当从下降切换到上升时,上升趋势量会“窃取”最后的下降趋势量并将其添加到自己的价格中。这一切都很令人困惑,因为逻辑很简单...
这是我的代码:
//@version=3
study("My Script")
deviation = input(title = "Deviation %", type=float, defval = 0.1)
running_vol = 0.0
running_sum = 0.0
Tup = true
Tdown = false
running_vol := nz(volume[1]) == 0 ? 0 : running_vol[1] + volume
running_sum := nz(volume[1]) == 0 ? 0 : running_sum[1] + (close*volume)
volwap = (running_sum/running_vol)
// flip to downtrend
if (Tup == true) and (Tdown == false) and (close < close[1]) and ((1 - (close/volwap)) > (deviation/100.0))
// reset running_vol and sum to current volume and sum since it's a new trend
running_vol := volume
running_sum := (close*volume)
// flip the trend switches
Tup := false
Tdown := true
// flip to uptrend
if (Tup == false) and (Tdown == true) and (close > close[1]) and (((close/volwap) - 1) > (deviation/100.0))
running_vol := volume
running_sum := (close*volume)
Tup := true
Tdown := false
up = Tup == true ? running_vol : 0
down = Tdown == true ? running_vol : 0
plot(up, style=histogram, color=green, linewidth=3)
plot(down, style=histogram, color=red, linewidth=3)
答案 0 :(得分:1)
在原始脚本中,Tup和Tdown的自引用是有问题的。您必须参考过去的Tup和Tdown,否则在每次通过脚本扫描时都会重新引入用户定义的Tup = true和tdown = false。由于每次扫描都会将Tup重新初始化为true,因此您一次只能有一个熊的体积线。对于这种新颖而有趣的波浪定义,我还看到了所需的转折点策略的问题。某些游戏可能会找到比此脚本中的剧本更好地满足您需求的转折点。我曾经尝试过相对于close [0]保持对volwap和close [1]的使用,但是我不确定我是否已按照您的实际意图捕获了它。希望这为您完善波形定义提供了一个起点。这是我的松树脚本翻译代码。干杯杰伊:
//@version=3
// my impression of the Weis VWAP code by Moreina by Jayy
study("Moreina Weis vwap")
deviation = input(title = "Deviation %", type=float, defval = 0.00000000)
running_vol = 0.0
running_sum = 0.0
Tup = 0
count=1
count:= nz(count[1])+1
running_vol := Tup[1]!=Tup[2] and nz(running_vol[1])==nz(volume[1])? nz(running_vol[1]) + volume: (Tup[1]==1 and Tup[2]==1) or (Tup[1]==-1 and Tup[2]==-1)? nz(running_vol[1]) + volume:na
running_sum := Tup[1]!=Tup[2] and nz(running_sum[1])==nz(close[1]*volume[1])? nz(running_sum[1]) + close*volume: (Tup[1]==1 and Tup[2]==1) or (Tup[1]==-1 and Tup[2]==-1)? nz(running_sum[1]) +close* volume:na
volwap = (running_sum/running_vol)
// flip to downtrend
if ((Tup[1] == 1) or (Tup[1] == 0)) and not ((close > close[1]) or (close/volwap)>1) //
// reset running_vol and sum to current volume and sum since it's a new trend
running_vol := volume
running_sum := (close*volume)
// flip the trend switches
Tup := -1
// flip to uptrend
if ((Tup[1] == -1) or (Tup[1] == 0)) and not ((close < close[1]) or ((close/volwap)) <1) //and (close/volwap) > 1)
running_vol := volume
running_sum := (close*volume)
Tup := 1
Tup:= nz(Tup[0])==1 and count>1?Tup[0]:nz(Tup[0])==-1 and count>1?Tup[0]: count>1 and Tup[0]==0?nz(Tup[1]):na//Tup
up = Tup == 1 ? running_vol : na
down = Tup == -1 ? running_vol : na
plot(up, style=histogram, color=green, linewidth=3)
plot(down, style=histogram, color=red, linewidth=3)