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

求助:修正TradingView中Heikin-Ashi蜡烛图高低点指标绘制问题

问题分析与解决方案

你的代码核心逻辑是对的,但标记位置偏移的问题出在最高点/最低点的范围判断和偏移计算的精准性上。原代码的highestbars(6)会包含后续3根下跌蜡烛的价格,导致标记可能错误地画在下跌蜡烛上;另外,偏移计算没有严格绑定触发条件的6根蜡烛范围。

修正后的代码(贴合常规摆动点逻辑)

以下代码确保Swing High标记仅出现在前3根上涨蜡烛的最高点,Swing Low标记仅出现在前3根下跌蜡烛的最低点,完全匹配你描述的“3涨接3跌/3跌接3涨”的摆动点定义:

//@version=5
indicator("Heikin-Ashi Swing Points", overlay=true)

// 封装Heikin-Ashi涨跌判断,简化代码
ha_up(idx) => open[idx] < close[idx]
ha_down(idx) => open[idx] > close[idx]

// Swing High条件:连续3根上涨后接连续3根下跌
swing_high = ha_up(5) and ha_up(4) and ha_up(3) and ha_down(2) and ha_down(1) and ha_down(0)
// Swing Low条件:连续3根下跌后接连续3根上涨
swing_low = ha_down(5) and ha_down(4) and ha_down(3) and ha_up(2) and ha_up(1) and ha_up(0)

// 计算Swing High偏移:仅在前3根上涨蜡烛中找最高点
sh_offset = swing_high ? ta.highestbars(high[3], 3) - 3 : na
// 计算Swing Low偏移:仅在前3根下跌蜡烛中找最低点
sl_offset = swing_low ? ta.lowestbars(low[3], 3) - 3 : na

// 绘制摆动点标记
plotchar(swing_high, title='Swing High', char='•', location=location.abovebar, color=color.blue, offset=sh_offset, size=size.small)
plotchar(swing_low, title='Swing Low', char='•', location=location.belowbar, color=color.red, offset=sl_offset, size=size.small)

关键修改点说明

  1. 范围精准限定:

    • 对于Swing High,我们只在触发条件的前3根上涨蜡烛(索引5、4、3)中查找最高点,避免后续下跌蜡烛干扰。
    • ta.highestbars(high[3], 3)专门计算这3根上涨蜡烛内部的最高点位置,再减去3得到相对于当前K线的偏移值,确保标记精准落在目标K线上。
  2. 代码可读性优化:

    • 封装了Heikin-Ashi涨跌判断函数,让条件逻辑更清晰,便于后续维护。

如果你需要标记全部6根蜡烛的最高点/最低点

若你确实希望标记覆盖全部6根蜡烛的极值,可使用以下代码(修正偏移计算的绑定逻辑):

//@version=5
indicator("Heikin-Ashi Swing Points", overlay=true)

swing_high = open[5] < close[5] and open[4] < close[4] and open[3] < close[3] and open[2] > close[2] and open[1] > close[1] and open[0] > close[0]
swing_low = open[5] > close[5] and open[4] > close[4] and open[3] > close[3] and open[2] < close[2] and open[1] < close[1] and open[0] < close[0]

// 仅在条件触发时计算偏移,避免无效值干扰
sh_offset = swing_high ? ta.highestbars(high, 6) : na
sl_offset = swing_low ? ta.lowestbars(low, 6) : na

plotchar(swing_high, title='Swing High', char='•', location=location.abovebar, color=color.blue, offset=sh_offset, size=size.small)
plotchar(swing_low, title='Swing Low', char='•', location=location.belowbar, color=color.red, offset=sl_offset, size=size.small)

内容的提问来源于stack exchange,提问作者The Ronin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:10:29