ICT First Presented & Reflection FVG [TradeSymmetry]

TradeSymmetry · study · 609 行 · 点赞 32 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// © TradeSymmetry
// ====================================================================================================================
// TradeSymmetry | First Presented & Reflection Fair Value Gap Engine (1st FVG & RFVG)
//
// Overview:
// This institutional indicator automatically identifies, tracks, and displays:
// 1. First Presented Fair Value Gap (1st FVG):
//    The initial liquidity imbalance (BISI or SIBI) established upon session opening.
// 2. First Presented Reflection Fair Value Gap (1st RFVG):
//    The critical opposite imbalance forming immediately following the initial bias.
//    - If the 1st Presented FVG is a BISI (Bullish), the Reflection FVG is the subsequent first SIBI (Bearish).
//    - If the 1st Presented FVG is a SIBI (Bearish), the Reflection FVG is the subsequent first BISI (Bullish).
// 3. Consequent Encroachment (C.E. 50% Midline):
//    Calculates and displays the exact 50% equilibrium level of both imbalances with reference labels.
// 4. Session Open Price Anchor:
//    Dynamic horizontal benchmark tracking opening price dynamics.
// 5. Configurable Session Modes & Forward Projection:
//    Keeps zones strictly session-specific by default, or optionally extends forward across multi-day profiles.
// ====================================================================================================================
 
