R3D Multi TimeFrame FVG

InR3D · study · 200 行 · 点赞 23 · TradingView 原页

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

Pine Script

//@version=6
indicator("R3D Multi TimeFrame FVG", overlay = true, max_boxes_count = 500, max_lines_count = 500)
 
// --- Configuration Inputs ---
ol             = input.int(1, "Extension (Bars)", group='Mind The Gap')
minGapSize     = input.float(1.0, "Minimum Size", group='Mind The Gap')
numFVGs        = input.int(300, "Max Elements (Boxes/Lines)", minval=50, maxval=500, group="Mind The Gap")
show_boxes     = input.bool(true, "Show FVG Boxes", group="Mind The Gap")
delete_inv     = input.bool(false, "Delete Inverted FVGs (Same TF)", group="Mind The Gap")
 
// --- Line Settings ---
show_lines     = input.bool(false, "Enable Mitigation Lines", group="Line Settings")
line_width     = input.int(1, "Line Width", minval=1, group="Line Settings")
line_style_in  = input.string("Solid", "Line Style", options=["Solid", "Dashed", "Dotted"], group="Line Settings")
line_style     = line_style_in == "Dashed" ? line.style_dashed : line_style_in == "Dotted" ? line.style_dotted : line.style_solid
 
// --- Time Filter Inputs ---
use_time       = input.bool(false, "Enable Time Filter", group="Time Filter")
sess_str       = input.session("0930-1600", "Trading Session", group="Time Filter")
sess_tz        = input.string("America/New_York", "Time Zone", group="Time Filter")
 
// --- Timeframes & Styles ---
show_4h  = input.bool(true, "4H", inline="4h", group='Timeframe Settings')
c4h_bg   = input.color(color.new(color.white, 85), "Bg", inline="4h", group='Timeframe Settings')
c4h_bd   = input.color(color.new(color.white, 100), "Border", inline="4h", group='Timeframe Settings')
 
show_1h  = input.bool(true, "1H", inline="1h", group='Timeframe Settings')
c1h_bg   = input.color(color.new(color.purple, 85), "Bg", inline="1h", group='Timeframe Settings')
c1h_bd   = input.color(color.new(color.purple, 100), "Border", inline="1h", group='Timeframe Settings')
 
show_30m = input.bool(true, "30m", inline="30m", group='Timeframe Settings')
c30m_bg  = input.color(color.new(color.orange, 85), "Bg", inline="30m", group='Timeframe Settings')
c30m_bd  = input.color(color.new(color.orange, 100), "Border", inline="30m", group='Timeframe Settings')
 
show_15m = input.bool(true, "15m", inline="15m", group='Timeframe Settings')
c15m_bg  = input.color(color.new(color.blue, 85), "Bg", inline="15m", group='Timeframe Settings')
c15m_bd  = input.color(color.new(color.blue, 100), "Border", inline="15m", group='Timeframe Settings')
 
show_5m  = input.bool(true, "5m", inline="5m", group='Timeframe Settings')
c5m_bg   = input.color(color.new(color.green, 85), "Bg", inline="5m", group='Timeframe Settings')
c5m_bd   = input.color(color.new(color.green, 100), "Border", inline="5m", group='Timeframe Settings')
 
show_4m  = input.bool(false, "4m", inline="4m", group='Timeframe Settings')
c4m_bg   = input.color(color.new(color.orange, 85), "Bg", inline="4m", group='Timeframe Settings')
c4m_bd   = input.color(color.new(color.orange, 100), "Border", inline="4m", group='Timeframe Settings')
 
show_3m  = input.bool(false, "3m", inline="3m", group='Timeframe Settings')
c3m_bg   = input.color(color.new(color.black, 85), "Bg", inline="3m", group='Timeframe Settings')
c3m_bd   = input.color(color.new(color.black, 100), "Border", inline="3m", group='Timeframe Settings')
 
show_2m  = input.bool(false, "2m", inline="2m", group='Timeframe Settings')
c2m_bg   = input.color(color.new(color.red, 85), "Bg", inline="2m", group='Timeframe Settings')
c2m_bd   = input.color(color.new(color.red, 100), "Border", inline="2m", group='Timeframe Settings')
 
