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

移除Trading View指标Higher Timeframe High & Low的重绘逻辑

解决Higher Timeframe High & Low [ChartPrime]指标的重绘问题

问题原因

原指标中plotchar使用ta.crossunder(high, pricehigh)和ta.crossover(low, pricelow)作为信号触发条件,当后续K线突破高低点时,历史K线的该条件会被重新计算为false,导致已显示的信号被移除,这就是重绘现象。

修改方案

通过固定记录信号触发的K线位置,让信号一旦生成就永久保留在对应K线上,不再随后续行情变化而消失。具体步骤:

  • 声明变量存储信号触发的K线索引
  • 当信号条件触发时,记录对应的K线位置
  • 修改plotchar的触发条件为「当前K线索引等于记录的信号K线索引」

修改后的完整代码

// © ChartPrime

//@version=5
indicator(title='Higher Timeframe High & Low [ChartPrime]', 
          shorttitle='H/L HTF [ChartPrime]', 
          overlay=true, 
          max_lines_count = 2, 
          max_labels_count = 10
          )

// ---------------------------------------------------------------------------------------------------------------------}
// 𝙄𝙉𝙋𝙐𝙏𝙎
// ---------------------------------------------------------------------------------------------------------------------{
extension  = input.int(50, "Extension to the right of High and Low", group = "Settings")
timeframe  = input.timeframe("D", "timeframe", tooltip = "Use Higher Timeframe then Current", group = "Settings")

separator  = input.bool(false, "Period Separator")
show_trend = input.bool(false, "Show Trend?")
show_break = input.bool(false, "Show Breakout Labels?")


// Variables Declaration
var color color = na
color color1    = na
var bool check  = na

color colorDn   = #d41919
color colorUp   = #13b818

// 新增:记录信号触发的K线索引,避免重绘
var int resistanceBar = na
var int supportBar = na


// ---------------------------------------------------------------------------------------------------------------------}
// 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
// ---------------------------------------------------------------------------------------------------------------------{
// To avoid differences on historical and realtime bars: 
// returns a value from the higher timeframe on the bar after it completes:
indexHighTF = barstate.isrealtime ? 1 : 0
indexCurrTF = barstate.isrealtime ? 0 : 1

pricehigh   = request.security(syminfo.tickerid, timeframe, high[indexHighTF])[indexCurrTF]
pricelow    = request.security(syminfo.tickerid, timeframe,  low[indexHighTF])[indexCurrTF]
price_avg   = math.avg(pricelow, pricehigh)

high_break = ta.crossover(close, pricehigh)
low_break  = ta.crossunder(close, pricelow)

// 新增:触发信号时记录对应K线索引(对应原offset=-1的目标K线)
if ta.crossunder(high, pricehigh)
    resistanceBar := bar_index - 1

if ta.crossover(low, pricelow)
    supportBar := bar_index - 1


// BreakOuts Checker (to avoid multiple labels) and color change
switch 
    high_break => check := true,  color1 := colorUp, color := colorUp
    low_break  => check := false, color1 := colorDn, color := colorDn


// ---------------------------------------------------------------------------------------------------------------------}
// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
// ---------------------------------------------------------------------------------------------------------------------{
// BreakOut labels
if high_break and check[1] == false and show_break
    
    label.new(x = bar_index, y = low*0.99, 
             text = timeframe + "\nHigh Break",
             style = label.style_label_up,
             textcolor = chart.fg_color, 
             color = #0e4e11, 
             size = size.small
             )

if low_break and check[1] == true and show_break

    label.new(x = bar_index, y = high*1.01, 
             text      = timeframe + "\nLow Break", 
             style     = label.style_label_down, 
             textcolor = chart.fg_color, 
             color     = #6e1414, 
             size      = size.small
             )

// High and Low plots from HTF
change_hl = not ta.change(pricehigh) or not ta.change(pricelow)


ph = plot(series    = change_hl and not show_trend ? pricehigh : show_trend ? pricehigh : na, 
          title     ='High', 
          style     = plot.style_linebr, 
          linewidth = 2, 
          color     = not show_trend ? colorDn : color.new(colorDn, 80)
          )

