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

TradingView PineScript:3M周期高低位水平线跨周期显示异常

解决PineScript跨周期显示3M高低位水平线问题

我想用TradingView的PineScript绘制指定数量的3M周期K线高低位水平线,要求在任意周期都能查看。当前脚本在3M周期下运行正常,但切换到更低周期时,部分支撑/阻力线无法显示。

现有代码

//@version=5
indicator("3M-HLINE", overlay=true)

// Define the timeframe
timeframe1 =input.timeframe('3M', "Timeframe")

// Calculate the number of bars to look back
numBarsBack1 =input.int(16,"BARS BACK")

// Arrays to store open and close values
var float[] openArr1 = array.new_float(numBarsBack1)
var float[] closeArr1 = array.new_float(numBarsBack1)

array.clear(openArr1)
array.clear(closeArr1)

baropen1 = request.security(syminfo.tickerid, timeframe1, high)
barclose1 = request.security(syminfo.tickerid, timeframe1, low)

// Accumulate 3M open/close values in arrays
for i = 1 to numBarsBack1
    array.unshift(openArr1, baropen1[i])
    array.unshift(closeArr1, barclose1[i])

// Plot the 3M open/close values as lines
for i = 0 to numBarsBack1 -1
    line.new(x1=bar_index - 1, y1=array.get(openArr1, i), x2=bar_index + 1, y2=array.get(openArr1, i), color=color.new(color.rgb(135, 206, 250), 0), width=2, extend=extend.both)
    line.new(x1=bar_index - 1, y1=array.get(closeArr1, i), x2=bar_index + 1, y2=array.get(closeArr1, i), color=color.new(color.rgb(135, 206, 250), 0), width=2, extend=extend.both)

已尝试的无效方案

  • 将request.security放入for循环,脚本不允许该操作
  • 使用hline绘制线条,无法适配浮点数组

解决方案

问题核心在于原代码在低周期下无法正确获取完整的3M周期历史高低点,且每次循环清空数组会丢失历史数据。修改后的代码通过检测目标周期新K线来更新数据,并用var数组保留历史值,确保跨周期显示所有线条:

//@version=5
indicator("3M高低位水平线", overlay=true)

// 输入参数
target_tf = input.timeframe("3M", "目标周期")
lookback_bars = input.int(16, "回溯K线数量")

// 存储3M周期高低点的数组,用var保持状态不丢失
var float[] high_levels = array.new_float()
var float[] low_levels = array.new_float()

// 检测目标周期是否出现新K线
is_new_target_bar = ta.change(time(target_tf))

// 仅在目标周期新K线出现时更新高低点数组
if is_new_target_bar
    // 获取当前目标周期的高低点
    current_high = request.security(syminfo.tickerid, target_tf, high)
    current_low = request.security(syminfo.tickerid, target_tf, low)
    
    // 将新高低点加入数组头部
    array.unshift(high_levels, current_high)
    array.unshift(low_levels, current_low)
    
    // 控制数组长度,只保留设定的回溯数量
    if array.size(high_levels) > lookback_bars
        array.pop(high_levels)
    if array.size(low_levels) > lookback_bars
        array.pop(low_levels)

// 绘制所有存储的高低位水平线
for i = 0 to array.size(high_levels) - 1
    high_val = array.get(high_levels, i)
    line.new(bar_index - 1, high_val, bar_index + 1, high_val, 
             color=color.new(color.rgb(135, 206, 250), 0), width=2, extend=extend.both)
    
    low_val = array.get(low_levels, i)
    line.new(bar_index - 1, low_val, bar_index + 1, low_val, 
             color=color.new(color.rgb(135, 206, 250), 0), width=2, extend=extend.both)

修改说明

  1. 用ta.change(time(target_tf))精准检测3M周期的新K线,避免无效计算
  2. var声明的数组会保留历史数据,解决低周期下数据丢失问题
  3. 控制数组长度,确保只保留指定数量的历史高低点
  4. 直接从数组读取存储的历史值绘制线条,跨周期下所有支撑/阻力线都能正常显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 03:59:54