EMA SMA Cloud Multi-TF & SMC Liquidity OB, BSL, SSL, FVG

realmacgyver77 · study · 751 行 · 点赞 60 · TradingView 原页

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

Pine Script

//@version=6
indicator("EMA SMA Cloud Multi-TF  & SMC Liquidity Suite with OB, BSL, SSL, FVG", shorttitle="MTF EMA/SMA & SMC", overlay=true, max_lines_count=500, max_boxes_count=500, max_labels_count=500, explicit_plot_zorder=true)
 
// ============================================================================
// 1. CURRENT TIMEFRAME DUAL MAIN MAs (SLOW & FAST) & CLOUD
// ============================================================================
grp_curr = "1. Current Timeframe Main MAs & Cloud"
i_ma_type       = input.string("EMA", "Moving Average Type (Default EMA)", options=["EMA", "SMA"], group=grp_curr, tooltip="Selects whether the primary moving averages on the current chart and the two higher reference timeframes are calculated as Exponential Moving Averages (EMA) or Simple Moving Averages (SMA).")
i_fast_len      = input.int(21, "Fast Main MA Length (Default 21)", minval=1, group=grp_curr, tooltip="Configurable period length for the fast primary moving average displayed on the current chart timeframe and evaluated against the next two higher timeframes.")
i_slow_len      = input.int(200, "Slow Main MA Length (Default 200)", minval=1, group=grp_curr, tooltip="Configurable period length for the slow primary moving average displayed on the current chart timeframe and evaluated against the next two higher timeframes.")
 
i_curr_slow_on  = input.bool(true, "Show Current TF Slow Main MA (Gold)", inline="cs", group=grp_curr, tooltip="Toggles visibility of the slow main moving average on the active chart timeframe.")
i_curr_slow_col = input.color(#FFD700, "Color", inline="cs", group=grp_curr, tooltip="Default color for the slow main moving average when not aligned in trend with the two higher timeframes.")
i_curr_slow_w   = input.int(2, "Width", minval=1, maxval=10, inline="cs", group=grp_curr, tooltip="Line thickness in pixels for the slow main moving average on the current chart timeframe (default 2).")
i_curr_slow_st  = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], inline="cs", group=grp_curr, tooltip="Line style (Solid, Dashed, or Dotted) for the slow main moving average on the current chart timeframe.")
 
i_curr_fast_on  = input.bool(true, "Show Current TF Fast Main MA (Dark Orange)", inline="cf", group=grp_curr, tooltip="Toggles visibility of the fast main moving average on the active chart timeframe.")
i_curr_fast_col = input.color(#FF6D00, "Color", inline="cf", group=grp_curr, tooltip="Default color for the fast main moving average when not aligned in trend with the two higher timeframes.")
i_curr_fast_w   = input.int(1, "Width", minval=1, maxval=10, inline="cf", group=grp_curr, tooltip="Line thickness in pixels for the fast main moving average on the current chart timeframe (default 1).")
i_curr_fast_st  = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], inline="cf", group=grp_curr, tooltip="Line style (Solid, Dashed, or Dotted) for the fast main moving average on the current chart timeframe.")
 
