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

Pine脚本FVG检测器优化:50%回补改色与盒子延长需求

优化后的FVG检测器Pine脚本实现
//@version=5
indicator("FVG Detector (Optimized)", overlay=true)

// 可配置参数
max_extend_bars = input.int(20, "最大延长K线数", minval=1, tooltip="FVG未完全回补时,盒子最多向后延长的K线数量")
color_fvg_up = input.color(color.new(color.green, 80), "未回补50%的向上FVG颜色")
color_fvg_down = input.color(color.new(color.red, 80), "未回补50%的向下FVG颜色")
color_mitigated = input.color(color.new(color.gray, 80), "回补50%后的FVG颜色")

// 定义FVG数据结构,存储每个缺口的关键信息
struct FVGInfo {
    box box_obj
    int start_bar
    float top
    float bottom
    float mid_point
    bool is_up_type
    bool is_full_mitigated
}

// 数组存储所有未完全回补的FVG,支持多缺口同时跟踪
var array<FVGInfo> fvg_array = array.new<FVGInfo>()

// FVG检测逻辑
is_fvg_up = ta.highest(high[2], 2) < low[0]  // 向上缺口:前两根K线最高 < 当前K线最低
is_fvg_down = ta.lowest(low[2], 2) > high[0] // 向下缺口:前两根K线最低 > 当前K线最高

// 生成新FVG标记
if is_fvg_up
    fvg_top = ta.highest(high[2], 2)
    fvg_bottom = low[0]
    new_box = box.new(left=bar_index[2], right=bar_index, top=fvg_top, bottom=fvg_bottom, bgcolor=color_fvg_up, border_width=0)
    array.push(fvg_array, FVGInfo.new(new_box, bar_index[2], fvg_top, fvg_bottom, (fvg_top+fvg_bottom)/2, true, false))

if is_fvg_down
    fvg_top = high[0]
    fvg_bottom = ta.lowest(low[2], 2)
    new_box = box.new(left=bar_index[2], right=bar_index, top=fvg_top, bottom=fvg_bottom, bgcolor=color_fvg_down, border_width=0)
    array.push(fvg_array, FVGInfo.new(new_box, bar_index[2], fvg_top, fvg_bottom, (fvg_top+fvg_bottom)/2, false, false))

// 遍历更新所有活跃FVG状态
var int i = 0
while i < array.size(fvg_array)
    fvg = array.get(fvg_array, i)
    
    // 检查是否完全回补
    if not fvg.is_full_mitigated
        fvg.is_full_mitigated := fvg.is_up_type ? (low <= fvg.top) : (high >= fvg.bottom)
        array.set(fvg_array, i, fvg)
    
    // 延长盒子:未完全回补且未超最大延长数时,同步到当前K线
    if not fvg.is_full_mitigated and (bar_index - fvg.start_bar) <= max_extend_bars
        box.set_right(fvg.box_obj, bar_index)
    
    // 50%回补判断与颜色切换
    is_half_mitigated = fvg.is_up_type ? (low <= fvg.mid_point) : (high >= fvg.mid_point)
    box.set_bgcolor(fvg.box_obj, is_half_mitigated ? color_mitigated : (fvg.is_up_type ? color_fvg_up : color_fvg_down))
    
    // 清理已过期的完全回补FVG,优化性能
    if fvg.is_full_mitigated and (bar_index - fvg.start_bar) > max_extend_bars
        box.delete(fvg.box_obj)
        array.remove(fvg_array, i)
    else
        i += 1

// 可选:显示当前未回补FVG数量
var label stat_label = label.new(bar_index, high, "", color=color.gray, textcolor=color.white, style=label.style_label_down)
label.set_text(stat_label, str.format("未回补FVG: %d", array.size(fvg_array)))
label.set_xy(stat_label, bar_index, ta.highest(high, 5))

功能优化说明

1. 50%回补动态变色

  • 实时跟踪每个FVG的价格走势,一旦价格触及缺口中间价(50%回补位),立即将标记盒切换为灰色
  • 未回补50%时,向上缺口保持绿色,向下缺口保持红色

2. 盒子自动延长至完全回补

  • 使用数组存储所有未回补的FVG,解决原代码仅能跟踪最新缺口的问题
  • 盒子会自动向后延长,直到缺口被完全回补,或达到设置的「最大延长K线数」后停止
  • 完全回补判断逻辑:
    • 向上缺口:价格跌破缺口顶部区间即视为完全回补
    • 向下缺口:价格涨破缺口底部区间即视为完全回补

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 17:34:52