indicator("ICT First Presented & Reflection FVG [TradeSymmetry]", shorttitle="ICT RFVG", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
 
// --------------------------------------------------------------------------------------------------------------------}
// 1. SESSION INPUTS
// --------------------------------------------------------------------------------------------------------------------{
grp_sess        = "1. Session Window"
session_preset  = input.string("09:30 - 11:00 (NY Morning / RTH Open)", "Session Window Preset", options=["09:30 - 11:00 (NY Morning / RTH Open)", "10:00 - 11:00 (NY AM Silver Bullet)", "09:30 - 16:00 (Full NY RTH Session)", "03:00 - 04:00 (London Silver Bullet)", "02:00 - 05:00 (London Open)", "14:00 - 15:00 (NY PM Silver Bullet)", "Custom Session"], group=grp_sess, tooltip="Select the trading session to anchor the first presented and reflection FVGs. Default is 09:30-11:00 NY Morning RTH.")
custom_sess_str = input.string("0930-1100", "Custom Session String (HHMM-HHMM)", group=grp_sess, tooltip="Used when 'Custom Session' is selected above.")
session_tz      = input.string("America/New_York", "Timezone", options=["America/New_York", "UTC", "Exchange", "Europe/London", "Asia/Tokyo"], group=grp_sess, tooltip="ICT concepts strictly anchor to New York local time (America/New_York).")
 
show_v_line     = input.bool(true, "Show Session Open Vertical Separator", group=grp_sess)
v_line_color    = input.color(color.new(color.white, 70), "Vertical Separator Color", group=grp_sess)
 
show_open_line  = input.bool(true, "Show Session Open Price Line", group=grp_sess, tooltip="Plots horizontal line at the session open price (e.g. 09:30 Opening Price).")
open_line_col   = input.color(color.new(#ffeb3b, 40), "Session Open Line Color", group=grp_sess)
open_line_style = input.string("Dotted", "Session Open Line Style", options=["Solid", "Dashed", "Dotted"], group=grp_sess)
 
// --------------------------------------------------------------------------------------------------------------------}
// 2. FVG DETECTION RULES
// --------------------------------------------------------------------------------------------------------------------{
grp_rules       = "2. FVG Detection Rules"
disp_mode       = input.string("Any FVG (Standard)", "Displacement Requirement", options=["Any FVG (Standard)", "Require Displacement (Swing H/L Sweep)"], group=grp_rules, tooltip="Any FVG (Standard): Detects the first presented FVG. Require Displacement: Requires the impulse candle to sweep recent swing highs or swing lows.")
disp_lookback   = input.int(5, "Displacement Swing Lookback (Bars)", minval=2, maxval=50, group=grp_rules, tooltip="Number of prior bars used to check if the impulse candle swept a swing high or swing low.")
min_gap_ticks   = input.float(1.0, "Minimum Gap Size (Ticks)", minval=0.0, step=0.5, group=grp_rules, tooltip="Filter out micro noise gaps below this tick size. Set to 0 to capture all gaps.")
 
mitig_source    = input.string("Wick", "Mitigation Detection Source", options=["Wick", "Close"], group=grp_rules, tooltip="Wick: Price touches the FVG boundary. Close: Candle close penetrates the FVG boundary.")
mitig_action    = input.string("Keep Active", "Mitigation Behavior", options=["Keep Active", "Dim Color On Mitigation", "Stop At Mitigation Bar"], group=grp_rules, tooltip="Keep Active: Retains the full zone and CE line. Dim: Lowers opacity when touched. Stop: Cuts off at the mitigation bar.")
 
// --------------------------------------------------------------------------------------------------------------------}
// 3. FIRST PRESENTED FVG (1ST FVG) VISUALS
// --------------------------------------------------------------------------------------------------------------------{
grp_first        = "3. First Presented FVG (1st FVG)"
show_first_fvg   = input.bool(true, "Show 1st Presented FVG", group=grp_first)
first_bull_col   = input.color(color.new(#089981, 75), "1st FVG Bullish Color (BISI)", group=grp_first)
first_bear_col   = input.color(color.new(#f23645, 75), "1st FVG Bearish Color (SIBI)", group=grp_first)
first_border_col = input.color(color.new(#787b86, 60), "1st FVG Border Color", group=grp_first)
 
show_first_ce    = input.bool(true, "Show 1st FVG C.E. (50% Midline)", group=grp_first)
first_ce_col     = input.color(#2962ff, "1st FVG C.E. Line Color", group=grp_first)
first_ce_style   = input.string("Dashed", "1st FVG C.E. Line Style", options=["Solid", "Dashed", "Dotted"], group=grp_first)
show_first_lbl   = input.bool(true, "Show 1st FVG Label", group=grp_first)
 
// --------------------------------------------------------------------------------------------------------------------}
// 4. FIRST PRESENTED REFLECTION FVG (RFVG) VISUALS
// --------------------------------------------------------------------------------------------------------------------{
grp_refl           = "4. First Presented Reflection FVG (RFVG)"
show_refl_fvg      = input.bool(true, "Show 1st Reflection FVG", group=grp_refl, tooltip="Plots the immediate opposing FVG that forms after the first presented FVG.")
refl_search_window = input.string("Within Session Only", "Reflection Search Window", options=["Within Session Only", "Extend Search to End of Day (16:00 NY)"], group=grp_refl, tooltip="Choose whether the Reflection FVG must form strictly inside the session window or can form anytime later in the trading day.")
 
refl_bull_col      = input.color(color.new(#00bcd4, 65), "Reflection Bullish Color (BISI)", group=grp_refl, tooltip="Opposing BISI formed after an initial bearish SIBI.")
refl_bear_col      = input.color(color.new(#ff9800, 65), "Reflection Bearish Color (SIBI)", group=grp_refl, tooltip="Opposing SIBI formed after an initial bullish BISI.")
refl_border_col    = input.color(color.new(color.white, 50), "Reflection Border Color", group=grp_refl)
 
show_refl_ce       = input.bool(true, "Show Reflection C.E. (50% Midline)", group=grp_refl)
refl_ce_col        = input.color(#ffeb3b, "Reflection C.E. Line Color", group=grp_refl)
refl_ce_style      = input.string("Dotted", "Reflection C.E. Line Style", options=["Solid", "Dashed", "Dotted"], group=grp_refl)
show_refl_lbl      = input.bool(true, "Show Reflection FVG Label", group=grp_refl)
 
// --------------------------------------------------------------------------------------------------------------------}
// 5. HISTORICAL DAYS & EXTENSION SETTINGS
// --------------------------------------------------------------------------------------------------------------------{
grp_hist        = "5. Historical Sessions & Forward Projection"
show_historical = input.bool(true, "Show Historical Days", group=grp_hist, tooltip="Display 1st Presented and Reflection FVGs from previous days.")
history_days    = input.int(5, "Lookback Days", minval=1, maxval=365, group=grp_hist, tooltip="Number of past calendar days of historical levels to display.")
ext_mode        = input.string("Extend to Current Bar", "Level Extension Mode", options=["Session Specific (No Extension)", "Extend to Current Bar", "Extend Infinitely (Right)"], group=grp_hist, tooltip="Session Specific (Default): Zones stay strictly contained within their respective session. Extend to Current Bar: Projects historical levels forward to the live chart bar.")
hist_dim_pct    = input.int(15, "Historical Dimming / Transparency Add %", minval=0, maxval=50, step=5, group=grp_hist, tooltip="Adds additional transparency to historical boxes so current session remains prominent.")
 
// --------------------------------------------------------------------------------------------------------------------}
// 6. LABELS & HUD STATUS SETTINGS
// --------------------------------------------------------------------------------------------------------------------{
grp_lbl          = "6. Label & Display Settings"
label_appearance = input.string("Badge / Tag (Filled)", "Label Appearance", options=["Clean Text (No Box)", "Badge / Tag (Filled)"], group=grp_lbl, tooltip="Clean Text (Default): Sharp, frameless text floating on the chart, identical to professional chart annotations. Badge: Filled label box.")
fvg_lbl_format   = input.string("Day + Date (e.g. Fri 09/04 1st P. FVG)", "1st FVG Label Format", options=["Day + Date (e.g. Fri 09/04 1st P. FVG)", "Day Only (e.g. Fri 1st P. FVG)", "Date Only (e.g. 09/04 1st P. FVG)", "Short (1st P. FVG)"], group=grp_lbl, tooltip="Controls the text prefix on the first presented FVG.")
refl_lbl_format  = input.string("Day Only (e.g. Fri 1st P. Reflection FVG)", "Reflection Label Format", options=["Day Only (e.g. Fri 1st P. Reflection FVG)", "Day + Date (e.g. Fri 09/04 1st P. Reflection FVG)", "Short (1st P. Reflection FVG)"], group=grp_lbl, tooltip="Controls the text prefix on the reflection FVG.")
use_bullet       = input.bool(false, "Include Bullet Point (•)", group=grp_lbl, tooltip="Adds '•' bullet point to labels, mirroring chart annotations: '• Fri 09/04 1st P. FVG'")
show_ce_line_lbl = input.bool(false, "Show '0.5' Midline Label", group=grp_lbl, tooltip="Displays the '0.5' label directly at the consequent encroachment midline.")
show_ce_price    = input.bool(true, "Include Exact Price in 0.5 Label", group=grp_lbl, tooltip="Displays '0.5 (29,674.00) •' with exact price, matching chart annotations.")
show_prices_lbl  = input.bool(false, "Include High/Low Price Range in FVG Label", group=grp_lbl, tooltip="Appends [Low - High] price range to FVG label.")
 
label_color_mode = input.string("Chart Auto (Theme Adaptive)", "Label Text Color Mode", options=["Chart Auto (Theme Adaptive)", "Zone Matching Color", "White", "Dark / Charcoal", "Custom Color"], group=grp_lbl, tooltip="Chart Auto: Automatically matches dark or light mode. Zone Matching: Inherits the color of the zone.")
custom_lbl_col   = input.color(color.white, "Custom Label Text Color", group=grp_lbl)
label_size_opt   = input.string("Normal", "Label Size", options=["Auto", "Tiny", "Small", "Normal", "Large"], group=grp_lbl)
label_x_offset   = input.int(2, "Label Right Offset (Bars)", minval=0, maxval=50, group=grp_lbl)
show_hud_table   = input.bool(true, "Show Session Status HUD Table", group=grp_lbl, tooltip="Displays a compact dashboard in the top-right showing active session status and detected FVGs.")
 
// --------------------------------------------------------------------------------------------------------------------}
// HELPER FUNCTIONS & STYLING MAPPERS
// --------------------------------------------------------------------------------------------------------------------{
get_line_style(style_str) =>
    switch style_str
        "Solid"  => line.style_solid
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        => line.style_solid
 
get_label_size(size_str) =>
    switch size_str
        "Auto"   => size.auto
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        => size.tiny
 
get_label_style(app_str) =>
    switch app_str
        "Clean Text (No Box)"  => label.style_none
        "Badge / Tag (Filled)" => label.style_label_left
        => label.style_none
 
get_day_formatted(t, tz) =>
    abbr = str.format_time(t, "E", tz)
    switch abbr
        "Thu" => "Thurs"
        "Tue" => "Tues"
        => abbr
 
active_session_str = switch session_preset
    "09:30 - 11:00 (NY Morning / RTH Open)" => "0930-1100"
    "10:00 - 11:00 (NY AM Silver Bullet)"   => "1000-1100"
    "09:30 - 16:00 (Full NY RTH Session)"   => "0930-1600"
    "03:00 - 04:00 (London Silver Bullet)"  => "0300-0400"
    "02:00 - 05:00 (London Open)"           => "0200-0500"
    "14:00 - 15:00 (NY PM Silver Bullet)"   => "1400-1500"
    => custom_sess_str
 
session_time  = time(timeframe.period, active_session_str, session_tz)
in_session    = not na(session_time)
is_new_sess   = in_session and not in_session[1]
in_day_window = not na(time(timeframe.period, "0930-1600", session_tz))
 
// --------------------------------------------------------------------------------------------------------------------}
// FVG CALCULATION & DETECTION
// --------------------------------------------------------------------------------------------------------------------{
min_tick = syminfo.mintick
 
bull_gap_size = low - high[2]
raw_bull_fvg  = low > high[2] and bull_gap_size >= min_gap_ticks * min_tick
 
bear_gap_size = low[2] - high
raw_bear_fvg  = high < low[2] and bear_gap_size >= min_gap_ticks * min_tick
 
swing_h = ta.highest(high, disp_lookback)[2]
swing_l = ta.lowest(low, disp_lookback)[2]
bull_has_disp = high[1] > swing_h or close[1] > swing_h
bear_has_disp = low[1] < swing_l or close[1] < swing_l
 
valid_bull_fvg = raw_bull_fvg and (disp_mode == "Any FVG (Standard)" or bull_has_disp)
valid_bear_fvg = raw_bear_fvg and (disp_mode == "Any FVG (Standard)" or bear_has_disp)
 
// --------------------------------------------------------------------------------------------------------------------}
// PERSISTENT SESSION STATE VARIABLES
// --------------------------------------------------------------------------------------------------------------------{
var line v_sep_line = na
var line sess_open_line = na
var label sess_open_label = na
var float curr_sess_open_p = na
 
var bool first_fvg_found = false
var bool first_fvg_is_bull = false
var float first_fvg_top = na
var float first_fvg_bot = na
var float first_fvg_ce = na
var int first_fvg_left_bar = na
var box first_fvg_box = na
var line first_fvg_ce_line = na
var label first_fvg_label = na
var label first_fvg_ce_lbl = na
var bool first_fvg_mitigated = false
var bool first_fvg_has_disp = false
 
var bool refl_fvg_found = false
var bool refl_fvg_is_bull = false
var float refl_fvg_top = na
var float refl_fvg_bot = na
var float refl_fvg_ce = na
var int refl_fvg_left_bar = na
var box refl_fvg_box = na
var line refl_fvg_ce_line = na
var label refl_fvg_label = na
var label refl_fvg_ce_lbl = na
var bool refl_fvg_mitigated = false
var bool refl_fvg_has_disp = false
 
var box[] hist_fvg_boxes = array.new_box()
var line[] hist_fvg_lines = array.new_line()
var label[] hist_fvg_labels = array.new_label()
var label[] hist_fvg_ce_labels = array.new_label()
var int[] hist_fvg_times = array.new_int()
var bool[] hist_fvg_is_refl = array.new_bool()
 
var line[] hist_sess_lines = array.new_line()
var label[] hist_sess_labels = array.new_label()
var int[] hist_sess_times = array.new_int()
 
var bool alert_first_triggered = false
var bool alert_refl_triggered = false
var bool alert_ce_triggered = false
 
// --------------------------------------------------------------------------------------------------------------------}
// CLEANUP & ARRAY MANAGEMENT HELPERS
// --------------------------------------------------------------------------------------------------------------------{
manage_fvg_arrays(max_count) =>
    if array.size(hist_fvg_boxes) > max_count
        b_del = array.shift(hist_fvg_boxes)
        box.delete(b_del)
        ln_del = array.shift(hist_fvg_lines)
        if not na(ln_del)
            line.delete(ln_del)
        lbl_del = array.shift(hist_fvg_labels)
        if not na(lbl_del)
            label.delete(lbl_del)
        lbl_ce_del = array.shift(hist_fvg_ce_labels)
        if not na(lbl_ce_del)
            label.delete(lbl_ce_del)
        array.shift(hist_fvg_times)
        array.shift(hist_fvg_is_refl)
 
manage_sess_arrays(max_count) =>
    if array.size(hist_sess_lines) > max_count
        ln_del = array.shift(hist_sess_lines)
        line.delete(ln_del)
        lbl_del = array.shift(hist_sess_labels)
        if not na(lbl_del)
            label.delete(lbl_del)
        array.shift(hist_sess_times)
 
// --------------------------------------------------------------------------------------------------------------------}
// ON NEW SESSION START
// --------------------------------------------------------------------------------------------------------------------{
if is_new_sess
    if show_historical
        if not na(first_fvg_box)
            array.push(hist_fvg_boxes, first_fvg_box)
            array.push(hist_fvg_lines, first_fvg_ce_line)
            array.push(hist_fvg_labels, first_fvg_label)
            array.push(hist_fvg_ce_labels, first_fvg_ce_lbl)
            array.push(hist_fvg_times, time)
            array.push(hist_fvg_is_refl, false)
        if not na(refl_fvg_box)
            array.push(hist_fvg_boxes, refl_fvg_box)
            array.push(hist_fvg_lines, refl_fvg_ce_line)
            array.push(hist_fvg_labels, refl_fvg_label)
            array.push(hist_fvg_ce_labels, refl_fvg_ce_lbl)
            array.push(hist_fvg_times, time)
            array.push(hist_fvg_is_refl, true)
        if not na(sess_open_line)
            array.push(hist_sess_lines, sess_open_line)
            array.push(hist_sess_labels, sess_open_label)
            array.push(hist_sess_times, time)
 
        manage_fvg_arrays(150)
        manage_sess_arrays(50)
    else
        if not na(first_fvg_box)
            box.delete(first_fvg_box)
            line.delete(first_fvg_ce_line)
            label.delete(first_fvg_label)
            label.delete(first_fvg_ce_lbl)
        if not na(refl_fvg_box)
            box.delete(refl_fvg_box)
            line.delete(refl_fvg_ce_line)
            label.delete(refl_fvg_label)
            label.delete(refl_fvg_ce_lbl)
        if not na(sess_open_line)
            line.delete(sess_open_line)
            label.delete(sess_open_label)
 
    first_fvg_found := false
    first_fvg_is_bull := false
    first_fvg_top := na
    first_fvg_bot := na
    first_fvg_ce := na
    first_fvg_box := na
    first_fvg_ce_line := na
    first_fvg_label := na
    first_fvg_ce_lbl := na
    first_fvg_mitigated := false
    first_fvg_has_disp := false
 
    refl_fvg_found := false
    refl_fvg_is_bull := false
    refl_fvg_top := na
    refl_fvg_bot := na
    refl_fvg_ce := na
    refl_fvg_box := na
    refl_fvg_ce_line := na
    refl_fvg_label := na
    refl_fvg_ce_lbl := na
    refl_fvg_mitigated := false
    refl_fvg_has_disp := false
 
    alert_first_triggered := false
    alert_refl_triggered := false
    alert_ce_triggered := false
 
    curr_sess_open_p := open
 
    if show_v_line
        v_sep_line := line.new(bar_index, low - min_tick * 10, bar_index, high + min_tick * 10, color=v_line_color, style=line.style_dotted, extend=extend.both)
 
    if show_open_line
        sess_open_line := line.new(bar_index, curr_sess_open_p, bar_index, curr_sess_open_p, color=open_line_col, style=get_line_style(open_line_style), extend=extend.none)
        sess_title = str.substring(active_session_str, 0, 2) + ":" + str.substring(active_session_str, 2, 4) + " Open"
        bullet_str = use_bullet ? "• " : ""
        open_lbl_style = get_label_style(label_appearance)
        sess_open_label := label.new(bar_index + label_x_offset, curr_sess_open_p, text=bullet_str + sess_title + " (" + str.tostring(curr_sess_open_p, "#.##") + ")", xloc=xloc.bar_index, yloc=yloc.price, style=open_lbl_style, size=get_label_size(label_size_opt), textcolor=open_line_col, color=color.new(open_line_col, 100))
 
// --------------------------------------------------------------------------------------------------------------------}
// FVG IDENTIFICATION & PLOTTING WITHIN SESSION
// --------------------------------------------------------------------------------------------------------------------{
day_formatted = get_day_formatted(time, session_tz)
date_mmdd     = str.format_time(time, "MM/dd", session_tz)
bullet_prefix = use_bullet ? "• " : ""
 
can_search_reflection = in_session or (refl_search_window == "Extend Search to End of Day (16:00 NY)" and in_day_window)
 
if in_session or can_search_reflection
    // STEP 1: DETECT FIRST PRESENTED FVG
    if in_session and not first_fvg_found and (valid_bull_fvg or valid_bear_fvg)
        first_fvg_found := true
        first_fvg_is_bull := valid_bull_fvg
        first_fvg_has_disp := valid_bull_fvg ? bull_has_disp : bear_has_disp
        first_fvg_left_bar := math.max(0, bar_index - 2)
 
        if first_fvg_is_bull
            first_fvg_top := low
            first_fvg_bot := high[2]
        else
            first_fvg_top := low[2]
            first_fvg_bot := high
 
        first_fvg_ce := (first_fvg_top + first_fvg_bot) * 0.5
 
        if show_first_fvg
            box_bg = first_fvg_is_bull ? first_bull_col : first_bear_col
            first_fvg_box := box.new(first_fvg_left_bar, first_fvg_top, bar_index, first_fvg_bot, border_color=first_border_col, border_style=line.style_solid, border_width=1, bgcolor=box_bg)
 
            if show_first_ce
                first_fvg_ce_line := line.new(first_fvg_left_bar, first_fvg_ce, bar_index, first_fvg_ce, color=first_ce_col, style=get_line_style(first_ce_style), width=1)
                if show_ce_line_lbl
                    ce_bullet = use_bullet ? " •" : ""
                    ce_text_1st = show_ce_price ? "0.5 (" + str.tostring(first_fvg_ce, "#.##") + ")" + ce_bullet : "0.5" + ce_bullet
                    first_fvg_ce_lbl := label.new(bar_index + label_x_offset, first_fvg_ce, text=ce_text_1st, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, size=get_label_size(label_size_opt), textcolor=first_ce_col)
 
            if show_first_lbl
                price_tag = show_prices_lbl ? " [" + str.tostring(first_fvg_bot, "#.##") + " - " + str.tostring(first_fvg_top, "#.##") + "]" : ""
                prefix_str = switch fvg_lbl_format
                    "Day + Date (e.g. Fri 09/04 1st P. FVG)" => day_formatted + " " + date_mmdd + " "
                    "Day Only (e.g. Fri 1st P. FVG)"         => day_formatted + " "
                    "Date Only (e.g. 09/04 1st P. FVG)"       => date_mmdd + " "
                    => ""
                lbl_text = bullet_prefix + prefix_str + "1st P. FVG" + price_tag
 
                lbl_style = get_label_style(label_appearance)
                zone_col = first_fvg_is_bull ? color.new(#089981, 0) : color.new(#f23645, 0)
                lbl_txt_col = switch label_color_mode
                    "Chart Auto (Theme Adaptive)" => chart.fg_color
                    "Zone Matching Color"         => zone_col
                    "White"                       => color.white
                    "Dark / Charcoal"              => #131722
                    => custom_lbl_col
 
                lbl_box_col = label_appearance == "Clean Text (No Box)" ? color.new(color.white, 100) : color.new(zone_col, 30)
 
                // Position slightly in the upper quadrant so it sits above the 0.5 CE line
                fvg_lbl_y = (first_fvg_top + first_fvg_ce) * 0.5
                first_fvg_label := label.new(bar_index + label_x_offset, fvg_lbl_y, text=lbl_text, xloc=xloc.bar_index, yloc=yloc.price, style=lbl_style, size=get_label_size(label_size_opt), textcolor=lbl_txt_col, color=lbl_box_col)
 
        if not alert_first_triggered
            alert("1st Presented FVG formed at " + str.tostring(close), alert.freq_once_per_bar)
            alert_first_triggered := true
 
    // STEP 2: DETECT FIRST PRESENTED REFLECTION FVG (RFVG)
    else if first_fvg_found and not refl_fvg_found
        is_reflection_opp = (first_fvg_is_bull and valid_bear_fvg) or (not first_fvg_is_bull and valid_bull_fvg)
 
        if is_reflection_opp
            refl_fvg_found := true
            refl_fvg_is_bull := valid_bull_fvg
            refl_fvg_has_disp := valid_bull_fvg ? bull_has_disp : bear_has_disp
            refl_fvg_left_bar := math.max(0, bar_index - 2)
 
            if refl_fvg_is_bull
                refl_fvg_top := low
                refl_fvg_bot := high[2]
            else
                refl_fvg_top := low[2]
                refl_fvg_bot := high
 
            refl_fvg_ce := (refl_fvg_top + refl_fvg_bot) * 0.5
 
            if show_refl_fvg
                refl_bg = refl_fvg_is_bull ? refl_bull_col : refl_bear_col
                refl_fvg_box := box.new(refl_fvg_left_bar, refl_fvg_top, bar_index, refl_fvg_bot, border_color=refl_border_col, border_style=line.style_solid, border_width=1, bgcolor=refl_bg)
 
                if show_refl_ce
                    refl_fvg_ce_line := line.new(refl_fvg_left_bar, refl_fvg_ce, bar_index, refl_fvg_ce, color=refl_ce_col, style=get_line_style(refl_ce_style), width=1)
                    if show_ce_line_lbl
                        ce_bullet_refl = use_bullet ? " •" : ""
                        ce_text_refl = show_ce_price ? "0.5 (" + str.tostring(refl_fvg_ce, "#.##") + ")" + ce_bullet_refl : "0.5" + ce_bullet_refl
                        refl_fvg_ce_lbl := label.new(bar_index + label_x_offset, refl_fvg_ce, text=ce_text_refl, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, size=get_label_size(label_size_opt), textcolor=refl_ce_col)
 
                if show_refl_lbl
                    price_tag = show_prices_lbl ? " [" + str.tostring(refl_fvg_bot, "#.##") + " - " + str.tostring(refl_fvg_top, "#.##") + "]" : ""
                    refl_prefix = switch refl_lbl_format
                        "Day Only (e.g. Fri 1st P. Reflection FVG)"        => day_formatted + " "
                        "Day + Date (e.g. Fri 09/04 1st P. Reflection FVG)" => day_formatted + " " + date_mmdd + " "
                        => ""
                    refl_lbl_text = bullet_prefix + refl_prefix + "1st P. Reflection FVG" + price_tag
 
                    lbl_style = get_label_style(label_appearance)
                    refl_zone_col = refl_fvg_is_bull ? color.new(#00bcd4, 0) : color.new(#ff9800, 0)
                    refl_lbl_txt_col = switch label_color_mode
                        "Chart Auto (Theme Adaptive)" => chart.fg_color
                        "Zone Matching Color"         => refl_zone_col
                        "White"                       => color.white
                        "Dark / Charcoal"              => #131722
                        => custom_lbl_col
 
                    refl_lbl_box_col = label_appearance == "Clean Text (No Box)" ? color.new(color.white, 100) : color.new(refl_zone_col, 20)
 
                    refl_lbl_y = (refl_fvg_top + refl_fvg_ce) * 0.5
                    refl_fvg_label := label.new(bar_index + label_x_offset, refl_lbl_y, text=refl_lbl_text, xloc=xloc.bar_index, yloc=yloc.price, style=lbl_style, size=get_label_size(label_size_opt), textcolor=refl_lbl_txt_col, color=refl_lbl_box_col)
 
            if not alert_refl_triggered
                alert("1st Reflection FVG formed at " + str.tostring(close), alert.freq_once_per_bar)
                alert_refl_triggered := true
 
// --------------------------------------------------------------------------------------------------------------------}
// ACTIVE SESSION MONITORING & MITIGATION TRACKING
// --------------------------------------------------------------------------------------------------------------------{
src_low = mitig_source == "Wick" ? low : close
src_high = mitig_source == "Wick" ? high : close
 
// Global CE test condition
tested_ce = refl_fvg_found and (refl_fvg_is_bull ? (low <= refl_fvg_ce and high >= refl_fvg_ce) : (high >= refl_fvg_ce and low <= refl_fvg_ce))
 
if first_fvg_found and not first_fvg_mitigated
    is_mitigated = first_fvg_is_bull ? (src_low < first_fvg_bot) : (src_high > first_fvg_top)
    if is_mitigated
        first_fvg_mitigated := true
        if mitig_action == "Dim Color On Mitigation" and not na(first_fvg_box)
            curr_bg = first_fvg_is_bull ? first_bull_col : first_bear_col
            box.set_bgcolor(first_fvg_box, color.new(curr_bg, 90))
 
if refl_fvg_found and not refl_fvg_mitigated
    is_refl_mitigated = refl_fvg_is_bull ? (src_low < refl_fvg_bot) : (src_high > refl_fvg_top)
    if is_refl_mitigated
        refl_fvg_mitigated := true
        if mitig_action == "Dim Color On Mitigation" and not na(refl_fvg_box)
            curr_refl_bg = refl_fvg_is_bull ? refl_bull_col : refl_bear_col
            box.set_bgcolor(refl_fvg_box, color.new(curr_refl_bg, 90))
 
    if tested_ce and not alert_ce_triggered
        alert("1st Reflection FVG C.E. (50%) Tested at " + str.tostring(close), alert.freq_once_per_bar)
        alert_ce_triggered := true
 
// --------------------------------------------------------------------------------------------------------------------}
// DYNAMIC FORWARD EXTENSION (CURRENT SESSION)
// --------------------------------------------------------------------------------------------------------------------{
can_ext_first = not na(first_fvg_box) and (not first_fvg_mitigated or mitig_action != "Stop At Mitigation Bar")
if can_ext_first
    if ext_mode == "Extend Infinitely (Right)"
        box.set_extend(first_fvg_box, extend.right)
        if not na(first_fvg_ce_line)
            line.set_extend(first_fvg_ce_line, extend.right)
        if not na(first_fvg_label)
            label.set_x(first_fvg_label, bar_index + label_x_offset)
        if not na(first_fvg_ce_lbl)
            label.set_x(first_fvg_ce_lbl, bar_index + label_x_offset)
    else if ext_mode == "Extend to Current Bar" or in_session
        box.set_right(first_fvg_box, bar_index)
        if not na(first_fvg_ce_line)
            line.set_x2(first_fvg_ce_line, bar_index)
        if not na(first_fvg_label)
            label.set_x(first_fvg_label, bar_index + label_x_offset)
        if not na(first_fvg_ce_lbl)
            label.set_x(first_fvg_ce_lbl, bar_index + label_x_offset)
 
can_ext_refl = not na(refl_fvg_box) and (not refl_fvg_mitigated or mitig_action != "Stop At Mitigation Bar")
if can_ext_refl
    if ext_mode == "Extend Infinitely (Right)"
        box.set_extend(refl_fvg_box, extend.right)
        if not na(refl_fvg_ce_line)
            line.set_extend(refl_fvg_ce_line, extend.right)
        if not na(refl_fvg_label)
            label.set_x(refl_fvg_label, bar_index + label_x_offset)
        if not na(refl_fvg_ce_lbl)
            label.set_x(refl_fvg_ce_lbl, bar_index + label_x_offset)
    else if ext_mode == "Extend to Current Bar" or in_session
        box.set_right(refl_fvg_box, bar_index)
        if not na(refl_fvg_ce_line)
            line.set_x2(refl_fvg_ce_line, bar_index)
        if not na(refl_fvg_label)
            label.set_x(refl_fvg_label, bar_index + label_x_offset)
        if not na(refl_fvg_ce_lbl)
            label.set_x(refl_fvg_ce_lbl, bar_index + label_x_offset)
 
if not na(sess_open_line)
    if ext_mode == "Extend Infinitely (Right)"
        line.set_extend(sess_open_line, extend.right)
        if not na(sess_open_label)
            label.set_x(sess_open_label, bar_index + label_x_offset)
    else if ext_mode == "Extend to Current Bar" or in_session
        line.set_x2(sess_open_line, bar_index)
        if not na(sess_open_label)
            label.set_x(sess_open_label, bar_index + label_x_offset)
 
// --------------------------------------------------------------------------------------------------------------------}
// HISTORICAL FORWARD PROJECTION (RUNS EFFICIENTLY ON LAST BAR)
// --------------------------------------------------------------------------------------------------------------------{
if barstate.islast and show_historical
    cutoff_time = time - history_days * 86400000
 
    if array.size(hist_fvg_boxes) > 0
        for i = 0 to array.size(hist_fvg_boxes) - 1
            b_time = array.get(hist_fvg_times, i)
            if b_time >= cutoff_time
                b = array.get(hist_fvg_boxes, i)
                ln = array.get(hist_fvg_lines, i)
                lbl = array.get(hist_fvg_labels, i)
                lbl_ce = array.get(hist_fvg_ce_labels, i)
 
                if ext_mode == "Extend to Current Bar"
                    box.set_right(b, bar_index)
                    if not na(ln)
                        line.set_x2(ln, bar_index)
                    if not na(lbl)
                        label.set_x(lbl, bar_index + label_x_offset)
                    if not na(lbl_ce)
                        label.set_x(lbl_ce, bar_index + label_x_offset)
                else if ext_mode == "Extend Infinitely (Right)"
                    box.set_extend(b, extend.right)
                    if not na(ln)
                        line.set_extend(ln, extend.right)
                    if not na(lbl)
                        label.set_x(lbl, bar_index + label_x_offset)
                    if not na(lbl_ce)
                        label.set_x(lbl_ce, bar_index + label_x_offset)
 
    if array.size(hist_sess_lines) > 0 and show_open_line
        for j = 0 to array.size(hist_sess_lines) - 1
            s_time = array.get(hist_sess_times, j)
            if s_time >= cutoff_time
                ln_s = array.get(hist_sess_lines, j)
                lbl_s = array.get(hist_sess_labels, j)
 
                if ext_mode == "Extend to Current Bar"
                    line.set_x2(ln_s, bar_index)
                    if not na(lbl_s)
                        label.set_x(lbl_s, bar_index + label_x_offset)
                else if ext_mode == "Extend Infinitely (Right)"
                    line.set_extend(ln_s, extend.right)
                    if not na(lbl_s)
                        label.set_x(lbl_s, bar_index + label_x_offset)
 
// --------------------------------------------------------------------------------------------------------------------}
// DASHBOARD HUD STATUS TABLE
// --------------------------------------------------------------------------------------------------------------------{
var table hud = table.new(position=position.top_right, columns=2, rows=4, bgcolor=color.new(#131722, 15), border_color=color.new(#2a2e39, 40), border_width=1, frame_color=color.new(#2a2e39, 40), frame_width=1)
 
if barstate.islast and show_hud_table
    table.cell(hud, 0, 0, "TradeSymmetry FVG Engine", bgcolor=color.new(#1e222d, 0), text_color=color.white, text_size=size.small, text_halign=text.align_left)
    table.cell(hud, 1, 0, active_session_str + " NY", bgcolor=color.new(#1e222d, 0), text_color=color.new(color.yellow, 0), text_size=size.small, text_halign=text.align_right)
 
    sess_status = in_session ? "ACTIVE" : "CLOSED"
    sess_status_col = in_session ? color.lime : color.gray
    table.cell(hud, 0, 1, "Session Status", text_color=color.rgb(180, 185, 200), text_size=size.tiny, text_halign=text.align_left)
    table.cell(hud, 1, 1, sess_status + (not na(curr_sess_open_p) ? " (" + str.tostring(curr_sess_open_p, "#.##") + ")" : ""), text_color=sess_status_col, text_size=size.tiny, text_halign=text.align_right)
 
    f1_status = first_fvg_found ? (first_fvg_is_bull ? "BISI" : "SIBI") + (first_fvg_has_disp ? " [Disp]" : "") : "Searching..."
    f1_col = first_fvg_found ? (first_fvg_is_bull ? color.lime : color.red) : color.gray
    table.cell(hud, 0, 2, "1st Presented FVG", text_color=color.rgb(180, 185, 200), text_size=size.tiny, text_halign=text.align_left)
    table.cell(hud, 1, 2, f1_status, text_color=f1_col, text_size=size.tiny, text_halign=text.align_right)
 
    refl_status = refl_fvg_found ? (refl_fvg_is_bull ? "BISI" : "SIBI") + (refl_fvg_has_disp ? " [Disp]" : "") : (first_fvg_found ? "Waiting for " + (first_fvg_is_bull ? "SIBI" : "BISI") : "Waiting for 1st FVG")
    refl_col = refl_fvg_found ? (refl_fvg_is_bull ? color.aqua : color.orange) : color.gray
    table.cell(hud, 0, 3, "1st Reflection FVG", text_color=color.rgb(180, 185, 200), text_size=size.tiny, text_halign=text.align_left)
    table.cell(hud, 1, 3, refl_status, text_color=refl_col, text_size=size.tiny, text_halign=text.align_right)
 
// --------------------------------------------------------------------------------------------------------------------}
// ALERT CONDITIONS
// --------------------------------------------------------------------------------------------------------------------{
alertcondition(valid_bull_fvg or valid_bear_fvg, "Any FVG Detected", "FVG formed on {{ticker}}")
alertcondition(first_fvg_found and not first_fvg_found[1], "1st Presented FVG Detected", "1st Presented FVG formed on {{ticker}}")
alertcondition(refl_fvg_found and not refl_fvg_found[1], "1st Reflection FVG Detected", "1st Reflection FVG formed on {{ticker}}")
alertcondition(tested_ce and not tested_ce[1], "Reflection FVG C.E. Tested", "Reflection FVG C.E. (50%) tested on {{ticker}}")

← 返回列表