show_1m  = input.bool(false, "1m", inline="1m", group='Timeframe Settings')
c1m_bg   = input.color(color.new(color.yellow, 85), "Bg", inline="1m", group='Timeframe Settings')
c1m_bd   = input.color(color.new(color.yellow, 100), "Border", inline="1m", group='Timeframe Settings')
 
borderWidth  = input.int(1, "Border Width", minval=1, group='Timeframe Settings')
usrTextColor = input.color(color.new(color.black, 50), "Text Color", inline='text', group='Text Style')
 
// --- Data Structure ---
type FVGElement
    box    bx
    line   ln
    float  top
    float  bottom
    bool   is_bullish
    bool   line_active
    string tf_label
    int    created_bar_time
 
var FVGElement[] fvg_elements = array.new<FVGElement>()
 
// --- Detection Function ---
fvg_check() =>
    float b_sz = low[1] - high[3]
    float s_sz = low[3] - high[1]
    bool is_b  = low[1] > high[3] and b_sz > minGapSize
    bool is_s  = low[3] > high[1] and s_sz > minGapSize
    // Returns FVG data + confirmed bar close in its own TF (close[1] and time_close[1])
    [is_b, is_s, is_b ? low[1] : low[3], is_b ? high[3] : high[1], is_b ? b_sz : s_sz, time_close[2], close[1], time_close[1]]
 
