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

Pine Script指标:如何将支撑阻力价格移至价格轴并过滤显示?

TradingView Pine Script v5 支撑阻力指标优化

需求概述

  • 原指标规则:绿柱高点遇后续红柱生成阻力线,红柱低点遇后续绿柱生成支撑线
  • 需将价格标签从图表区域移至价格轴显示
  • 仅保留当前价格下方的绿色支撑位、上方的红色阻力位,降低图表噪音

修改后的完整代码

//@version=5
indicator('LVLES', shorttitle='S/R Lines with Price Labels', overlay=true)

// 支撑位相关变量
var float supportPrice = na
var line supportLine = na
var bool greenCandleSupport = na
var label supportLabel = na

// 阻力位相关变量
var float resistancePrice = na
var line resistanceLine = na
var bool redCandleResistance = na
var label resistanceLabel = na

// 红柱(阴线)触发支撑位记录
if close < open
    supportPrice := low
    greenCandleSupport := false
    // 删除旧支撑线和标签
    if not na(supportLine)
        line.delete(supportLine)
    if not na(supportLabel)
        label.delete(supportLabel)

// 后续绿柱(阳线)生成支撑线
if not na(close[1]) and close[1] < open[1] and close > open
    greenCandleSupport := true
    // 仅显示当前价格下方的支撑位
    if supportPrice < close
        supportLine := line.new(x1=bar_index[1], y1=supportPrice, x2=bar_index, y2=supportPrice, color=color.green, width=1, extend=extend.right)
        // 价格轴标签:x值设为当前K线左侧偏移,对齐价格轴
        supportLabel := label.new(x=bar_index - 10, y=supportPrice, text=str.tostring(supportPrice, "#.####"), color=color.green, style=label.style_label_left, yloc=yloc.price)

// 绿柱(阳线)触发阻力位记录
if close > open
    resistancePrice := high
    redCandleResistance := false
    // 删除旧阻力线和标签
    if not na(resistanceLine)
        line.delete(resistanceLine)
    if not na(resistanceLabel)
        label.delete(resistanceLabel)

// 后续红柱(阴线)生成阻力线
if not na(close[1]) and close[1] > open[1] and close < open
    redCandleResistance := true
    // 仅显示当前价格上方的阻力位
    if resistancePrice > close
        resistanceLine := line.new(x1=bar_index[1], y1=resistancePrice, x2=bar_index, y2=resistancePrice, color=color.red, width=1, extend=extend.right)
        // 价格轴标签:x值设为当前K线左侧偏移,对齐价格轴
        resistanceLabel := label.new(x=bar_index - 10, y=resistancePrice, text=str.tostring(resistancePrice, "#.####"), color=color.red, style=label.style_label_left, yloc=yloc.price)

关键修改说明

  1. 价格轴标签实现

    • 调整label.new的x参数为bar_index - 10(可根据图表宽度自行调整偏移量),让标签固定显示在价格轴附近
    • 设置yloc=yloc.price确保标签严格对齐对应价格刻度
    • 使用style=label.style_label_left让标签文本朝右侧指向价格线
  2. 过滤逻辑实现

    • 生成支撑线时增加判断supportPrice < close,只保留当前价格下方的有效支撑
    • 生成阻力线时增加判断resistancePrice > close,只保留当前价格上方的有效阻力
    • 每次触发新的支撑/阻力记录时,自动删除旧的线条和标签,避免冗余显示
  3. 规则修正与变量优化

    • 修正原代码中阻力位仅按月更新的逻辑,严格遵循「绿柱高点遇后续红柱生成阻力线」的规则
    • 新增标签变量统一管理,确保线条和标签同步创建/删除

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:22:34