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

求助:如何让Pine Script的FVG指标固定显示1D时间框架结果

FVG指标固定显示日线输出解决方案

我需要实现一个FVG指标,无论当前图表使用什么时间框架,都只显示日线(1D)级别的FVG信号。尝试过封装函数和固定时间框架的方法,但都没成功,以下是我的尝试代码:

原始代码

//@version=5
indicator("FVG", shorttitle="FVG", overlay = true, max_boxes_count = 500)
// string tfInput = input.timeframe("")

IsDeleteBoxes = input.bool(false, "Deleted Used boxes")

var box boxUp = na
var BoxesInformation = array.new_box(0)
var AllBoxes = box.all
if (low[0]>high[2])
    if (low[2]<low[1] and high>high[1])
        boxUp := box.new(bottom=high[2], left=bar_index[2], top=low[0], right=bar_index[0], bgcolor=color.rgb(43, 43, 43), extend = extend.right, border_color=color.rgb(131, 141, 141))
        AllBoxes := box.all
else
    if array.size(AllBoxes) > 0
        for i = 0 to array.size(AllBoxes) - 1
            if (box.get_top(array.get(AllBoxes, i))>low  and not array.includes(BoxesInformation, array.get(AllBoxes, i)))
                box.set_extend(array.get(AllBoxes, i),extend.none)
                box.set_right(array.get(AllBoxes, i),right=bar_index[0])
                box.set_right(array.get(AllBoxes, i),right=bar_index[0])
                box.set_bgcolor(array.get(AllBoxes, i),color.rgb(39, 43, 43))
                box.set_border_color(array.get(AllBoxes, i),color.rgb(0, 0, 0))
                array.push(BoxesInformation, array.get(AllBoxes, i) )
                if IsDeleteBoxes
                    box.delete(array.get(AllBoxes, i))

更新尝试代码(改用time替代bar_index)

IsDeleteBoxes = input.bool(false, "Deleted Used boxes")

string TimeFrame = input.timeframe("60")
[custom_open, custom_close, custom_high, custom_low, custom_time] = request.security(syminfo.tickerid, TimeFrame, [open, close, high, low, time])
custom_bar_index = request.security(syminfo.tickerid, TimeFrame, bar_index)

var box boxUp = na
var BoxesInformationUp = array.new_box(0)
var AllBoxesUp = box.all
if (custom_low[0]>custom_high[2])
    if (custom_low[2]<custom_low[1] and custom_high>custom_high[1])
        boxUp := box.new(bottom=custom_high[2], left=custom_time[2], top=custom_low[0], right=custom_time[0], bgcolor=color.rgb(43, 43, 43), extend = extend.right,xloc=xloc.bar_time,  border_color=color.rgb(131, 141, 141), text=TimeFrame,text_size=size.tiny,text_color=color.rgb(207, 195, 195))
        boxUp := box.new(bottom=high[2], left=custom_time[2], top=low[0], right=custom_time[0], bgcolor=color.rgb(43, 43, 43), extend = extend.right,xloc=xloc.bar_time, border_color=color.rgb(131, 141, 141))
        AllBoxesUp := box.all
else
    if array.size(AllBoxesUp) > 0
        for i = 0 to array.size(AllBoxesUp) - 1
            if (box.get_top(array.get(AllBoxesUp, i))>custom_low  and not array.includes(BoxesInformationUp, array.get(AllBoxesUp, i)))
                box.set_extend(array.get(AllBoxesUp, i),extend.none)
                box.set_right(array.get(AllBoxesUp, i),right=custom_time[0])
                box.set_bgcolor(array.get(AllBoxesUp, i),color.rgb(39, 43, 43))
                box.set_border_color(array.get(AllBoxesUp, i),color.rgb(0, 0, 0))
                array.push(BoxesInformationUp, array.get(AllBoxesUp, i) )
                if IsDeleteBoxes
                    box.delete(array.get(AllBoxesUp, i))

修正后的代码(固定显示日线FVG)

//@version=5
indicator("Daily FVG", shorttitle="Daily FVG", overlay = true, max_boxes_count = 500)

// 输入设置
IsDeleteBoxes = input.bool(false, "删除已触发的FVG盒子")
target_tf = "D" // 固定日线时间框架

// 获取日线级别核心数据
[dh, dl, dt] = request.security(syminfo.tickerid, target_tf, [high, low, time])

// 存储已创建的FVG盒子和已触发的盒子
var array<box> fvg_boxes = array.new_box()
var array<box> triggered_boxes = array.new_box()

// 识别日线级别的向上FVG缺口
fvg_condition = dl[0] > dh[2] and dl[2] < dl[1] and dh[0] > dh[1]

// 当识别到新FVG时创建盒子
if fvg_condition
    new_box = box.new(
        bottom = dh[2],
        left = dt[2],
        top = dl[0],
        right = dt[0],
        bgcolor = color.rgb(43, 43, 43),
        extend = extend.right,
        xloc = xloc.bar_time,
        border_color = color.rgb(131, 141, 141),
        text = "Daily FVG",
        text_size = size.tiny,
        text_color = color.rgb(207, 195, 195)
    )
    array.push(fvg_boxes, new_box)

// 仅在最后一根K线检查FVG触发状态,提升运行效率
if barstate.islast
    for i = 0 to array.size(fvg_boxes) - 1
        current_box = array.get(fvg_boxes, i)
        if not array.includes(triggered_boxes, current_box)
            box_top = box.get_top(current_box)
            // 用最新日线低点判断是否触发FVG
            if dl[0] < box_top
                // 更新触发后的盒子样式
                box.set_extend(current_box, extend.none)
                box.set_right(current_box, dt[0])
                box.set_bgcolor(current_box, color.rgb(39, 43, 43))
                box.set_border_color(current_box, color.rgb(0, 0, 0))
                array.push(triggered_boxes, current_box)
                // 开启删除选项时移除已触发盒子
                if IsDeleteBoxes
                    box.delete(current_box)

关键修正点

  • 直接固定目标时间框架为"D",避免输入错误
  • 仅获取日线核心数据(高低点、时间),减少冗余请求
  • 用数组管理盒子,避免box.all带来的全局盒子冲突
  • 仅在最后一根K线检查触发状态,提升指标运行效率
  • 统一使用xloc.bar_time定位盒子,确保跨时间框架显示准确
  • 移除重复创建盒子的逻辑,避免生成冗余盒子

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:15:34