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

如何在Pine Script低时间框架中绘制上月最高价水平线

Pine Script 实现低时间框架绘制上月起始最高价水平线

要实现从上个月第一根K线开始,水平绘制上月最高价的需求,你需要先调取月线级别的上月数据,再通过line.new方法绘制固定路径的水平线,核心是避免重复生成线条,具体实现如下:

核心实现步骤

  • 用request.security接口调取月线维度的两个关键值:上一个完整月的最高价、上一个完整月第一根K线的时间戳
  • 仅在当月第一根K线触发线条创建逻辑,避免每个K线重复生成新线条占用资源
  • 可按需配置是否让线条自动延伸到当前最新K线位置

基础版本代码(固定长度)

//@version=5
indicator("上月最高价水平线", overlay=true)

// 调取月线级别的上月最高价、上月首根K线时间
[prev_month_high, prev_month_first_time] = request.security(syminfo.tickerid, "M", [high[1], time[1]])

// 仅在每月第一个K线创建一次线条
if ta.change(time("M"))
    line.new(
        x1=prev_month_first_time, 
        y1=prev_month_high, 
        x2=time, 
        y2=prev_month_high, 
        xloc=xloc.bar_time, 
        color=color.new(color.blue, 0), 
        width=2,
        style=line.style_solid
    )

进阶版本代码(自动延伸到最新K线)

如果需要水平线一直延伸到当前图表的最新K线位置,可通过var定义全局线条变量,实时更新线条右端点:

//@version=5
indicator("上月最高价水平线(自动延伸)", overlay=true)

// 定义全局变量存储线条对象,避免重复创建
var line prev_high_line = na
// 调取月线维度数据
[prev_month_high, prev_month_first_time] = request.security(syminfo.tickerid, "M", [high[1], time[1]])

// 每月初重置线条
if ta.change(time("M"))
    prev_high_line := line.new(
        x1=prev_month_first_time, 
        y1=prev_month_high, 
        x2=time, 
        y2=prev_month_high, 
        xloc=xloc.bar_time, 
        color=color.blue, 
        width=2
    )

// 每个K线更新线条右端点到当前位置
line.set_x2(prev_high_line, time)

参数说明

  • request.security第二个参数传入"M"代表取月线级别数据,high[1]对应上一个完整自然月的最高价格,time[1]对应上一个月第一根K线的时间戳
  • ta.change(time("M"))的判断逻辑会在每个月第一根K线时触发,确保每个月只生成一条新的水平线

内容的提问来源于stack exchange,提问作者n.y

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 19:15:05