我正在分段计算 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)