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

Pine Script求助:绘制最新K线高点及近两根K线最低低点水平线

Pine Script 实现方案

以下是满足你需求的代码,基于Pine Script v5编写:

//@version=5
indicator("Latest High & Lowest Low Lines", overlay=true)

// 获取所需价格数据
latest_high = high[0]  // 最新K线的高点
lowest_low = math.min(low[0], low[1])  // 最新K线与前一根K线低点的最小值

// 创建并更新高点水平线及价格标签
var line high_line = na
if barstate.islast
    high_line := line.new(bar_index, latest_high, bar_index + 100, latest_high, color=color.new(color.green, 0), width=2)
    label.new(bar_index + 5, latest_high, str.tostring(latest_high, "#.####"), color=color.new(color.green, 0), textcolor=color.white, style=label.style_label_left)

// 创建并更新最低低点水平线及价格标签
var line low_line = na
if barstate.islast
    low_line := line.new(bar_index, lowest_low, bar_index + 100, lowest_low, color=color.new(color.red, 0), width=2)
    label.new(bar_index + 5, lowest_low, str.tostring(lowest_low, "#.####"), color=color.new(color.red, 0), textcolor=color.white, style=label.style_label_left)

代码说明:

  • //@version=5:指定使用Pine Script第5版本,确保语法兼容性
  • indicator(..., overlay=true):将指标叠加在主图上显示
  • latest_high = high[0]:直接获取当前最新K线的高点价格
  • lowest_low = math.min(low[0], low[1]):用math.min()函数取最新K线(low[0])和前一根K线(low[1])低点的最小值
  • var line:使用var关键字确保只创建一次线条对象,避免每根K线重复生成
  • barstate.islast:仅在最新K线时执行线条和标签的创建/更新,提升性能
  • line.new():创建水平线,参数依次为起点X、起点Y、终点X、终点Y、颜色、宽度
  • label.new():在线条右侧添加价格标签,str.tostring()用于格式化价格显示精度,style=label.style_label_left让标签左对齐指向线条

你可以直接将这段代码复制到TradingView的Pine编辑器中运行,根据需要调整颜色、宽度或价格显示精度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 03:30:52