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

如何在Pine Script中仅绘制首次交叉触发的水平线

修正后的Pine Script代码(满足当日首次触发绘制需求)

核心修改点:

  • 新增交易日判断逻辑,每日自动重置触发状态
  • 锁定当日首次突破的价格,忽略同一交易日内的后续触发
  • 通过plot()实现水平线从触发点持续显示至当日收盘,次日自动停止
//@version=5
indicator(title="Crossover", shorttitle="Crossover", overlay=true)

//*5Mins High Low calculation*//
inputMax = input(5, title= "ORB total time (minutes)")
sess = input("0915-0920", title="Session Time") 
t = time(timeframe.period, sess + ":1234567")
hide = timeframe.isintraday and timeframe.multiplier <= inputMax
in_session = not na(t)
is_first = in_session and not in_session[1]

// ORB高低点计算逻辑保留
orb_high = float(na)
orb_low = float(na)
if is_first
    orb_high := high
    orb_low := low
else
    orb_high := orb_high[1]
    orb_low := orb_low[1]
if high > orb_high and in_session
    orb_high := high
if low < orb_low and in_session
    orb_low := low

plot(hide ? orb_high : na , style=plot.style_line, color=orb_high[1] != orb_high ? na : color.green , title="ORB High", linewidth=3)
plot(hide ? orb_low : na , style=plot.style_line, color=orb_low[1] != orb_low ? na : color.red, title="ORB Low", linewidth=3)

//*Crossover condition - 修正逻辑*//
Up = orb_high + 30

// 初始化当日触发状态和首次触发价格
var bool is_triggered_today = false
var float first_trigger_price = na

// 每日开盘重置触发状态
if time.newday()
    is_triggered_today := false
    first_trigger_price := na

// 仅在未触发过的情况下,记录首次突破价格
cross_over_happened = ta.crossover(close, Up) and barstate.isconfirmed
if cross_over_happened and not is_triggered_today
    is_triggered_today := true
    first_trigger_price := close  // 记录首次突破的收盘价作为水平线价格

// 绘制水平线:仅在当日已触发且未进入下一个交易日时显示
plot(is_triggered_today and not time.newday() ? first_trigger_price : na, color=color.black, linewidth=2, title="First Breakout Level")

关键逻辑说明

  1. 交易日重置:通过time.newday()识别新交易日,自动重置触发标记和价格记录,确保每日重新判断首次突破。
  2. 首次触发锁定:仅在is_triggered_today为false时,才会记录突破价格并标记当日已触发,同一交易日内的后续突破会被直接忽略。
  3. plot()显示控制:仅在当日已触发且未进入下一个交易日的时段内,绘制首次记录的价格,其余时段返回na,实现水平线从触发点持续到当日收盘的效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:30:56