pm = plot(series   = change_hl and not show_trend ? price_avg : show_trend ? price_avg : na, 
         title     ='L & H Avg', 
         style     = show_trend ? plot.style_stepline_diamond : plot.style_circles, 
         linewidth = 1, 
         color     = show_trend ? color.new(chart.fg_color, 90) : color.new(chart.fg_color, 50)
         )

pl = plot(series    = change_hl and not show_trend ? pricelow  : show_trend ? pricelow : na,
          title     = 'Low', 
          style     = plot.style_linebr,  
          linewidth = 2, 
          color     = not show_trend ? colorUp : color.new(colorUp, 80)
          )


// 修改:使用记录的K线索引触发信号,避免重绘
plotchar(series   = bar_index == resistanceBar, 
         title    = "Resistance", 
         char     = "◆", 
         location = location.abovebar, 
         offset   = 0,  // 已通过bar_index-1定位目标K线,无需额外偏移
         color    = colorDn, 
         size     = size.tiny
         )

plotchar(series   = bar_index == supportBar, 
         title    = "Support",
         char     = "◆", 
         location = location.belowbar,
         offset   = 0,  // 同理修改偏移量
         color    = colorUp, 
         size     = size.tiny
         )


// Extension High and Low to a future
if barstate.islast

    label.new(point = chart.point.from_index(bar_index + extension, pricehigh), 

              text      = "⮪ " + timeframe + " High", 
              style     = label.style_label_left, 
              textcolor = chart.fg_color, 
              color     = colorDn, 
              size      = size.normal
              )

    label.new(point = chart.point.from_index(bar_index + extension, pricelow),

              text      = "⮨ " + timeframe + " Low",  
              style     = label.style_label_left, 
              textcolor = chart.fg_color, 
              color     = #158d19, 
              size      = size.normal
              )

if change_hl and barstate.islast

    h_Left         = chart.point.from_index(bar_index,           pricehigh)
    h_Right        = chart.point.from_index(bar_index+extension, pricehigh)

    line.new(h_Left, h_Right, color = colorDn, style = line.style_solid, width = 2)

    l_Left         = chart.point.from_index(bar_index,           pricelow)
    l_Right        = chart.point.from_index(bar_index+extension, pricelow)

    line.new(l_Left, l_Right, color = colorUp, width = 2)

    m_Left         = chart.point.from_index(bar_index,           math.avg(pricelow, pricehigh))
    m_Right        = chart.point.from_index(bar_index+extension, math.avg(pricelow, pricehigh))

    line.new(m_Left, m_Right, 
                 color = chart.fg_color, 
                 width = show_trend ? 1 : 2, 
                 style = show_trend ? line.style_solid : line.style_dotted
                 )


// BreakOut Bar Color of Trend Color
barcolor(show_trend ? color : color1)


bgcolor(separator ? (not change_hl
                      ? color.new(chart.fg_color, 90): na) 
                      : na
                      )


// Fill OB and OS Zones
fill(plot1        = ph,
     plot2        = pl,
     top_value    = pricehigh,
     bottom_value = pricelow, 
     top_color    = not show_trend ? color.new(colorDn, chart.bg_color == color.white ?  80 : 92) : na,
     bottom_color = not show_trend ? color.new(colorUp, chart.bg_color == color.white ?  80 : 92) : na
     )

    
// ---------------------------------------------------------------------------------------------------------------------}

关键修改说明

  1. 新增索引变量:用var关键字声明resistanceBar和supportBar,确保变量只初始化一次,不会随K线刷新重置,用于存储信号触发的K线位置。
  2. 记录信号位置:当原信号条件触发时,将对应目标K线的索引存入变量,匹配原指标的offset=-1显示逻辑。
  3. 固定信号触发条件:将plotchar的触发条件改为判断当前K线索引是否等于记录的信号索引,确保信号一旦生成就固定在对应K线上,不会因后续行情变化消失。
  4. 调整偏移量:因为已通过索引定位到目标K线,所以将offset改为0,避免重复偏移。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 03:27:32