You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将VWAP锚定到枢轴高低点?解决锚定延迟问题

解决方案:让锚定VWAP从枢轴点本身开始

问题核心是ta.pivothigh()/ta.pivotlow()需要等待rightbars根K线走完才会确认枢轴点,导致锚点触发延迟。要让VWAP从枢轴点本身开始计算,需先记录枢轴点位置,待确认后回溯触发VWAP锚定。

具体实现步骤

  1. 标记潜在枢轴点(无需等待rightbars确认)
  2. 当枢轴点被确认时,记录该枢轴点的原始K线索引
  3. 基于记录的索引,让VWAP从枢轴点K线开始计算

修改后的完整代码

//@version=5
indicator("Automated Anchored VWAP", shorttitle="aaVWAP", overlay=true)
vw_g1   = "Pivots Settings"
vw_g2   = "Tracking Settings"
s_piv   = input(true,       "Show Pivots",          group=vw_g1)
_lb     = input.int(10,     "Left Bars Lookback",   group=vw_g1)
_rb     = input.int(10,     "Right Bars Lookahead", group=vw_g1)

// 1. 识别潜在枢轴点(当前K线是否可能成为枢轴点,先不等待rightbars确认)
potential_ph = ta.highest(_lb) == high
potential_pl = ta.lowest(_lb) == low

// 2. 等待rightbars后确认枢轴点,同时记录枢轴点的原始K线索引
var int ph_anchor_bar = na
var int pl_anchor_bar = na

// 确认枢轴高点,并记录其原始位置
if ta.pivothigh(_lb, _rb)
    ph_anchor_bar := bar_index - _rb  // 回溯到枢轴点所在的K线

// 确认枢轴低点,并记录其原始位置
if ta.pivotlow(_lb, _rb)
    pl_anchor_bar := bar_index - _rb  // 回溯到枢轴点所在的K线

// 3. 定义VWAP的锚点条件:当前K线 >= 枢轴点K线,这里取最近的枢轴点作为锚
current_anchor = not na(ph_anchor_bar) ? ph_anchor_bar : pl_anchor_bar
vwap_trigger = bar_index == current_anchor and not na(current_anchor)

// 计算锚定VWAP:从枢轴点K线开始累加
[vwap, upper, lower] = ta.vwap(open, vwap_trigger, 1)

// 枢轴点标记(保持原逻辑,offset=-_rb确保显示在正确位置)
_lh     = ta.valuewhen(ta.pivothigh(_lb, _rb), high, 0) < ta.valuewhen(ta.pivothigh(_lb, _rb)[1], high[1], 0)
_hh     = ta.valuewhen(ta.pivothigh(_lb, _rb), high, 0) > ta.valuewhen(ta.pivothigh(_lb, _rb)[1], high[1], 0)
_ll     = ta.valuewhen(ta.pivotlow(_lb, _rb)[1], low[1], 0) > ta.valuewhen(ta.pivotlow(_lb, _rb), low, 0)
_hl     = ta.valuewhen(ta.pivotlow(_lb, _rb)[1], low[1], 0) < ta.valuewhen(ta.pivotlow(_lb, _rb), low, 0)

plotshape(s_piv ? _hh : na, 'Higher High', style=shape.square, location=location.abovebar, color=color.new(color.green, 50), text='[HH]', offset=-_rb, size=size.auto)
plotshape(s_piv ? _lh : na, 'Higher Low',  style=shape.square, location=location.abovebar, color=color.new(color.red, 50),   text='[LH]', offset=-_rb, size=size.auto)
plotshape(s_piv ? _ll : na, 'Lower Low',   style=shape.square, location=location.belowbar, color=color.new(color.red, 50),   text='[LL]', offset=-_rb, size=size.auto)
plotshape(s_piv ? _hl : na, 'Lower High',  style=shape.square, location=location.belowbar, color=color.new(color.green, 50), text='[HL]', offset=-_rb, size=size.auto)

// 绘制VWAP
plot(vwap, color=color.blue, title="Anchored VWAP")
plot(upper, color=color.green, title="VWAP Upper Band")
plot(lower, color=color.red, title="VWAP Lower Band")

关键逻辑说明

  • 记录枢轴点索引:当ta.pivothigh()确认时,通过bar_index - _rb得到枢轴点实际所在的K线索引(确认发生在枢轴点之后的第_rb根K线)。
  • 锚定触发条件:使用bar_index == current_anchor作为ta.vwap()的锚点参数,确保VWAP从枢轴点K线开始计算,而非确认K线。
  • 多枢轴点处理:代码默认取最近的枢轴点作为锚,你可以根据需求调整逻辑(比如保留所有历史枢轴点的VWAP,或切换锚点规则)。

内容的提问来源于stack exchange,提问作者Simon Križnik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 02:35:56