i_align_enable  = input.bool(true, "Enable MTF Trend Alignment Coloring (Green)", group=grp_curr, tooltip="When enabled, checks if the current chart MA direction (upward, downward, or sideways) matches the same MA on the next two higher timeframes (HTF +1 and HTF +2 in the 1m-5m-15m-1h-4h-1d-1w grid). If all three align, the current Slow Main MA turns Light Green and the current Fast Main MA turns Dark Green.")
i_align_slow_col= input.color(#69F0AE, "Aligned Slow Main MA Color (Light Green)", inline="al_c", group=grp_curr, tooltip="Color applied to the current chart Slow Main MA (default Light Green) when its trend direction (upward, downward, or sideways) aligns with the Slow Main MAs of the next two higher timeframes (HTF +1 and HTF +2).")
i_align_fast_col= input.color(#00C853, "Aligned Fast Main MA Color (Dark Green)", inline="al_c", group=grp_curr, tooltip="Color applied to the current chart Fast Main MA (default Dark Green) when its trend direction (upward, downward, or sideways) aligns with the Fast Main MAs of the next two higher timeframes (HTF +1 and HTF +2).")
i_sideways_pct  = input.float(0.01, "Sideways Slope Threshold (% per Bar)", minval=0.0, step=0.005, group=grp_curr, tooltip="Percentage change threshold per bar used to classify an MA slope as sideways (flat) versus upward or downward when evaluating multi-timeframe trend alignment.")
i_smooth_mtf    = input.bool(true, "Smooth Reference HTF MAs for Slope Check", group=grp_curr, tooltip="Applies EMA smoothing proportional to the timeframe ratio to the hidden higher timeframe reference MAs so their slope direction transitions smoothly without stair-step jumps.")
 
i_cloud_enable  = input.bool(true, "Enable Point Cloud Around Current Main MA", group=grp_curr, tooltip="Displays a symmetrical shaded zone above and below the selected current timeframe main MA using a fixed distance in points/ticks.")
i_cloud_target  = input.string("Fast Main MA", "Cloud Center MA", options=["Fast Main MA", "Slow Main MA"], group=grp_curr, tooltip="Chooses whether the point-distance cloud wraps around the current timeframe Fast Main MA or Slow Main MA, automatically matching its active color.")
i_cloud_pts     = input.float(50.0, "Cloud Offset Distance (in Points/Ticks)", minval=0.0, step=1.0, group=grp_curr, tooltip="Distance in chart ticks/points added above and subtracted below the selected current timeframe main MA to form the cloud band.")
i_cloud_transp  = input.int(90, "Cloud Transparency (%)", minval=0, maxval=100, group=grp_curr, tooltip="Transparency level (0 = opaque, 100 = invisible) for the point-distance cloud around the current timeframe main MA.")
 
// ============================================================================
// 2. MULTI-TIMEFRAME LIQUIDITY LEVELS (BSL & SSL)
// ============================================================================
grp_liq = "2. Multi-Timeframe Liquidity Levels (BSL & SSL)"
i_liq_enable      = input.bool(true, "Enable MTF BSL & SSL Levels", group=grp_liq, tooltip="Detects and plots unmitigated Buy-Side Liquidity (swing highs) and Sell-Side Liquidity (swing lows) anchored directly at the exact pivot wick where the liquidity formed.")
i_liq_curr_on     = input.bool(true, "Show Current Chart TF BSL & SSL", group=grp_liq, tooltip="Enables Buy-Side Liquidity (BSL) and Sell-Side Liquidity (SSL) level detection and plotting for the currently displayed chart timeframe.")
i_liq_swing       = input.int(5, "Swing Detection Lookback (Bars)", minval=2, maxval=50, group=grp_liq, tooltip="Number of bars to the left and right required to confirm a structural swing high (BSL) or swing low (SSL) on each liquidity timeframe.")
i_liq_extend      = input.int(10, "Extend Lines Right of Current Price (Bars)", minval=1, maxval=200, group=grp_liq, tooltip="Number of bars to project active liquidity lines and current TF SMC zones to the right past the current live candle (default 10 bars).")
i_liq_max_lvls    = input.int(8, "Max Active Levels per Side per TF", minval=1, maxval=10, group=grp_liq, tooltip="Maximum number of active unswept BSL lines and SSL lines kept per timeframe before older levels are pruned (default 8).")
 
i_liq_keep_swept  = input.bool(false, "Keep Swept Liquidity as Faded Gray", inline="lsw", group=grp_liq, tooltip="When disabled (default), swept BSL and SSL levels are removed immediately. When enabled, swept levels remain visible in transparent gray ending at the exact candle where they were swept.")
i_liq_swept_col   = input.color(color.new(#9E9E9E, 75), "Swept Color", inline="lsw", group=grp_liq, tooltip="Faded gray color and transparency used for swept BSL and SSL lines and labels when 'Keep Swept Liquidity as Faded Gray' is enabled.")
i_liq_max_swept   = input.int(15, "Max Retained Swept Lines", minval=1, maxval=50, inline="lsw", group=grp_liq, tooltip="Maximum number of historical swept liquidity lines retained on the chart when 'Keep Swept Liquidity as Faded Gray' is enabled.")
 
i_liq_bsl_col     = input.color(#FF1744, "BSL Line & Text Color (Red - Bearish Reversal)", inline="lcol", group=grp_liq, tooltip="Color for Buy-Side Liquidity (BSL) horizontal lines and text labels (default Red, signaling potential bearish reversal after liquidity sweep).")
i_liq_ssl_col     = input.color(#00E676, "SSL Line & Text Color (Green - Bullish Reversal)", inline="lcol", group=grp_liq, tooltip="Color for Sell-Side Liquidity (SSL) horizontal lines and text labels (default Green, signaling potential bullish reversal after liquidity sweep).")
i_liq_txt_size    = input.string("Small", "Liquidity Label Text Size", options=["Tiny", "Small", "Normal", "Large"], inline="ltxt", group=grp_liq, tooltip="Font size for the BSL and SSL timeframe labels displayed on the liquidity lines.")
i_liq_txt_pos     = input.string("Right", "Liquidity Label Position", options=["Right", "Center", "Left"], inline="ltxt", group=grp_liq, tooltip="Horizontal placement of the BSL/SSL label along each liquidity line (Right end, Center, or Left pivot wick origin).")
 
i_liq_tf1_on      = input.bool(true, "Liq TF 1", inline="ltf_a", group=grp_liq, tooltip="Enables liquidity level tracking for Liquidity Timeframe 1.")
i_liq_tf1         = input.timeframe("15", "", inline="ltf_a", group=grp_liq, tooltip="Timeframe used for Liquidity Slot 1 (default 15m).")
i_liq_tf2_on      = input.bool(true, "Liq TF 2", inline="ltf_a", group=grp_liq, tooltip="Enables liquidity level tracking for Liquidity Timeframe 2.")
i_liq_tf2         = input.timeframe("60", "", inline="ltf_a", group=grp_liq, tooltip="Timeframe used for Liquidity Slot 2 (default 1H).")
i_liq_tf3_on      = input.bool(true, "Liq TF 3", inline="ltf_b", group=grp_liq, tooltip="Enables liquidity level tracking for Liquidity Timeframe 3.")
i_liq_tf3         = input.timeframe("240", "", inline="ltf_b", group=grp_liq, tooltip="Timeframe used for Liquidity Slot 3 (default 4H).")
i_liq_tf4_on      = input.bool(true, "Liq TF 4", inline="ltf_b", group=grp_liq, tooltip="Enables liquidity level tracking for Liquidity Timeframe 4.")
i_liq_tf4         = input.timeframe("D", "", inline="ltf_b", group=grp_liq, tooltip="Timeframe used for Liquidity Slot 4 (default Daily).")
 
// ============================================================================
// 3. SMART MONEY CONCEPTS (CURRENT TF & RIGHT-SIDE HTF SMC OB & FVG PANEL)
// ============================================================================
grp_smc = "3. Smart Money Concepts (SMC OB, Mitigation Block, FVG, iFVG, Breaker)"
i_ob_enable        = input.bool(true, "Show Current TF SMC Order Blocks (SMC OB)", inline="obs", group=grp_smc, tooltip="Displays true structural SMC Order Blocks (labeled 'SMC OB' on chart) from the current chart timeframe: the last opposing candle at a confirmed local Swing High or Swing Low followed by an impulsive Fair Value Gap displacement (enabled by default).")
i_ob_swing_len     = input.int(5, "Swing Lookback", minval=2, maxval=30, inline="obs", group=grp_smc, tooltip="Number of preceding bars used to verify whether the opposing candle marks a true structural Swing High/Low (classified as 'SMC OB') or occurs mid-trend (classified as 'Mitigation Block').")
 
i_htf_ob_enable    = input.bool(true, "Show Additional TF SMC OBs Right of BSL/SSL", inline="htfob1", group=grp_smc, tooltip="Displays structural SMC Order Blocks from the selected additional timeframe (default 4H) as compact floating boxes positioned to the right of the chart and right of the BSL/SSL liquidity labels.")
i_htf_fvg_enable   = input.bool(true, "Show Additional TF FVGs Right of BSL/SSL", inline="htfob1", group=grp_smc, tooltip="Displays Fair Value Gaps from the selected additional timeframe (default 4H) as compact floating boxes positioned to the right of the chart and right of the BSL/SSL liquidity labels.")
i_htf_ob_tf        = input.timeframe("240", "Additional TF (Default 4H)", inline="htfob2", group=grp_smc, tooltip="Timeframe used to calculate the right-side floating SMC Order Block and Fair Value Gap boxes (default 4H).")
i_htf_ob_offset    = input.int(15, "Gap Right of BSL/SSL (Bars)", minval=1, maxval=100, inline="htfob2", group=grp_smc, tooltip="Horizontal spacing in chart bars to the right of the BSL/SSL line endpoints where the additional timeframe SMC OB and FVG boxes start (default 15 bars).")
i_htf_ob_width     = input.int(15, "Box Width (Bars)", minval=3, maxval=100, inline="htfob2", group=grp_smc, tooltip="Horizontal width in chart bars of each additional timeframe SMC OB and FVG box displayed on the right side (default 15 bars).")
 
i_mb_enable        = input.bool(false, "Show Mitigation Blocks (Mitigation Block)", group=grp_smc, tooltip="Displays mid-trend continuation Mitigation Blocks (labeled 'Mitigation Block' on chart): an opposing candle inside an ongoing trend (not at a local Swing High/Low) that is followed by a Fair Value Gap continuation move (disabled by default).")
i_bb_enable        = input.bool(false, "Show Breaker Blocks (Mitigated SMC OBs)", group=grp_smc, tooltip="Displays Breaker Blocks (labeled 'Breaker' on chart) when an active structural SMC Order Block is invalidated by a candle close through it, flipping its support/resistance polarity (disabled by default).")
i_fvg_enable       = input.bool(true, "Show Current TF Fair Value Gaps (FVG)", group=grp_smc, tooltip="Displays 3-candle price imbalances (Fair Value Gaps, labeled 'FVG' on chart) from the current chart timeframe until price closes beyond the gap (enabled by default).")
i_ifvg_enable      = input.bool(false, "Show Inverse Fair Value Gaps (iFVG)", group=grp_smc, tooltip="Converts invalidated Fair Value Gaps into Inverse Fair Value Gaps (labeled 'iFVG' on chart) with inverted support/resistance polarity (disabled by default).")
 
i_smc_keep_invalid = input.bool(false, "Keep Invalidated FVGs & OBs as Faded Gray", inline="smcinv", group=grp_smc, tooltip="When disabled (default), invalidated FVGs, SMC OBs, and Mitigation Blocks are deleted immediately. When enabled, they remain visible as transparent gray boxes ending at the exact candle where price invalidated them.")
i_smc_invalid_col  = input.color(color.new(#9E9E9E, 92), "Invalidated Color", inline="smcinv", group=grp_smc, tooltip="Faded gray fill color and high transparency applied to invalidated FVGs and OBs when 'Keep Invalidated FVGs & OBs as Faded Gray' is enabled.")
i_smc_max_invalid  = input.int(20, "Max Retained Invalid Boxes", minval=1, maxval=60, inline="smcinv", group=grp_smc, tooltip="Maximum number of historical invalidated FVG and OB boxes retained on the chart when 'Keep Invalidated FVGs & OBs as Faded Gray' is enabled.")
i_smc_max_boxes    = input.int(15, "Max Active SMC Zones per Type", minval=3, maxval=50, group=grp_smc, tooltip="Maximum number of active boxes per SMC category retained on the chart to preserve performance and stay within TradingView limits.")
 
i_bull_ob_col      = input.color(color.new(#00FFFF, 90), "Bullish SMC OB Color (Neon Turquoise)", inline="smc1", group=grp_smc, tooltip="Fill and border color for active structural Bullish SMC Order Blocks (default Neon Turquoise with high transparency).")
i_bear_ob_col      = input.color(color.new(#FF1493, 90), "Bearish SMC OB Color (Neon Pink)", inline="smc1", group=grp_smc, tooltip="Fill and border color for active structural Bearish SMC Order Blocks (default Neon Pink with high transparency).")
i_bull_mb_col      = input.color(color.new(#26A69A, 90), "Bullish Mitigation Block Color (Teal)", inline="smcmb", group=grp_smc, tooltip="Fill and border color for active Bullish Mitigation Blocks (mid-trend continuation candles with FVG, default Teal with high transparency).")
i_bear_mb_col      = input.color(color.new(#EF5350, 90), "Bearish Mitigation Block Color (Coral)", inline="smcmb", group=grp_smc, tooltip="Fill and border color for active Bearish Mitigation Blocks (mid-trend continuation candles with FVG, default Coral with high transparency).")
i_bull_bb_col      = input.color(color.new(#00E5FF, 88), "Bullish Breaker Color (Light Blue)", inline="smc2", group=grp_smc, tooltip="Fill and border color for active Bullish Breaker Blocks (default Light Blue with high transparency, clearly distinct from Bearish Breaker Blocks).")
i_bear_bb_col      = input.color(color.new(#3D5AFE, 86), "Bearish Breaker Color (Dark Blue)", inline="smc2", group=grp_smc, tooltip="Fill and border color for active Bearish Breaker Blocks (default Dark Indigo Blue with high transparency, clearly distinct from Bullish Breaker Blocks).")
i_bull_fvg_col     = input.color(color.new(#00E676, 90), "Bullish FVG Color (Green)", inline="smc3", group=grp_smc, tooltip="Fill and border color for active Bullish Fair Value Gaps (default Green with high transparency).")
i_bear_fvg_col     = input.color(color.new(#FF1744, 90), "Bearish FVG Color (Red)", inline="smc3", group=grp_smc, tooltip="Fill and border color for active Bearish Fair Value Gaps (default Red with high transparency).")
i_bull_ifvg_col    = input.color(color.new(#FFD600, 90), "Bullish iFVG Color (Yellow)", inline="smc4", group=grp_smc, tooltip="Fill and border color for active Bullish Inverse Fair Value Gaps (default Yellow with high transparency).")
i_bear_ifvg_col    = input.color(color.new(#FFEA00, 90), "Bearish iFVG Color (Yellow)", inline="smc4", group=grp_smc, tooltip="Fill and border color for active Bearish Inverse Fair Value Gaps (default Yellow with high transparency).")
 
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
f_get_txt_size(string s) =>
    switch s
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        => size.small
 
f_calc_ma(int len) =>
    i_ma_type == "EMA" ? ta.ema(close, len) : ta.sma(close, len)
 
f_format_tf(string tf) =>
    string clean = tf == "" ? timeframe.period : tf
    clean == "60" ? "1H" : clean == "120" ? "2H" : clean == "180" ? "3H" : clean == "240" ? "4H" : clean
 
f_apply_line_style(float val, string st) =>
    if st == "Dashed"
        bar_index % 4 == 3 ? na : val
    else if st == "Dotted"
        bar_index % 2 == 1 ? na : val
    else
        val
 
// Classify MA slope/direction: 1 = upward, -1 = downward, 0 = sideways
f_get_ma_dir(float src) =>
    if na(src) or na(src[1]) or src[1] == 0
        0
    else
        float pct_chg = ((src - src[1]) / src[1]) * 100.0
        pct_chg > i_sideways_pct ? 1 : pct_chg < -i_sideways_pct ? -1 : 0
 
// ============================================================================
// TIMEFRAME GRID ENGINE: 1m (0), 5m (1), 15m (2), 1h (3), 4h (4), 1d (5), 1w (6)
// ============================================================================
int chart_sec = timeframe.in_seconds(timeframe.period)
int bar_ms    = math.max(1000, chart_sec * 1000)
 
// Map current chart timeframe to grid index 0..6
int curr_idx = switch
    chart_sec <= 60     => 0 // 1m
    chart_sec <= 300    => 1 // 5m
    chart_sec <= 900    => 2 // 15m
    chart_sec <= 3600   => 3 // 1h
    chart_sec <= 14400  => 4 // 4h
    chart_sec <= 86400  => 5 // 1d
    => 6                     // 1w
 
// Precompute simple int smoothing lengths for each fixed grid timeframe
int len_1m  = math.max(2, math.min(50, int(math.round(60.0     / math.max(1, chart_sec)))))
int len_5m  = math.max(2, math.min(50, int(math.round(300.0    / math.max(1, chart_sec)))))
int len_15m = math.max(2, math.min(50, int(math.round(900.0    / math.max(1, chart_sec)))))
int len_1h  = math.max(2, math.min(50, int(math.round(3600.0   / math.max(1, chart_sec)))))
int len_4h  = math.max(2, math.min(50, int(math.round(14400.0  / math.max(1, chart_sec)))))
int len_1d  = math.max(2, math.min(50, int(math.round(86400.0  / math.max(1, chart_sec)))))
int len_1w  = math.max(2, math.min(50, int(math.round(604800.0 / math.max(1, chart_sec)))))
 
// Fetch Fast & Slow MAs for each of the 7 grid timeframes (used strictly for MTF trend alignment check)
[ma_fast_1m_u,  ma_slow_1m_u]  = request.security(syminfo.tickerid, "1",   [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
[ma_fast_5m_u,  ma_slow_5m_u]  = request.security(syminfo.tickerid, "5",   [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
[ma_fast_15m_u, ma_slow_15m_u] = request.security(syminfo.tickerid, "15",  [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
[ma_fast_1h_u,  ma_slow_1h_u]  = request.security(syminfo.tickerid, "60",  [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
[ma_fast_4h_u,  ma_slow_4h_u]  = request.security(syminfo.tickerid, "240", [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
[ma_fast_1d_u,  ma_slow_1d_u]  = request.security(syminfo.tickerid, "D",   [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
[ma_fast_1w_u,  ma_slow_1w_u]  = request.security(syminfo.tickerid, "W",   [f_calc_ma(i_fast_len), f_calc_ma(i_slow_len)])
 
// Apply simple-int EMA smoothing per fixed timeframe
float ma_fast_1m   = i_smooth_mtf ? ta.ema(ma_fast_1m_u,   len_1m)  : ma_fast_1m_u
float ma_slow_1m   = i_smooth_mtf ? ta.ema(ma_slow_1m_u,   len_1m)  : ma_slow_1m_u
float ma_fast_5m   = i_smooth_mtf ? ta.ema(ma_fast_5m_u,   len_5m)  : ma_fast_5m_u
float ma_slow_5m   = i_smooth_mtf ? ta.ema(ma_slow_5m_u,   len_5m)  : ma_slow_5m_u
float ma_fast_15m  = i_smooth_mtf ? ta.ema(ma_fast_15m_u,  len_15m) : ma_fast_15m_u
float ma_slow_15m  = i_smooth_mtf ? ta.ema(ma_slow_15m_u,  len_15m) : ma_slow_15m_u
float ma_fast_1h   = i_smooth_mtf ? ta.ema(ma_fast_1h_u,   len_1h)  : ma_fast_1h_u
float ma_slow_1h   = i_smooth_mtf ? ta.ema(ma_slow_1h_u,   len_1h)  : ma_slow_1h_u
float ma_fast_4h   = i_smooth_mtf ? ta.ema(ma_fast_4h_u,   len_4h)  : ma_fast_4h_u
float ma_slow_4h   = i_smooth_mtf ? ta.ema(ma_slow_4h_u,   len_4h)  : ma_slow_4h_u
float ma_fast_1d   = i_smooth_mtf ? ta.ema(ma_fast_1d_u,   len_1d)  : ma_fast_1d_u
float ma_slow_1d   = i_smooth_mtf ? ta.ema(ma_slow_1d_u,   len_1d)  : ma_slow_1d_u
float ma_fast_1w   = i_smooth_mtf ? ta.ema(ma_fast_1w_u,   len_1w)  : ma_fast_1w_u
float ma_slow_1w   = i_smooth_mtf ? ta.ema(ma_slow_1w_u,   len_1w)  : ma_slow_1w_u
 
// Current Chart Direct MAs (exact native chart calculation)
float curr_fast = f_calc_ma(i_fast_len)
float curr_slow = f_calc_ma(i_slow_len)
 
// Helper to retrieve [ma_fast, ma_slow] by grid index 0..6
f_get_grid_mas(int idx) =>
    switch idx
        0 => [ma_fast_1m,  ma_slow_1m]
        1 => [ma_fast_5m,  ma_slow_5m]
        2 => [ma_fast_15m, ma_slow_15m]
        3 => [ma_fast_1h,  ma_slow_1h]
        4 => [ma_fast_4h,  ma_slow_4h]
        5 => [ma_fast_1d,  ma_slow_1d]
        6 => [ma_fast_1w,  ma_slow_1w]
        => [na, na]
 
// Resolve Relative Higher Timeframes (+1 and +2) for MTF alignment check
[htf1_fast_raw, htf1_slow_raw] = f_get_grid_mas(curr_idx + 1)
[htf2_fast_raw, htf2_slow_raw] = f_get_grid_mas(curr_idx + 2)
 
// Evaluate Multi-Timeframe Trend Alignment (Current TF vs HTF +1 and HTF +2)
int dir_curr_slow = f_get_ma_dir(curr_slow)
int dir_htf1_slow = f_get_ma_dir(htf1_slow_raw)
int dir_htf2_slow = f_get_ma_dir(htf2_slow_raw)
bool aligned_slow = i_align_enable and not na(htf1_slow_raw) and not na(htf2_slow_raw) and (dir_curr_slow == dir_htf1_slow) and (dir_curr_slow == dir_htf2_slow)
 
int dir_curr_fast = f_get_ma_dir(curr_fast)
int dir_htf1_fast = f_get_ma_dir(htf1_fast_raw)
int dir_htf2_fast = f_get_ma_dir(htf2_fast_raw)
bool aligned_fast = i_align_enable and not na(htf1_fast_raw) and not na(htf2_fast_raw) and (dir_curr_fast == dir_htf1_fast) and (dir_curr_fast == dir_htf2_fast)
 
color active_slow_col = aligned_slow ? i_align_slow_col : i_curr_slow_col
color active_fast_col = aligned_fast ? i_align_fast_col : i_curr_fast_col
 
// Plot ONLY Current Timeframe Dual Main MAs (Slow Main MA Gold/Light Green & Fast Main MA Dark Orange/Dark Green)
float p_curr_slow = i_curr_slow_on ? f_apply_line_style(curr_slow, i_curr_slow_st) : na
float p_curr_fast = i_curr_fast_on ? f_apply_line_style(curr_fast, i_curr_fast_st) : na
plot(p_curr_slow, "Current TF Slow Main MA (Gold / Aligned Light Green)",       color=active_slow_col, linewidth=i_curr_slow_w, style=plot.style_linebr, display=display.all - display.price_scale)
plot(p_curr_fast, "Current TF Fast Main MA (Dark Orange / Aligned Dark Green)", color=active_fast_col, linewidth=i_curr_fast_w, style=plot.style_linebr, display=display.all - display.price_scale)
 
// ============================================================================
// CLOUD AROUND CURRENT TIMEFRAME MAIN MA (IN POINTS/TICKS)
// ============================================================================
float cloud_center_val = i_cloud_target == "Fast Main MA" ? curr_fast : curr_slow
color cloud_center_col = i_cloud_target == "Fast Main MA" ? active_fast_col : active_slow_col
 
float offset_price = i_cloud_pts * syminfo.mintick
float cloud_upper  = i_cloud_enable ? cloud_center_val + offset_price : na
float cloud_lower  = i_cloud_enable ? cloud_center_val - offset_price : na
 
p_cloud_up = plot(cloud_upper, "Current TF MA Upper Band", color=color.new(cloud_center_col, 100), editable=false, display=display.none)
p_cloud_dn = plot(cloud_lower, "Current TF MA Lower Band", color=color.new(cloud_center_col, 100), editable=false, display=display.none)
fill(p_cloud_up, p_cloud_dn, color=color.new(cloud_center_col, i_cloud_transp), title="Current TF MA Point Cloud")
 
// ============================================================================
// MULTI-TIMEFRAME LIQUIDITY LEVELS (BSL & SSL) ANCHORED AT EXACT PIVOT WICK
// ============================================================================
var array<line> swept_liq_lns   = array.new_line()
var array<label> swept_liq_lbls = array.new_label()
 
f_archive_swept_liq(line ln, label lbl, int s_time, bool is_bsl) =>
    if i_liq_keep_swept
        line.set_x2(ln, time)
        line.set_color(ln, i_liq_swept_col)
        line.set_style(ln, line.style_dotted)
        int lbl_x = i_liq_txt_pos == "Left" ? s_time : i_liq_txt_pos == "Center" ? int(math.round((s_time + time) / 2)) : time
        string lbl_st = i_liq_txt_pos == "Left" ? (is_bsl ? label.style_label_down : label.style_label_up) : i_liq_txt_pos == "Center" ? (is_bsl ? label.style_label_down : label.style_label_up) : label.style_label_left
        label.set_x(lbl, lbl_x)
        label.set_style(lbl, lbl_st)
        label.set_textcolor(lbl, i_liq_swept_col)
        array.push(swept_liq_lns, ln)
        array.push(swept_liq_lbls, lbl)
        if array.size(swept_liq_lns) > i_liq_max_swept
            line.delete(array.shift(swept_liq_lns))
            label.delete(array.shift(swept_liq_lbls))
            int _v = 0
        int _void = 0
    else
        line.delete(ln)
        label.delete(lbl)
        int _void = 0
 
f_get_mtf_pivots(string tf, int len) =>
    request.security(syminfo.tickerid, tf, [ta.pivothigh(high, len, len), ta.pivotlow(low, len, len), time[len]], lookahead=barmerge.lookahead_off)
 
f_manage_liq(bool tf_on, string tf_str, array<line> b_lns, array<label> b_lbls, array<float> b_prcs, array<int> b_times, array<line> s_lns, array<label> s_lbls, array<float> s_prcs, array<int> s_times) =>
    if i_liq_enable and tf_on
        [ph, pl, piv_t] = f_get_mtf_pivots(tf_str, i_liq_swing)
        string tf_label = f_format_tf(tf_str)
        string t_size   = f_get_txt_size(i_liq_txt_size)
        int right_t     = time + i_liq_extend * bar_ms
 
        // New BSL (Swing High anchored at exact pivot wick timestamp)
        bool new_ph = not na(ph) and not na(piv_t) and (na(ph[1]) or ph != ph[1] or piv_t != piv_t[1])
        if new_ph
            int x1_t = piv_t
            line l = line.new(x1_t, ph, right_t, ph, xloc=xloc.bar_time, color=i_liq_bsl_col, width=1, style=line.style_solid)
            label lb = label.new(right_t, ph, tf_label + " BSL", xloc=xloc.bar_time, color=color.new(color.black, 100), textcolor=i_liq_bsl_col, style=label.style_label_left, size=size.small)
            label.set_size(lb, t_size)
            array.push(b_lns, l)
            array.push(b_lbls, lb)
            array.push(b_prcs, ph)
            array.push(b_times, x1_t)
            if array.size(b_lns) > i_liq_max_lvls
                line.delete(array.shift(b_lns))
                label.delete(array.shift(b_lbls))
                array.shift(b_prcs)
                array.shift(b_times)
            int _void1 = 0
 
        // New SSL (Swing Low anchored at exact pivot wick timestamp)
        bool new_pl = not na(pl) and not na(piv_t) and (na(pl[1]) or pl != pl[1] or piv_t != piv_t[1])
        if new_pl
            int x1_t = piv_t
            line l = line.new(x1_t, pl, right_t, pl, xloc=xloc.bar_time, color=i_liq_ssl_col, width=1, style=line.style_solid)
            label lb = label.new(right_t, pl, tf_label + " SSL", xloc=xloc.bar_time, color=color.new(color.black, 100), textcolor=i_liq_ssl_col, style=label.style_label_left, size=size.small)
            label.set_size(lb, t_size)
            array.push(s_lns, l)
            array.push(s_lbls, lb)
            array.push(s_prcs, pl)
            array.push(s_times, x1_t)
            if array.size(s_lns) > i_liq_max_lvls
                line.delete(array.shift(s_lns))
                label.delete(array.shift(s_lbls))
                array.shift(s_prcs)
                array.shift(s_times)
            int _void2 = 0
 
        // Update BSL positions & handle swept levels
        if array.size(b_lns) > 0
            for i = array.size(b_lns) - 1 to 0
                float prc  = array.get(b_prcs, i)
                int s_time = array.get(b_times, i)
                line ln    = array.get(b_lns, i)
                label lbl  = array.get(b_lbls, i)
                if high > prc and time > s_time
                    f_archive_swept_liq(ln, lbl, s_time, true)
                    array.remove(b_lns, i)
                    array.remove(b_lbls, i)
                    array.remove(b_prcs, i)
                    array.remove(b_times, i)
                    int _void3 = 0
                else
                    line.set_x2(ln, right_t)
                    int lbl_x = i_liq_txt_pos == "Left" ? s_time : i_liq_txt_pos == "Center" ? int(math.round((s_time + right_t) / 2)) : right_t
                    string lbl_st = i_liq_txt_pos == "Left" ? label.style_label_down : i_liq_txt_pos == "Center" ? label.style_label_down : label.style_label_left
                    label.set_x(lbl, lbl_x)
                    label.set_style(lbl, lbl_st)
                    int _void4 = 0
 
        // Update SSL positions & handle swept levels
        if array.size(s_lns) > 0
            for i = array.size(s_lns) - 1 to 0
                float prc  = array.get(s_prcs, i)
                int s_time = array.get(s_times, i)
                line ln    = array.get(s_lns, i)
                label lbl  = array.get(s_lbls, i)
                if low < prc and time > s_time
                    f_archive_swept_liq(ln, lbl, s_time, false)
                    array.remove(s_lns, i)
                    array.remove(s_lbls, i)
                    array.remove(s_prcs, i)
                    array.remove(s_times, i)
                    int _void5 = 0
                else
                    line.set_x2(ln, right_t)
                    int lbl_x = i_liq_txt_pos == "Left" ? s_time : i_liq_txt_pos == "Center" ? int(math.round((s_time + right_t) / 2)) : right_t
                    string lbl_st = i_liq_txt_pos == "Left" ? label.style_label_up : i_liq_txt_pos == "Center" ? label.style_label_up : label.style_label_left
                    label.set_x(lbl, lbl_x)
                    label.set_style(lbl, lbl_st)
                    int _void6 = 0
    int _ret = 0
 
// Storage arrays for Current TF + TF 1..4 Liquidity (using exact bar timestamps)
var array<line> bc_lns = array.new_line(), var array<label> bc_lbls = array.new_label(), var array<float> bc_prcs = array.new_float(), var array<int> bc_times = array.new_int()
var array<line> sc_lns = array.new_line(), var array<label> sc_lbls = array.new_label(), var array<float> sc_prcs = array.new_float(), var array<int> sc_times = array.new_int()
 
var array<line> b1_lns = array.new_line(), var array<label> b1_lbls = array.new_label(), var array<float> b1_prcs = array.new_float(), var array<int> b1_times = array.new_int()
var array<line> s1_lns = array.new_line(), var array<label> s1_lbls = array.new_label(), var array<float> s1_prcs = array.new_float(), var array<int> s1_times = array.new_int()
 
var array<line> b2_lns = array.new_line(), var array<label> b2_lbls = array.new_label(), var array<float> b2_prcs = array.new_float(), var array<int> b2_times = array.new_int()
var array<line> s2_lns = array.new_line(), var array<label> s2_lbls = array.new_label(), var array<float> s2_prcs = array.new_float(), var array<int> s2_times = array.new_int()
 
var array<line> b3_lns = array.new_line(), var array<label> b3_lbls = array.new_label(), var array<float> b3_prcs = array.new_float(), var array<int> b3_times = array.new_int()
var array<line> s3_lns = array.new_line(), var array<label> s3_lbls = array.new_label(), var array<float> s3_prcs = array.new_float(), var array<int> s3_times = array.new_int()
 
var array<line> b4_lns = array.new_line(), var array<label> b4_lbls = array.new_label(), var array<float> b4_prcs = array.new_float(), var array<int> b4_times = array.new_int()
var array<line> s4_lns = array.new_line(), var array<label> s4_lbls = array.new_label(), var array<float> s4_prcs = array.new_float(), var array<int> s4_times = array.new_int()
 
f_manage_liq(i_liq_curr_on, "", bc_lns, bc_lbls, bc_prcs, bc_times, sc_lns, sc_lbls, sc_prcs, sc_times)
f_manage_liq(i_liq_tf1_on, i_liq_tf1, b1_lns, b1_lbls, b1_prcs, b1_times, s1_lns, s1_lbls, s1_prcs, s1_times)
f_manage_liq(i_liq_tf2_on, i_liq_tf2, b2_lns, b2_lbls, b2_prcs, b2_times, s2_lns, s2_lbls, s2_prcs, s2_times)
f_manage_liq(i_liq_tf3_on, i_liq_tf3, b3_lns, b3_lbls, b3_prcs, b3_times, s3_lns, s3_lbls, s3_prcs, s3_times)
f_manage_liq(i_liq_tf4_on, i_liq_tf4, b4_lns, b4_lbls, b4_prcs, b4_times, s4_lns, s4_lbls, s4_prcs, s4_times)
 
// ============================================================================
// SMART MONEY CONCEPTS (CURRENT TF & RIGHT-SIDE HTF SMC OB & FVG PANEL)
// ============================================================================
var array<box> ob_bxs      = array.new_box(), var array<float> ob_tops      = array.new_float(), var array<float> ob_btms      = array.new_float(), var array<bool> ob_bulls      = array.new_bool()
var array<box> hob_bxs     = array.new_box(), var array<float> hob_tops     = array.new_float(), var array<float> hob_btms     = array.new_float(), var array<bool> hob_bulls     = array.new_bool()
var array<box> hfvg_bxs    = array.new_box(), var array<float> hfvg_tops    = array.new_float(), var array<float> hfvg_btms    = array.new_float(), var array<bool> hfvg_bulls    = array.new_bool()
var array<box> mb_bxs      = array.new_box(), var array<float> mb_tops      = array.new_float(), var array<float> mb_btms      = array.new_float(), var array<bool> mb_bulls      = array.new_bool()
var array<box> bb_bxs      = array.new_box(), var array<float> bb_tops      = array.new_float(), var array<float> bb_btms      = array.new_float(), var array<bool> bb_bulls      = array.new_bool()
var array<box> fvg_bxs     = array.new_box(), var array<float> fvg_tops     = array.new_float(), var array<float> fvg_btms     = array.new_float(), var array<bool> fvg_bulls     = array.new_bool()
var array<box> inv_bxs     = array.new_box(), var array<float> inv_tops     = array.new_float(), var array<float> inv_btms     = array.new_float(), var array<bool> inv_bulls     = array.new_bool()
var array<box> invalid_bxs = array.new_box()
 
// Helper to either delete an invalidated FVG/OB box or archive it as faded gray
f_handle_invalid_smc_box(box bx) =>
    if not na(bx)
        if i_smc_keep_invalid
            box.set_right(bx, bar_index)
            box.set_bgcolor(bx, i_smc_invalid_col)
            box.set_border_color(bx, color.new(i_smc_invalid_col, 75))
            box.set_text_color(bx, color.new(color.gray, 45))
            array.push(invalid_bxs, bx)
            if array.size(invalid_bxs) > i_smc_max_invalid
                box.delete(array.shift(invalid_bxs))
                int _v = 0
            int _void = 0
        else
            box.delete(bx)
            int _void = 0
 
// 1. Detect Current TF Fair Value Gaps (FVG)
bool bull_fvg_cond = low > high[2] and close[1] > open[1]
bool bear_fvg_cond = high < low[2] and close[1] < open[1]
 
if i_fvg_enable or i_ifvg_enable
    if bull_fvg_cond
        box b = i_fvg_enable ? box.new(bar_index - 2, low, bar_index + i_liq_extend, high[2], border_color=color.new(i_bull_fvg_col, 65), bgcolor=i_bull_fvg_col, text="FVG", text_size=size.tiny, text_color=color.white, text_halign=text.align_right) : na
        array.push(fvg_bxs, b)
        array.push(fvg_tops, low)
        array.push(fvg_btms, high[2])
        array.push(fvg_bulls, true)
        if array.size(fvg_bxs) > i_smc_max_boxes
            box old_b = array.shift(fvg_bxs)
            if not na(old_b)
                box.delete(old_b)
            array.shift(fvg_tops)
            array.shift(fvg_btms)
            array.shift(fvg_bulls)
            int _void = 0
 
    if bear_fvg_cond
        box b = i_fvg_enable ? box.new(bar_index - 2, low[2], bar_index + i_liq_extend, high, border_color=color.new(i_bear_fvg_col, 65), bgcolor=i_bear_fvg_col, text="FVG", text_size=size.tiny, text_color=color.white, text_halign=text.align_right) : na
        array.push(fvg_bxs, b)
        array.push(fvg_tops, low[2])
        array.push(fvg_btms, high)
        array.push(fvg_bulls, false)
        if array.size(fvg_bxs) > i_smc_max_boxes
            box old_b = array.shift(fvg_bxs)
            if not na(old_b)
                box.delete(old_b)
            array.shift(fvg_tops)
            array.shift(fvg_btms)
            array.shift(fvg_bulls)
            int _void = 0
 
// Update Current TF FVGs & Convert Mitigated FVGs into Inverse FVGs (iFVG) or Faded Gray
if array.size(fvg_bxs) > 0
    for i = array.size(fvg_bxs) - 1 to 0
        box bx     = array.get(fvg_bxs, i)
        float top  = array.get(fvg_tops, i)
        float btm  = array.get(fvg_btms, i)
        bool bull  = array.get(fvg_bulls, i)
        bool mitigated = bull ? close < btm : close > top
        if mitigated
            f_handle_invalid_smc_box(bx)
            array.remove(fvg_bxs, i)
            array.remove(fvg_tops, i)
            array.remove(fvg_btms, i)
            array.remove(fvg_bulls, i)
            if i_ifvg_enable
                bool inv_bull = not bull
                color inv_col = inv_bull ? i_bull_ifvg_col : i_bear_ifvg_col
                box ib = box.new(bar_index, top, bar_index + i_liq_extend, btm, border_color=color.new(inv_col, 65), bgcolor=inv_col, text="iFVG", text_size=size.tiny, text_color=color.white, text_halign=text.align_right)
                array.push(inv_bxs, ib)
                array.push(inv_tops, top)
                array.push(inv_btms, btm)
                array.push(inv_bulls, inv_bull)
                if array.size(inv_bxs) > i_smc_max_boxes
                    box.delete(array.shift(inv_bxs))
                    array.shift(inv_tops)
                    array.shift(inv_btms)
                    array.shift(inv_bulls)
                    int _void = 0
            int _void1 = 0
        else
            if not na(bx)
                box.set_right(bx, bar_index + i_liq_extend)
            int _void2 = 0
 
// Update Active iFVGs & Remove Once Re-Mitigated
if array.size(inv_bxs) > 0
    for i = array.size(inv_bxs) - 1 to 0
        box bx     = array.get(inv_bxs, i)
        float top  = array.get(inv_tops, i)
        float btm  = array.get(inv_btms, i)
        bool bull  = array.get(inv_bulls, i)
        bool inv_mitigated = bull ? close < btm : close > top
        if inv_mitigated
            box.delete(bx)
            array.remove(inv_bxs, i)
            array.remove(inv_tops, i)
            array.remove(inv_btms, i)
            array.remove(inv_bulls, i)
            int _void1 = 0
        else
            box.set_right(bx, bar_index + i_liq_extend)
            int _void2 = 0
 
// 2. Separate Structural SMC Order Blocks (SMC OB at Swing High/Low) vs Mid-Trend Mitigation Blocks
bool is_swing_low  = low[2]  <= ta.lowest(low, i_ob_swing_len)[2]
bool is_swing_high = high[2] >= ta.highest(high, i_ob_swing_len)[2]
 
bool bull_ob_cond = close[2] < open[2] and bull_fvg_cond and is_swing_low
bool bear_ob_cond = close[2] > open[2] and bear_fvg_cond and is_swing_high
 
bool bull_mb_cond = close[2] < open[2] and bull_fvg_cond and not is_swing_low
bool bear_mb_cond = close[2] > open[2] and bear_fvg_cond and not is_swing_high
 
// Create & Track Current TF Structural SMC Order Blocks (labeled 'SMC OB')
if i_ob_enable or i_bb_enable
    if bull_ob_cond
        box b = i_ob_enable ? box.new(bar_index - 2, high[2], bar_index + i_liq_extend, low[2], border_color=color.new(i_bull_ob_col, 65), bgcolor=i_bull_ob_col, text="SMC OB", text_size=size.tiny, text_color=color.white, text_halign=text.align_right) : na
        array.push(ob_bxs, b)
        array.push(ob_tops, high[2])
        array.push(ob_btms, low[2])
        array.push(ob_bulls, true)
        if array.size(ob_bxs) > i_smc_max_boxes
            box old_b = array.shift(ob_bxs)
            if not na(old_b)
                box.delete(old_b)
            array.shift(ob_tops)
            array.shift(ob_btms)
            array.shift(ob_bulls)
            int _void = 0
 
    if bear_ob_cond
        box b = i_ob_enable ? box.new(bar_index - 2, high[2], bar_index + i_liq_extend, low[2], border_color=color.new(i_bear_ob_col, 65), bgcolor=i_bear_ob_col, text="SMC OB", text_size=size.tiny, text_color=color.white, text_halign=text.align_right) : na
        array.push(ob_bxs, b)
        array.push(ob_tops, high[2])
        array.push(ob_btms, low[2])
        array.push(ob_bulls, false)
        if array.size(ob_bxs) > i_smc_max_boxes
            box old_b = array.shift(ob_bxs)
            if not na(old_b)
                box.delete(old_b)
            array.shift(ob_tops)
            array.shift(ob_btms)
            array.shift(ob_bulls)
            int _void = 0
 
// Create & Track Continuation Mitigation Blocks Separately (labeled 'Mitigation Block')
if i_mb_enable
    if bull_mb_cond
        box b = box.new(bar_index - 2, high[2], bar_index + i_liq_extend, low[2], border_color=color.new(i_bull_mb_col, 65), bgcolor=i_bull_mb_col, text="Mitigation Block", text_size=size.tiny, text_color=color.white, text_halign=text.align_right)
        array.push(mb_bxs, b)
        array.push(mb_tops, high[2])
        array.push(mb_btms, low[2])
        array.push(mb_bulls, true)
        if array.size(mb_bxs) > i_smc_max_boxes
            box.delete(array.shift(mb_bxs))
            array.shift(mb_tops)
            array.shift(mb_btms)
            array.shift(mb_bulls)
            int _void = 0
 
    if bear_mb_cond
        box b = box.new(bar_index - 2, high[2], bar_index + i_liq_extend, low[2], border_color=color.new(i_bear_mb_col, 65), bgcolor=i_bear_mb_col, text="Mitigation Block", text_size=size.tiny, text_color=color.white, text_halign=text.align_right)
        array.push(mb_bxs, b)
        array.push(mb_tops, high[2])
        array.push(mb_btms, low[2])
        array.push(mb_bulls, false)
        if array.size(mb_bxs) > i_smc_max_boxes
            box.delete(array.shift(mb_bxs))
            array.shift(mb_tops)
            array.shift(mb_btms)
            array.shift(mb_bulls)
            int _void = 0
 
// Update Active Mitigation Blocks & Handle Invalidated
if array.size(mb_bxs) > 0
    for i = array.size(mb_bxs) - 1 to 0
        box bx     = array.get(mb_bxs, i)
        float top  = array.get(mb_tops, i)
        float btm  = array.get(mb_btms, i)
        bool bull  = array.get(mb_bulls, i)
        bool mb_broken = bull ? close < btm : close > top
        if mb_broken
            f_handle_invalid_smc_box(bx)
            array.remove(mb_bxs, i)
            array.remove(mb_tops, i)
            array.remove(mb_btms, i)
            array.remove(mb_bulls, i)
            int _void1 = 0
        else
            box.set_right(bx, bar_index + i_liq_extend)
            int _void2 = 0
 
// Update Current TF SMC OBs & Convert Mitigated SMC OBs into Breaker Blocks (BB) or Faded Gray
if array.size(ob_bxs) > 0
    for i = array.size(ob_bxs) - 1 to 0
        box bx     = array.get(ob_bxs, i)
        float top  = array.get(ob_tops, i)
        float btm  = array.get(ob_btms, i)
        bool bull  = array.get(ob_bulls, i)
        bool ob_broken = bull ? close < btm : close > top
        if ob_broken
            f_handle_invalid_smc_box(bx)
            array.remove(ob_bxs, i)
            array.remove(ob_tops, i)
            array.remove(ob_btms, i)
            array.remove(ob_bulls, i)
            if i_bb_enable
                bool bb_bull = not bull
                color bb_col = bb_bull ? i_bull_bb_col : i_bear_bb_col
                box bb = box.new(bar_index, top, bar_index + i_liq_extend, btm, border_color=color.new(bb_col, 55), bgcolor=bb_col, text="Breaker", text_size=size.tiny, text_color=color.white, text_halign=text.align_right)
                array.push(bb_bxs, bb)
                array.push(bb_tops, top)
                array.push(bb_btms, btm)
                array.push(bb_bulls, bb_bull)
                if array.size(bb_bxs) > i_smc_max_boxes
                    box.delete(array.shift(bb_bxs))
                    array.shift(bb_tops)
                    array.shift(bb_btms)
                    array.shift(bb_bulls)
                    int _void = 0
            int _void1 = 0
        else
            if not na(bx)
                box.set_right(bx, bar_index + i_liq_extend)
            int _void2 = 0
 
// Update Active Breaker Blocks & Remove Once Broken
if array.size(bb_bxs) > 0
    for i = array.size(bb_bxs) - 1 to 0
        box bx     = array.get(bb_bxs, i)
        float top  = array.get(bb_tops, i)
        float btm  = array.get(bb_btms, i)
        bool bull  = array.get(bb_bulls, i)
        bool bb_broken = bull ? close < btm : close > top
        if bb_broken
            box.delete(bx)
            array.remove(bb_bxs, i)
            array.remove(bb_tops, i)
            array.remove(bb_btms, i)
            array.remove(bb_bulls, i)
            int _void1 = 0
        else
            box.set_right(bx, bar_index + i_liq_extend)
            int _void2 = 0
 
// 3. Additional Timeframe Structural SMC Order Blocks & FVGs (Placed Right of BSL/SSL Labels, Default 4H)
f_detect_htf_smc(int sw_len) =>
    bool h_bull_fvg = low > high[2] and close[1] > open[1]
    bool h_bear_fvg = high < low[2] and close[1] < open[1]
    bool h_sw_low   = low[2]  <= ta.lowest(low, sw_len)[2]
    bool h_sw_high  = high[2] >= ta.highest(high, sw_len)[2]
    bool h_bull_ob  = close[2] < open[2] and h_bull_fvg and h_sw_low
    bool h_bear_ob  = close[2] > open[2] and h_bear_fvg and h_sw_high
    float h_fvg_top = h_bull_fvg ? low : h_bear_fvg ? low[2] : na
    float h_fvg_btm = h_bull_fvg ? high[2] : h_bear_fvg ? high : na
    [h_bull_ob, h_bear_ob, high[2], low[2], h_bull_fvg, h_bear_fvg, h_fvg_top, h_fvg_btm, time[2]]
 
[htf_bull_ob, htf_bear_ob, htf_ob_top, htf_ob_btm, htf_bull_fvg, htf_bear_fvg, htf_fvg_top, htf_fvg_btm, htf_smc_t] = request.security(syminfo.tickerid, i_htf_ob_tf, f_detect_htf_smc(i_ob_swing_len), lookahead=barmerge.lookahead_off)
 
string htf_clean_lbl = f_format_tf(i_htf_ob_tf)
int htf_x1 = bar_index + i_liq_extend + i_htf_ob_offset
int htf_x2 = htf_x1 + i_htf_ob_width
 
// Right-Side Additional TF Structural SMC Order Blocks (Default 4H)
if i_htf_ob_enable
    string htf_ob_lbl = htf_clean_lbl + " SMC OB"
 
    bool new_htf_bull_ob = htf_bull_ob and not na(htf_smc_t) and (na(htf_smc_t[1]) or htf_smc_t != htf_smc_t[1])
    if new_htf_bull_ob
        box b = box.new(htf_x1, htf_ob_top, htf_x2, htf_ob_btm, border_color=color.new(i_bull_ob_col, 55), bgcolor=i_bull_ob_col, text=htf_ob_lbl, text_size=size.tiny, text_color=color.white, text_halign=text.align_center)
        array.push(hob_bxs, b)
        array.push(hob_tops, htf_ob_top)
        array.push(hob_btms, htf_ob_btm)
        array.push(hob_bulls, true)
        if array.size(hob_bxs) > i_smc_max_boxes
            box.delete(array.shift(hob_bxs))
            array.shift(hob_tops)
            array.shift(hob_btms)
            array.shift(hob_bulls)
            int _void = 0
 
    bool new_htf_bear_ob = htf_bear_ob and not na(htf_smc_t) and (na(htf_smc_t[1]) or htf_smc_t != htf_smc_t[1])
    if new_htf_bear_ob
        box b = box.new(htf_x1, htf_ob_top, htf_x2, htf_ob_btm, border_color=color.new(i_bear_ob_col, 55), bgcolor=i_bear_ob_col, text=htf_ob_lbl, text_size=size.tiny, text_color=color.white, text_halign=text.align_center)
        array.push(hob_bxs, b)
        array.push(hob_tops, htf_ob_top)
        array.push(hob_btms, htf_ob_btm)
        array.push(hob_bulls, false)
        if array.size(hob_bxs) > i_smc_max_boxes
            box.delete(array.shift(hob_bxs))
            array.shift(hob_tops)
            array.shift(hob_btms)
            array.shift(hob_bulls)
            int _void = 0
 
if array.size(hob_bxs) > 0
    for i = array.size(hob_bxs) - 1 to 0
        box bx     = array.get(hob_bxs, i)
        float top  = array.get(hob_tops, i)
        float btm  = array.get(hob_btms, i)
        bool bull  = array.get(hob_bulls, i)
        bool hob_broken = not i_htf_ob_enable or (bull ? close < btm : close > top)
        if hob_broken
            box.delete(bx)
            array.remove(hob_bxs, i)
            array.remove(hob_tops, i)
            array.remove(hob_btms, i)
            array.remove(hob_bulls, i)
            int _void1 = 0
        else
            box.set_left(bx, htf_x1)
            box.set_right(bx, htf_x2)
            int _void2 = 0
 
// Right-Side Additional TF Fair Value Gaps (Default 4H)
if i_htf_fvg_enable
    string htf_fvg_lbl = htf_clean_lbl + " FVG"
 
    bool new_htf_bull_fvg = htf_bull_fvg and not na(htf_smc_t) and (na(htf_smc_t[1]) or htf_smc_t != htf_smc_t[1])
    if new_htf_bull_fvg
        box b = box.new(htf_x1, htf_fvg_top, htf_x2, htf_fvg_btm, border_color=color.new(i_bull_fvg_col, 55), bgcolor=i_bull_fvg_col, text=htf_fvg_lbl, text_size=size.tiny, text_color=color.white, text_halign=text.align_center)
        array.push(hfvg_bxs, b)
        array.push(hfvg_tops, htf_fvg_top)
        array.push(hfvg_btms, htf_fvg_btm)
        array.push(hfvg_bulls, true)
        if array.size(hfvg_bxs) > i_smc_max_boxes
            box.delete(array.shift(hfvg_bxs))
            array.shift(hfvg_tops)
            array.shift(hfvg_btms)
            array.shift(hfvg_bulls)
            int _void = 0
 
    bool new_htf_bear_fvg = htf_bear_fvg and not na(htf_smc_t) and (na(htf_smc_t[1]) or htf_smc_t != htf_smc_t[1])
    if new_htf_bear_fvg
        box b = box.new(htf_x1, htf_fvg_top, htf_x2, htf_fvg_btm, border_color=color.new(i_bear_fvg_col, 55), bgcolor=i_bear_fvg_col, text=htf_fvg_lbl, text_size=size.tiny, text_color=color.white, text_halign=text.align_center)
        array.push(hfvg_bxs, b)
        array.push(hfvg_tops, htf_fvg_top)
        array.push(hfvg_btms, htf_fvg_btm)
        array.push(hfvg_bulls, false)
        if array.size(hfvg_bxs) > i_smc_max_boxes
            box.delete(array.shift(hfvg_bxs))
            array.shift(hfvg_tops)
            array.shift(hfvg_btms)
            array.shift(hfvg_bulls)
            int _void = 0
 
if array.size(hfvg_bxs) > 0
    for i = array.size(hfvg_bxs) - 1 to 0
        box bx     = array.get(hfvg_bxs, i)
        float top  = array.get(hfvg_tops, i)
        float btm  = array.get(hfvg_btms, i)
        bool bull  = array.get(hfvg_bulls, i)
        bool hfvg_broken = not i_htf_fvg_enable or (bull ? close < btm : close > top)
        if hfvg_broken
            box.delete(bx)
            array.remove(hfvg_bxs, i)
            array.remove(hfvg_tops, i)
            array.remove(hfvg_btms, i)
            array.remove(hfvg_bulls, i)
            int _void1 = 0
        else
            box.set_left(bx, htf_x1)
            box.set_right(bx, htf_x2)
            int _void2 = 0

← 返回列表