Opening Hour Swing H/L + FVG (ET)

IonaMilcah · study · 94 行 · 点赞 2 · TradingView 原页

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

Pine Script

//@version=6
indicator("Opening Hour Swing H/L + FVG (ET)", shorttitle="OH Swing+FVG", overlay=true, max_lines_count=500, max_boxes_count=500)
 
// ── Inputs ────────────────────────────────────────────────
tz        = "America/New_York"
winSess   = input.session("0900-1000", "Range window (ET)", tooltip="Hour used to find the swing high / swing low. Change to 1000-1100 if you want the hour starting at 10:00.")
daySess   = input.session("0900-1600", "Track lines & FVGs until (ET)")
lineWidth = input.int(2, "Line width", minval=1, maxval=5)
highCol   = input.color(color.red,   "Swing high line")
lowCol    = input.color(color.green, "Swing low line")
bullCol   = input.color(color.new(color.teal, 80), "Bullish FVG")
bearCol   = input.color(color.new(color.orange, 80), "Bearish FVG")
minTicks  = input.int(0, "Min FVG size (ticks)", minval=0)
hideMit   = input.bool(false, "Remove FVG once filled")
 
// ── Session logic (Eastern Time) ──────────────────────────
inWin = not na(time(timeframe.period, winSess, tz))
inDay = not na(time(timeframe.period, daySess, tz))
newWin = inWin and not inWin[1]
 
var float hiP = na
var float loP = na
var int   hiT = na
var int   loT = na
var line  hiLn = na
var line  loLn = na
var bool  active = false
 
if newWin
    hiP := high
    loP := low
    hiT := time
    loT := time
    hiLn := line.new(time, high, time, high, xloc=xloc.bar_time, color=highCol, width=lineWidth)
    loLn := line.new(time, low,  time, low,  xloc=xloc.bar_time, color=lowCol,  width=lineWidth)
    active := true
else if inWin and active
    if high > hiP
        hiP := high
        hiT := time
        line.set_xy1(hiLn, hiT, hiP)
    if low < loP
        loP := low
        loT := time
        line.set_xy1(loLn, loT, loP)
 
if active and inDay
    line.set_xy2(hiLn, time, hiP)
    line.set_xy2(loLn, time, loP)
 
if active and not inDay
    active := false
 
// ── Fair value gaps (only after the range window closes) ──
var box[]  boxes = array.new<box>()
var bool[] bulls = array.new<bool>()
 
postWin = active and not inWin
minGap  = minTicks * syminfo.mintick
 
bullFVG = postWin and low > high[2] and (low - high[2]) >= minGap
bearFVG = postWin and high < low[2] and (low[2] - high) >= minGap
 
if bullFVG
    array.push(boxes, box.new(time[2], low, time, high[2], xloc=xloc.bar_time, border_color=na, bgcolor=bullCol))
    array.push(bulls, true)
if bearFVG
    array.push(boxes, box.new(time[2], low[2], time, high, xloc=xloc.bar_time, border_color=na, bgcolor=bearCol))
    array.push(bulls, false)
 
// Extend open FVGs until filled or the day ends
if array.size(boxes) > 0
    for i = array.size(boxes) - 1 to 0
        b = array.get(boxes, i)
        isBull = array.get(bulls, i)
        top = box.get_top(b)
        bot = box.get_bottom(b)
        bool done = false
        if (isBull and close < bot) or (not isBull and close > top)
            if hideMit
                box.delete(b)
            else
                box.set_right(b, time)
            done := true
        else if not inDay
            done := true
        else
            box.set_right(b, time)
        if done
            array.remove(boxes, i)
            array.remove(bulls, i)
 
alertcondition(bullFVG, "Bullish FVG formed", "Bullish FVG formed after opening-hour range")
alertcondition(bearFVG, "Bearish FVG formed", "Bearish FVG formed after opening-hour range")

← 返回列表