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

如何在Pinescript中绘制表格内的日内支撑阻力位?

解决Pinescript绘制大量日内支撑阻力位的方案

核心思路

放弃受64个限制且无法在局部作用域调用的plot()函数,改用line.new()绘制水平线段——它支持在局部逻辑中创建,单脚本最多允许500条,足够覆盖大多数日内回测场景。

实现步骤

  1. 存储支撑阻力数据:用全局数组按日期顺序存储目标日期、支撑位、阻力位。
  2. 匹配当日数据:在每个K线bar上检查当前日期是否在数据列表中。
  3. 绘制日内水平线:找到对应数据后,在当日开盘到收盘的时间范围内绘制支撑/阻力线。

完整代码示例

//@version=5
indicator("日内支撑阻力位绘制", overlay=true)

// 1. 初始化全局数组存储数据
var int[] targetDates = array.new_int()       // 目标日期(timestamp格式)
var float[] supportLevels = array.new_float() // 对应日期的支撑位
var float[] resistanceLevels = array.new_float() // 对应日期的阻力位

// 2. 填充你的支撑阻力数据(替换成自己的表格内容,按日期排序)
if barstate.islast
    // 示例数据:日期、支撑、阻力
    array.push(targetDates, timestamp("2024-05-20"))
    array.push(supportLevels, 1.0800)
    array.push(resistanceLevels, 1.0950)
    
    array.push(targetDates, timestamp("2024-05-21"))
    array.push(supportLevels, 1.0780)
    array.push(resistanceLevels, 1.0920)
    
    array.push(targetDates, timestamp("2024-05-22"))
    array.push(supportLevels, 1.0820)
    array.push(resistanceLevels, 1.0980)
    // 继续添加更多数据...

// 3. 获取当前日期的timestamp(仅保留年月日部分)
currentDate = timestamp(year, month, dayofmonth, 0, 0, 0)

// 4. 遍历数组,匹配当前日期并绘制线条
for i = 0 to array.size(targetDates) - 1
    targetDate = array.get(targetDates, i)
    if targetDate == currentDate
        // 获取当日支撑/阻力位
        support = array.get(supportLevels, i)
        resistance = array.get(resistanceLevels, i)
        
        // 获取当日开盘和收盘的时间戳(适配当前交易时段)
        sessionStart = timestamp(year, month, dayofmonth, session.tonumber(session.get("start")), 0, 0)
        sessionEnd = timestamp(year, month, dayofmonth, session.tonumber(session.get("end")), 0, 0)
        
        // 绘制支撑线(绿色,虚线)
        line.new(sessionStart, support, sessionEnd, support, color=color.new(color.green, 0), width=2, style=line.style_dashed)
        // 绘制阻力线(红色,虚线)
        line.new(sessionStart, resistance, sessionEnd, resistance, color=color.new(color.red, 0), width=2, style=line.style_dashed)
        
        // 标注价位文本(可选)
        label.new(sessionStart, support, str.tostring(support), color=color.green, textcolor=color.white, style=label.style_label_right)
        label.new(sessionStart, resistance, str.tostring(resistance), color=color.red, textcolor=color.white, style=label.style_label_right)

关键细节说明

  • 日期匹配:通过timestamp(year, month, dayofmonth, 0, 0, 0)提取当前K线的年月日,确保只匹配当日的数据。
  • 时段适配:用session.get()自动获取当前图表的交易时段,确保线条覆盖完整的日内交易时间。
  • 性能优化:在barstate.islast时填充数据,避免每次bar更新都执行数组写入操作。
  • 线条限制:如果支撑阻力位超过500条,可以合并重复价位,或者分多个脚本绘制(TradingView允许同时加载多个脚本)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 16:43:13