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

如何在Pine Script中手绘自由线并计算价格与支撑阻力线的百分比距离

在Pine Script中导入手动绘制的支撑/阻力线并计算价格距离百分比

核心思路

你需要先导出手动绘制的支撑/阻力线坐标,再在Pine Script中重建这些线,最后基于当前价格计算百分比距离。以下分两种常见场景给出实现方案:


场景1:水平线支撑/阻力

如果你的支撑/阻力是水平线,只需提取线的固定价格即可:

//@version=5
indicator("水平线支撑阻力距离计算器", overlay=true)

// 替换为你手动导出的支撑/阻力价格
support_price = 1800.0
resistance_price = 2000.0

// 计算百分比距离
support_distance = ((close - support_price) / support_price) * 100
resistance_distance = ((resistance_price - close) / close) * 100

// 绘制支撑/阻力线
line.new(bar_index - 100, support_price, bar_index + 100, support_price, color=color.green, width=2)
line.new(bar_index - 100, resistance_price, bar_index + 100, resistance_price, color=color.red, width=2)

// 实时显示距离
var label support_label = label.new(bar_index, support_price, "", color=color.green, style=label.style_label_up)
var label resistance_label = label.new(bar_index, resistance_price, "", color=color.red, style=label.style_label_down)

label.set_text(support_label, str.format("支撑距离: %.2f%%", support_distance))
label.set_text(resistance_label, str.format("阻力距离: %.2f%%", resistance_distance))
label.set_x(support_label, bar_index)
label.set_x(resistance_label, bar_index)

场景2:斜线支撑/阻力

如果是随时间变化的斜线,需要提取线的两个端点坐标(时间戳+价格),计算斜率后得到当前时间对应的线价格:

//@version=5
indicator("斜线支撑阻力距离计算器", overlay=true)

// 替换为你手动导出的支撑线两个端点坐标
support_point1_time = timestamp(2024, 1, 1, 0, 0)
support_point1_price = 1750.0
support_point2_time = timestamp(2024, 2, 1, 0, 0)
support_point2_price = 1800.0

// 计算支撑线斜率与当前价格
slope_support = (support_point2_price - support_point1_price) / (support_point2_time - support_point1_time)
current_support_price = support_point1_price + slope_support * (time - support_point1_time)

// 替换为你手动导出的阻力线两个端点坐标
resistance_point1_time = timestamp(2024, 1, 10, 0, 0)
resistance_point1_price = 2050.0
resistance_point2_time = timestamp(2024, 2, 10, 0, 0)
resistance_point2_price = 2000.0

// 计算阻力线斜率与当前价格
slope_resistance = (resistance_point2_price - resistance_point1_price) / (resistance_point2_time - resistance_point1_time)
current_resistance_price = resistance_point1_price + slope_resistance * (time - resistance_point1_time)

// 计算百分比距离
support_distance = ((close - current_support_price) / current_support_price) * 100
resistance_distance = ((current_resistance_price - close) / close) * 100

// 绘制斜线
line.new(support_point1_time, support_point1_price, support_point2_time, support_point2_price, color=color.green, width=2)
line.new(resistance_point1_time, resistance_point1_price, resistance_point2_time, resistance_point2_price, color=color.red, width=2)

// 实时显示距离
var label support_label = label.new(bar_index, current_support_price, "", color=color.green, style=label.style_label_up)
var label resistance_label = label.new(bar_index, current_resistance_price, "", color=color.red, style=label.style_label_down)

label.set_text(support_label, str.format("支撑距离: %.2f%%", support_distance))
label.set_text(resistance_label, str.format("阻力距离: %.2f%%", resistance_distance))
label.set_x(support_label, bar_index)
label.set_y(support_label, current_support_price)
label.set_x(resistance_label, bar_index)
label.set_y(resistance_label, current_resistance_price)

操作步骤

  1. 导出手动线数据:在TradingView右键点击你绘制的线,选择「导出数据」,保存CSV文件,里面包含线的端点时间戳和价格。
  2. 替换代码变量:将示例中的价格、时间戳替换为CSV里的实际数值。
  3. 运行脚本:把代码粘贴到Pine Script编辑器,添加到图表即可看到实时的百分比距离。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 18:22:27