// --- Fetch MTF Data with lookahead_off ---
[b4, s4, b4_t, b4_b, sz4, t4, c4, tc4]         = request.security(syminfo.tickerid, "240", fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b1, s1, b1_t, b1_b, sz1, t1, c1, tc1]         = request.security(syminfo.tickerid, "60",  fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b30, s30, b30_t, b30_b, sz30, t30, c30, tc30] = request.security(syminfo.tickerid, "30",  fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b15, s15, b15_t, b15_b, sz15, t15, c15, tc15] = request.security(syminfo.tickerid, "15",  fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b5, s5, b5_t, b5_b, sz5, t5, c5, tc5]         = request.security(syminfo.tickerid, "5",   fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b6, s6, b6_t, b6_b, sz6, t6, c6, tc6]         = request.security(syminfo.tickerid, "4",   fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b3, s3, b3_t, b3_b, sz3, t3, c3, tc3]         = request.security(syminfo.tickerid, "3",   fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b2, s2, b2_t, b2_b, sz2, t2, c2, tc2]         = request.security(syminfo.tickerid, "2",   fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
[b7, s7, b7_t, b7_b, sz7, t7, c7, tc7]         = request.security(syminfo.tickerid, "1",   fvg_check(), barmerge.gaps_off, barmerge.lookahead_off)
 
render_fvg(bool is_b, bool is_s, float top_p, float bot_p, float sz, string tf, int startTime, color bgColor, color bdColor, int htf_mins, int barCloseTime) =>
    if is_b or is_s
        box bx = na
        line l = na
        
        if show_boxes
            int extensionTime = startTime + (ol * htf_mins * 60000)
            bx := box.new(left=startTime, top=top_p, right=extensionTime, bottom=bot_p, 
                 xloc=xloc.bar_time,
                 bgcolor=bgColor, 
                 border_color=bdColor, 
                 border_width=borderWidth,
                 text=tf, // + " (" + str.tostring(sz, "#.#") + ")", 
                 text_size=size.tiny, 
                 text_color=usrTextColor, 
                 text_halign=text.align_right)
 
        if show_lines
            float target_p = is_b ? top_p : bot_p
            color line_col = color.new(bgColor, 0)
            
            l := line.new(x1=startTime, y1=target_p, x2=time, y2=target_p,
                 xloc=xloc.bar_time,
                 color=line_col,
                 width=line_width,
                 style=line_style)
        
        array.push(fvg_elements, FVGElement.new(bx, l, top_p, bot_p, is_b, show_lines, tf, barCloseTime))
        
        if array.size(fvg_elements) > numFVGs
            FVGElement old = array.shift(fvg_elements)
            if not na(old.bx)
                box.delete(old.bx)
            if not na(old.ln)
                line.delete(old.ln)
 
// Session Filter
in_session = not use_time or not na(time(timeframe.period, sess_str, sess_tz))
 
// Trigger on change of FVG timestamp
process_tf(bool show, bool is_b, bool is_s, float top_p, float bot_p, float sz, string tf, int t_start, color bg, color bd, int mins, int tc) =>
    if show and (ta.change(t_start) != 0) and in_session
        render_fvg(is_b, is_s, top_p, bot_p, sz, tf, t_start, bg, bd, mins, tc)
 
process_tf(show_4h,  b4,  s4,  b4_t,  b4_b,  sz4,  "4H",  t4,  c4h_bg,  c4h_bd,  240, tc4)
process_tf(show_1h,  b1,  s1,  b1_t,  b1_b,  sz1,  "1H",  t1,  c1h_bg,  c1h_bd,  60,  tc1)
process_tf(show_30m, b30, s30, b30_t, b30_b, sz30, "30m", t30, c30m_bg, c30m_bd, 30,  tc30)
process_tf(show_15m, b15, s15, b15_t, b15_b, sz15, "15m", t15, c15m_bg, c15m_bd, 15,  tc15)
process_tf(show_5m,  b5,  s5,  b5_t,  b5_b,  sz5,  "5m",  t5,  c5m_bg,  c5m_bd,  5,   tc5)
process_tf(show_4m,  b6,  s6,  b6_t,  b6_b,  sz6,  "4m",  t6,  c4m_bg,  c4m_bd,  4,   tc6)
process_tf(show_3m,  b3,  s3,  b3_t,  b3_b,  sz3,  "3m",  t3,  c3m_bg,  c3m_bd,  3,   tc3)
process_tf(show_2m,  b2,  s2,  b2_t,  b2_b,  sz2,  "2m",  t2,  c2m_bg,  c2m_bd,  2,   tc2)
process_tf(show_1m,  b7,  s7,  b7_t,  b7_b,  sz7,  "1m",  t7,  c1m_bg,  c1m_bd,  1,   tc7)
 
// Detect when a new candle closes in each specific timeframe
bool new_4h  = ta.change(tc4)  != 0
bool new_1h  = ta.change(tc1)  != 0
bool new_30m = ta.change(tc30) != 0
bool new_15m = ta.change(tc15) != 0
bool new_5m  = ta.change(tc5)  != 0
bool new_4m  = ta.change(tc6)  != 0
bool new_3m  = ta.change(tc3)  != 0
bool new_2m  = ta.change(tc2)  != 0
bool new_1m  = ta.change(tc7)  != 0
 
tf_inv_data(string tf) =>
    switch tf
        "4H"  => [new_4h,  c4,  tc4]
        "1H"  => [new_1h,  c1,  tc1]
        "30m" => [new_30m, c30, tc30]
        "15m" => [new_15m, c15, tc15]
        "5m"  => [new_5m,  c5,  tc5]
        "4m"  => [new_4m,  c6,  tc6]
        "3m"  => [new_3m,  c3,  tc3]
        "2m"  => [new_2m,  c2,  tc2]
        "1m"  => [new_1m,  c7,  tc7]
        => [false, na, na]
 
// --- Update, Mitigate & Delete Inverted FVGs ---
if array.size(fvg_elements) > 0
    for i = array.size(fvg_elements) - 1 to 0
        FVGElement item = array.get(fvg_elements, i)
        
        [is_new_bar, htf_close, htf_tc] = tf_inv_data(item.tf_label)
        
        // Check if a candle closed in the FVG's own timeframe (after its creation)
        bool is_inverted = false
        if delete_inv and is_new_bar and htf_tc > item.created_bar_time
            is_inverted := item.is_bullish ? (htf_close < item.bottom) : (htf_close > item.top)
        
        if is_inverted
            if not na(item.bx)
                box.delete(item.bx)
            if not na(item.ln)
                line.delete(item.ln)
            array.remove(fvg_elements, i)
        else
            // Update and mitigate active lines while the FVG has not been inverted
            if item.line_active and not na(item.ln)
                float target_p = item.is_bullish ? item.top : item.bottom
                bool hit = item.is_bullish ? (low <= target_p) : (high >= target_p)
                
                line.set_x2(item.ln, time)
                
                if hit
                    item.line_active := false

← 返回列表