Liquidity Indicator 1:1 - iFVGRobin

RobinDaBankzFX · study · 842 行 · 点赞 22 · TradingView 原页

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

Pine Script

//@version=6
indicator("Liquidity Indicator 1:1 - iFVGRobin", overlay=true, max_lines_count=500, max_labels_count=250, max_boxes_count=150, max_bars_back=2000, dynamic_requests=true)
//════════════════════════════════════════════
// INPUTS
//════════════════════════════════════════════
showDaily = input.bool(true, "PDH / PDL", group="Key Levels")
showWeekly = input.bool(true, "PWH / PWL", group="Key Levels")
showMonthly = input.bool(true, "PMH / PML", group="Key Levels")
showATH = input.bool(true, "ATH", group="Key Levels")
showAsia = input.bool(true, "Asia Open / High / Low", group="Sessions")
showLondon = input.bool(true, "London Open / High / Low", group="Sessions")
showNewYork = input.bool(true, "New York Open / High / Low", group="Sessions")
show0830 = input.bool(true, "8:30 AM News Release", group="New York Time Levels")
show0930 = input.bool(true, "9:30 AM", group="New York Time Levels")
show1000 = input.bool(true, "10AM", group="New York Time Levels")
show4HFVG = input.bool(true, "Show 4H FVG", group="Fair Value Gaps")
showFVGMidline = input.bool(true, "Show 50% Midline", group="Fair Value Gaps")
fvgATRMultiplier = input.float(3.0, "Distance Filter (Daily ATR)", minval=0.1, step=0.1, group="Fair Value Gaps")
fvgPerSide = input.int(5, "Maximum FVGs Above / Below", minval=1, maxval=10, group="Fair Value Gaps")
fvgTransparency = input.int(94, "FVG Fill Transparency", minval=0, maxval=100, group="Fair Value Gaps")
fvgHistoryLimit = input.int(250, "Maximum Stored FVGs", minval=25, maxval=500, group="Fair Value Gaps")
fvgDashLength = input.float(0.3, "50% Dash Length (Candles)", minval=0.1, maxval=2.0, step=0.1, group="Fair Value Gaps")
fvgDashSpacing = input.float(2.0, "50% Dash Spacing (Candles)", minval=0.5, maxval=8.0, step=0.5, group="Fair Value Gaps")
fvgTextClearance = input.float(1.5, "FVG Text Clearance (Candles)", minval=0.5, maxval=5.0, step=0.5, group="Fair Value Gaps")
maxFVGDashes = input.int(300, "Maximum Visible 50% Dashes", minval=20, maxval=350, group="Fair Value Gaps")
showEQ = input.bool(true, "Equal Highs / Lows", group="Equal Highs & Lows")
pivotLeft = input.int(3, "Pivot Left", minval=1, maxval=20, group="Equal Highs & Lows")
pivotRight = input.int(3, "Pivot Right", minval=1, maxval=20, group="Equal Highs & Lows")
eqToleranceTicks = input.int(8, "EQ Tolerance (Ticks)", minval=1, maxval=100, group="Equal Highs & Lows")
maxPivotGap = input.int(100, "Maximum Bars Between Pivots", minval=5, maxval=1000, group="Equal Highs & Lows")
maxEQClusters = input.int(20, "Maximum EQ Groups", minval=2, maxval=40, group="Equal Highs & Lows")
rightOffset = input.int(5, "FVG Extension (Candles)", minval=1, maxval=50, group="Appearance")
keyLevelExtraExtension = input.int(100, "Key Level Extra Extension", minval=1, maxval=300, group="Appearance")
textOffsetTicks = input.int(2, "Text Distance From Line (Ticks)", minval=0, maxval=100, group="Appearance")
labelHorizontalOffset = input.int(3, "Label Horizontal Offset (Candles)", minval=0, maxval=50, group="Appearance")
textSizeInput = input.string("Tiny", "Text Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group="Appearance")
//════════════════════════════════════════════
// GLOBAL SETTINGS
//════════════════════════════════════════════
string nyTimezone = "America/New_York"
string osloTimezone = "Europe/Oslo"
color levelColor = color.rgb(190, 190, 190)
color textColor = color.black
float eqTolerance = eqToleranceTicks * syminfo.mintick
float textDistance = textOffsetTicks * syminfo.mintick
int chartSeconds = timeframe.in_seconds(timeframe.period)
int chartMs = chartSeconds * 1000
int keyLevelEndOffset = rightOffset + keyLevelExtraExtension
bool sessionSupported = timeframe.isintraday and chartSeconds <= 14400
bool display4HFVG = show4HFVG and chartSeconds <= 14400
//════════════════════════════════════════════
// LABEL HELPERS
//════════════════════════════════════════════
labelTextSize() =>
    switch textSizeInput
        "Tiny" => size.tiny
        "Small" => size.small
        "Normal" => size.normal
        "Large" => size.large
        => size.huge
labelPrice(float price, bool isHigh) =>
    isHigh ? price + textDistance : price - textDistance
labelX(int endTime) =>
    endTime - chartMs * labelHorizontalOffset
createLevelLabel(int xTime, float price, string txt, bool isHigh) =>
    label.new(
         x=labelX(xTime),
         y=labelPrice(price, isHigh),
         text=txt,
         xloc=xloc.bar_time,
         yloc=yloc.price,
         style=label.style_none,
         color=color.new(levelColor, 100),
         textcolor=textColor,
         textalign=text.align_right,
         size=labelTextSize(),
         text_formatting=text.format_italic)
//════════════════════════════════════════════
// DAILY / WEEKLY / MONTHLY STORAGE
//════════════════════════════════════════════
var array<float> prices = array.new_float()
var array<bool> isHighLevels = array.new_bool()
var array<bool> mitigatedLevels = array.new_bool()
var array<int> types = array.new_int()
var array<line> lines = array.new_line()
var array<label> labels = array.new_label()
createLevel(float price, string txt, bool isHigh, int typ, int originTime) =>
    if not na(price) and not na(originTime)
        int endTime = time + chartMs * keyLevelEndOffset
        line ln = line.new(
             x1=originTime, y1=price,
             x2=endTime, y2=price,
             xloc=xloc.bar_time,
             color=levelColor, width=1)
        label lb = createLevelLabel(endTime, price, txt, isHigh)
        array.push(prices, price)
        array.push(isHighLevels, isHigh)
        array.push(mitigatedLevels, false)
        array.push(types, typ)
        array.push(lines, ln)
        array.push(labels, lb)
    0
deleteLevel(int i) =>
    line.delete(array.get(lines, i))
    label.delete(array.get(labels, i))
    array.remove(prices, i)
    array.remove(isHighLevels, i)
    array.remove(mitigatedLevels, i)
    array.remove(types, i)
    array.remove(lines, i)
    array.remove(labels, i)
    0
removeType(int typ) =>
    if array.size(prices) > 0
        for i = array.size(prices) - 1 to 0
            if array.get(types, i) == typ
                deleteLevel(i)
    0
removeMitigated() =>
    if array.size(prices) > 0
        for i = array.size(prices) - 1 to 0
            if array.get(mitigatedLevels, i)
                deleteLevel(i)
    0
//════════════════════════════════════════════
// EQUAL HIGHS / LOWS STORAGE
//════════════════════════════════════════════
var array<float> eqPrices = array.new_float()
var array<bool> eqIsHigh = array.new_bool()
var array<int> eqCounts = array.new_int()
var array<int> eqStartTimes = array.new_int()
var array<int> eqLastBars = array.new_int()
var array<int> eqCreatedBars = array.new_int()
var array<line> eqLines = array.new_line()
var array<label> eqLabels = array.new_label()
deleteEQ(int i) =>
    line ln = array.get(eqLines, i)
    label lb = array.get(eqLabels, i)
    if not na(ln)
        line.delete(ln)
    if not na(lb)
        label.delete(lb)
    array.remove(eqPrices, i)
    array.remove(eqIsHigh, i)
    array.remove(eqCounts, i)
    array.remove(eqStartTimes, i)
    array.remove(eqLastBars, i)
    array.remove(eqCreatedBars, i)
    array.remove(eqLines, i)
    array.remove(eqLabels, i)
    0
registerPivot(float pivotPrice, int pivotTime, int pivotBar, bool isHigh) =>
    int match = -1
    float bestDistance = na
    if array.size(eqPrices) > 0
        for i = 0 to array.size(eqPrices) - 1
            float anchor = array.get(eqPrices, i)
            int gap = pivotBar - array.get(eqLastBars, i)
            float distance = math.abs(pivotPrice - anchor)
            bool directionValid = isHigh ? pivotPrice <= anchor : pivotPrice >= anchor
            bool valid = array.get(eqIsHigh, i) == isHigh and directionValid and gap > 0 and gap <= maxPivotGap and distance <= eqTolerance
            if valid and (na(bestDistance) or distance < bestDistance)
                match := i
                bestDistance := distance
    if match >= 0
        int count = array.get(eqCounts, match) + 1
        float anchor = array.get(eqPrices, match)
        array.set(eqCounts, match, count)
        array.set(eqLastBars, match, pivotBar)
        string txt = isHigh ? (count >= 3 ? "EQ3H" : "EQH") : (count >= 3 ? "EQ3L" : "EQL")
        line ln = array.get(eqLines, match)
        label lb = array.get(eqLabels, match)
        if na(ln)
            ln := line.new(
                 x1=array.get(eqStartTimes, match),
                 y1=anchor,
                 x2=time + chartMs * keyLevelEndOffset,
                 y2=anchor,
                 xloc=xloc.bar_time,
                 color=levelColor,
                 width=1,
                 style=line.style_dashed)
            lb := createLevelLabel(
                 time + chartMs * keyLevelEndOffset,
                 anchor, txt, isHigh)
            array.set(eqLines, match, ln)
            array.set(eqLabels, match, lb)
        else
            label.set_text(lb, txt)
    else
        array.push(eqPrices, pivotPrice)
        array.push(eqIsHigh, isHigh)
        array.push(eqCounts, 1)
        array.push(eqStartTimes, pivotTime)
        array.push(eqLastBars, pivotBar)
        array.push(eqCreatedBars, bar_index)
        array.push(eqLines, na)
        array.push(eqLabels, na)
        while array.size(eqPrices) > maxEQClusters
            deleteEQ(0)
    0
//════════════════════════════════════════════
// PERIOD HIGH / LOW TRACKING
//════════════════════════════════════════════
var float dayHigh = na
var float dayLow = na
var int dayHighTime = na
var int dayLowTime = na
var float weekHigh = na
var float weekLow = na
var int weekHighTime = na
var int weekLowTime = na
var float monthHigh = na
var float monthLow = na
var int monthHighTime = na
var int monthLowTime = na
if barstate.isfirst
    dayHigh := high
    dayLow := low
    dayHighTime := time
    dayLowTime := time
    weekHigh := high
    weekLow := low
    weekHighTime := time
    weekLowTime := time
    monthHigh := high
    monthLow := low
    monthHighTime := time
    monthLowTime := time
else
    if timeframe.change("D")
        removeMitigated()
        removeType(0)
        if showDaily
            createLevel(dayHigh, "PDH", true, 0, dayHighTime)
            createLevel(dayLow, "PDL", false, 0, dayLowTime)
        dayHigh := high
        dayLow := low
        dayHighTime := time
        dayLowTime := time
    else
        if high > dayHigh
            dayHigh := high
            dayHighTime := time
        if low < dayLow
            dayLow := low
            dayLowTime := time
    if timeframe.change("W")
        removeType(1)
        if showWeekly
            createLevel(weekHigh, "PWH", true, 1, weekHighTime)
            createLevel(weekLow, "PWL", false, 1, weekLowTime)
        weekHigh := high
        weekLow := low
        weekHighTime := time
        weekLowTime := time
    else
        if high > weekHigh
            weekHigh := high
            weekHighTime := time
        if low < weekLow
            weekLow := low
            weekLowTime := time
    if timeframe.change("M")
        removeType(2)
        if showMonthly
            createLevel(monthHigh, "PMH", true, 2, monthHighTime)
            createLevel(monthLow, "PML", false, 2, monthLowTime)
        monthHigh := high
        monthLow := low
        monthHighTime := time
        monthLowTime := time
    else
        if high > monthHigh
            monthHigh := high
            monthHighTime := time
        if low < monthLow
            monthLow := low
            monthLowTime := time
//════════════════════════════════════════════
// EQUAL HIGH / LOW DETECTION
//════════════════════════════════════════════
float pivotHigh = ta.pivothigh(high, pivotLeft, pivotRight)
float pivotLow = ta.pivotlow(low, pivotLeft, pivotRight)
if showEQ
    if not na(pivotHigh)
        registerPivot(pivotHigh, time[pivotRight], bar_index - pivotRight, true)
    if not na(pivotLow)
        registerPivot(pivotLow, time[pivotRight], bar_index - pivotRight, false)
if array.size(eqPrices) > 0
    for i = array.size(eqPrices) - 1 to 0
        if bar_index > array.get(eqCreatedBars, i)
            float anchor = array.get(eqPrices, i)
            bool isHigh = array.get(eqIsHigh, i)
            bool taken = isHigh ? high > anchor : low < anchor
            if taken
                deleteEQ(i)
//════════════════════════════════════════════
// KEY LEVEL MITIGATION
//════════════════════════════════════════════
// Keep swept levels visible as dashed until the next trading day.
if array.size(prices) > 0
    for i = array.size(prices) - 1 to 0
        if not array.get(mitigatedLevels, i)
            float price = array.get(prices, i)
            bool isHigh = array.get(isHighLevels, i)
            bool taken = isHigh ? high > price : low < price
            if taken
                array.set(mitigatedLevels, i, true)
                line.set_style(array.get(lines, i), line.style_dashed)
 
//════════════════════════════════════════════
// SESSION TIME DEFINITIONS
//════════════════════════════════════════════
bool asiaInSession = sessionSupported and not na(time(timeframe.period, "1700-0200", nyTimezone))
bool londonInSession = sessionSupported and not na(time(timeframe.period, "0800-1600", osloTimezone))
bool newYorkInSession = sessionSupported and not na(time(timeframe.period, "1300-1600", osloTimezone))
bool asiaStart = asiaInSession and not asiaInSession[1]
bool asiaEnd = not asiaInSession and asiaInSession[1]
bool londonStart = londonInSession and not londonInSession[1]
bool londonEnd = not londonInSession and londonInSession[1]
bool newYorkStart = newYorkInSession and not newYorkInSession[1]
bool newYorkEnd = not newYorkInSession and newYorkInSession[1]
//════════════════════════════════════════════
// SESSION STORAGE
// 0 = Asia, 1 = London, 2 = New York
//════════════════════════════════════════════
var array<float> sessionOpens = array.new_float(3, na)
var array<float> sessionHighs = array.new_float(3, na)
var array<float> sessionLows = array.new_float(3, na)
var array<int> sessionOpenTimes = array.new_int(3, na)
var array<int> sessionHighTimes = array.new_int(3, na)
var array<int> sessionLowTimes = array.new_int(3, na)
var array<bool> sessionHighMitigated = array.new_bool(3, false)
var array<bool> sessionLowMitigated = array.new_bool(3, false)
var array<line> sessionOpenLines = array.new_line(3, na)
var array<line> sessionHighLines = array.new_line(3, na)
var array<line> sessionLowLines = array.new_line(3, na)
var array<label> sessionOpenLabels = array.new_label(3, na)
var array<label> sessionHighLabels = array.new_label(3, na)
var array<label> sessionLowLabels = array.new_label(3, na)
sessionName(int index) =>
    switch index
        0 => "Asia"
        1 => "London"
        => "New York"
sessionEnabled(int index) =>
    switch index
        0 => showAsia
        1 => showLondon
        => showNewYork
clearSessionDrawings(int index) =>
    line openLn = array.get(sessionOpenLines, index)
    line highLn = array.get(sessionHighLines, index)
    line lowLn = array.get(sessionLowLines, index)
    label openLb = array.get(sessionOpenLabels, index)
    label highLb = array.get(sessionHighLabels, index)
    label lowLb = array.get(sessionLowLabels, index)
    if not na(openLn)
        line.delete(openLn)
    if not na(highLn)
        line.delete(highLn)
    if not na(lowLn)
        line.delete(lowLn)
    if not na(openLb)
        label.delete(openLb)
    if not na(highLb)
        label.delete(highLb)
    if not na(lowLb)
        label.delete(lowLb)
    array.set(sessionOpenLines, index, na)
    array.set(sessionHighLines, index, na)
    array.set(sessionLowLines, index, na)
    array.set(sessionOpenLabels, index, na)
    array.set(sessionHighLabels, index, na)
    array.set(sessionLowLabels, index, na)
    0
startSession(int index) =>
    clearSessionDrawings(index)
    array.set(sessionOpens, index, open)
    array.set(sessionHighs, index, high)
    array.set(sessionLows, index, low)
    array.set(sessionOpenTimes, index, time)
    array.set(sessionHighTimes, index, time)
    array.set(sessionLowTimes, index, time)
    array.set(sessionHighMitigated, index, false)
    array.set(sessionLowMitigated, index, false)
    if sessionEnabled(index)
        int endTime = time + chartMs * keyLevelEndOffset
        line ln = line.new(
             x1=time, y1=open,
             x2=endTime, y2=open,
             xloc=xloc.bar_time,
             color=levelColor,
             width=1)
        label lb = createLevelLabel(
             endTime, open,
             sessionName(index) + " Open",
             true)
        array.set(sessionOpenLines, index, ln)
        array.set(sessionOpenLabels, index, lb)
    0
trackSession(int index) =>
    float oldHigh = array.get(sessionHighs, index)
    float oldLow = array.get(sessionLows, index)
    if na(oldHigh) or high > oldHigh
        array.set(sessionHighs, index, high)
        array.set(sessionHighTimes, index, time)
    if na(oldLow) or low < oldLow
        array.set(sessionLows, index, low)
        array.set(sessionLowTimes, index, time)
    0
closeSession(int index) =>
    line openLn = array.get(sessionOpenLines, index)
    label openLb = array.get(sessionOpenLabels, index)
    if not na(openLn)
        line.delete(openLn)
    if not na(openLb)
        label.delete(openLb)
    array.set(sessionOpenLines, index, na)
    array.set(sessionOpenLabels, index, na)
    if sessionEnabled(index)
        int endTime = time + chartMs * keyLevelEndOffset
        float sHigh = array.get(sessionHighs, index)
        float sLow = array.get(sessionLows, index)
        int highTime = array.get(sessionHighTimes, index)
        int lowTime = array.get(sessionLowTimes, index)
        if not na(sHigh) and not na(highTime)
            line highLn = line.new(
                 x1=highTime, y1=sHigh,
                 x2=endTime, y2=sHigh,
                 xloc=xloc.bar_time,
                 color=levelColor,
                 width=1)
            label highLb = createLevelLabel(
                 endTime, sHigh,
                 sessionName(index) + " High",
                 true)
            array.set(sessionHighLines, index, highLn)
            array.set(sessionHighLabels, index, highLb)
        if not na(sLow) and not na(lowTime)
            line lowLn = line.new(
                 x1=lowTime, y1=sLow,
                 x2=endTime, y2=sLow,
                 xloc=xloc.bar_time,
                 color=levelColor,
                 width=1)
            label lowLb = createLevelLabel(
                 endTime, sLow,
                 sessionName(index) + " Low",
                 false)
            array.set(sessionLowLines, index, lowLn)
            array.set(sessionLowLabels, index, lowLb)
    0
mitigateSession(int index) =>
    line highLn = array.get(sessionHighLines, index)
    line lowLn = array.get(sessionLowLines, index)
    float sHigh = array.get(sessionHighs, index)
    float sLow = array.get(sessionLows, index)
 
    if not na(highLn) and not na(sHigh) and not array.get(sessionHighMitigated, index)
        if high > sHigh
            array.set(sessionHighMitigated, index, true)
            line.set_style(highLn, line.style_dashed)
 
    if not na(lowLn) and not na(sLow) and not array.get(sessionLowMitigated, index)
        if low < sLow
            array.set(sessionLowMitigated, index, true)
            line.set_style(lowLn, line.style_dashed)
    0
 
//════════════════════════════════════════════
// NY TIMED LEVELS
//════════════════════════════════════════════
var array<float> nyPrices = array.new_float(3, na)
var array<int> nyCreatedBars = array.new_int(3, na)
var array<bool> nyMitigated = array.new_bool(3, false)
var array<line> nyLines = array.new_line(3, na)
var array<label> nyLabels = array.new_label(3, na)
createNYLevel(int index, string txt, bool enabled) =>
    line oldLine = array.get(nyLines, index)
    label oldLabel = array.get(nyLabels, index)
    if not na(oldLine)
        line.delete(oldLine)
    if not na(oldLabel)
        label.delete(oldLabel)
    array.set(nyLines, index, na)
    array.set(nyLabels, index, na)
    array.set(nyPrices, index, open)
    array.set(nyCreatedBars, index, bar_index)
    array.set(nyMitigated, index, false)
    if enabled
        int endTime = time + chartMs * keyLevelEndOffset
        line ln = line.new(
             x1=time, y1=open,
             x2=endTime, y2=open,
             xloc=xloc.bar_time,
             color=levelColor,
             width=1)
        label lb = createLevelLabel(endTime, open, txt, true)
        array.set(nyLines, index, ln)
        array.set(nyLabels, index, lb)
    0
resetNYLevels() =>
    for i = 0 to 2
        line ln = array.get(nyLines, i)
        label lb = array.get(nyLabels, i)
        if not na(ln)
            line.delete(ln)
        if not na(lb)
            label.delete(lb)
        array.set(nyLines, i, na)
        array.set(nyLabels, i, na)
        array.set(nyPrices, i, na)
        array.set(nyCreatedBars, i, na)
        array.set(nyMitigated, i, false)
    0
//════════════════════════════════════════════
// SESSION EXECUTION
//════════════════════════════════════════════
if asiaStart
    resetNYLevels()
    // New trading day (17:00 New York): remove prior session highs/lows.
    clearSessionDrawings(1)
    clearSessionDrawings(2)
    startSession(0)
else if asiaInSession
    trackSession(0)
else if asiaEnd
    closeSession(0)
else
    mitigateSession(0)
if londonStart
    startSession(1)
else if londonInSession
    trackSession(1)
else if londonEnd
    closeSession(1)
else
    mitigateSession(1)
if newYorkStart
    startSession(2)
else if newYorkInSession
    trackSession(2)
else if newYorkEnd
    closeSession(2)
else
    mitigateSession(2)
//════════════════════════════════════════════
// NY TIME LEVELS
//════════════════════════════════════════════
int nyHour = hour(time, nyTimezone)
int nyMinute = minute(time, nyTimezone)
bool bar0830 = sessionSupported and nyHour == 8 and nyMinute == 30
bool bar0930 = sessionSupported and nyHour == 9 and nyMinute == 30
bool bar1000 = sessionSupported and nyHour == 10 and nyMinute == 0
if bar0830
    createNYLevel(0, "8:30 AM News Release", show0830)
if bar0930
    createNYLevel(1, "9:30 AM", show0930)
if bar1000
    createNYLevel(2, "10AM", show1000)
if sessionSupported
    for i = 0 to 2
        line ln = array.get(nyLines, i)
        float price = array.get(nyPrices, i)
        int createdBar = array.get(nyCreatedBars, i)
        if not na(ln) and not array.get(nyMitigated, i) and not na(createdBar)
            if bar_index > createdBar
                bool tradedThrough = low < price and high > price
                bool crossedFromAbove = close[1] > price and low < price
                bool crossedFromBelow = close[1] < price and high > price
                if tradedThrough or crossedFromAbove or crossedFromBelow
                    array.set(nyMitigated, i, true)
                    line.set_style(ln, line.style_dashed)
//════════════════════════════════════════════
// 4H FVG STORAGE
//════════════════════════════════════════════
var array<float> fvgTops = array.new_float()
var array<float> fvgBottoms = array.new_float()
var array<bool> fvgBullish = array.new_bool()
var array<int> fvgStartTimes = array.new_int()
var array<box> visibleBoxes = array.new_box()
var array<line> visibleMidlines = array.new_line()
var array<label> visibleFVGLabels = array.new_label()
var int lastProcessed4HTime = 0
removeFVG(int i) =>
    array.remove(fvgTops, i)
    array.remove(fvgBottoms, i)
    array.remove(fvgBullish, i)
    array.remove(fvgStartTimes, i)
    0
process4HFVGCandle(int candleTime, int startTime, float candleLow, float candleHigh, float firstHigh, float firstLow, int previousTime) =>
    int updatedTime = previousTime
    if not na(candleTime) and not na(candleLow) and not na(candleHigh)
        if candleTime > previousTime
            updatedTime := candleTime
            if array.size(fvgTops) > 0
                for i = array.size(fvgTops) - 1 to 0
                    float top = array.get(fvgTops, i)
                    float bottom = array.get(fvgBottoms, i)
                    bool bullish = array.get(fvgBullish, i)
                    bool fullyFilled = bullish ? candleLow <= bottom : candleHigh >= top
                    if fullyFilled
                        removeFVG(i)
            if not na(startTime) and not na(firstHigh) and not na(firstLow)
                bool bullish = candleLow > firstHigh
                bool bearish = candleHigh < firstLow
                if bullish or bearish
                    float zoneTop = bullish ? candleLow : firstLow
                    float zoneBottom = bullish ? firstHigh : candleHigh
                    array.push(fvgTops, zoneTop)
                    array.push(fvgBottoms, zoneBottom)
                    array.push(fvgBullish, bullish)
                    array.push(fvgStartTimes, startTime)
                    while array.size(fvgTops) > fvgHistoryLimit
                        removeFVG(0)
    updatedTime
if display4HFVG
    [candleTime, startTime, candleLow, candleHigh, firstHigh, firstLow] = request.security(
         syminfo.tickerid,
         "240",
         [time[1], time[3], low[1], high[1], high[3], low[3]],
         lookahead=barmerge.lookahead_on)
    lastProcessed4HTime := process4HFVGCandle(
         candleTime,
         startTime,
         candleLow,
         candleHigh,
         firstHigh,
         firstLow,
         lastProcessed4HTime)
dailyATR = request.security(
     syminfo.tickerid,
     "D",
     ta.atr(14)[1],
     lookahead=barmerge.lookahead_on)
//════════════════════════════════════════════
// DRAW 4H FVGS
//════════════════════════════════════════════
if barstate.islast
    if array.size(visibleBoxes) > 0
        for i = array.size(visibleBoxes) - 1 to 0
            box.delete(array.get(visibleBoxes, i))
            array.remove(visibleBoxes, i)
    if array.size(visibleMidlines) > 0
        for i = array.size(visibleMidlines) - 1 to 0
            line.delete(array.get(visibleMidlines, i))
            array.remove(visibleMidlines, i)
    if array.size(visibleFVGLabels) > 0
        for i = array.size(visibleFVGLabels) - 1 to 0
            label.delete(array.get(visibleFVGLabels, i))
            array.remove(visibleFVGLabels, i)
    if display4HFVG and not na(dailyATR)
        float maxDistance = dailyATR * fvgATRMultiplier
        int boxRight = time + chartMs * rightOffset
        array<int> selected = array.new_int()
        for side = 0 to 1
            int selectedOnSide = 0
            for rank = 0 to fvgPerSide - 1
                int bestIndex = -1
                float bestDistance = na
                if array.size(fvgTops) > 0
                    for i = 0 to array.size(fvgTops) - 1
                        if not array.includes(selected, i)
                            float top = array.get(fvgTops, i)
                            float bottom = array.get(fvgBottoms, i)
                            float mid = (top + bottom) / 2.0
                            bool isAbove = bottom > close or (bottom <= close and top >= close and mid >= close)
                            bool matchesSide = side == 0 ? isAbove : not isAbove
                            float distance = math.max(math.max(bottom - close, close - top), 0.0)
                            if matchesSide and distance <= maxDistance
                                if na(bestDistance) or distance < bestDistance
                                    bestDistance := distance
                                    bestIndex := i
                if bestIndex >= 0
                    array.push(selected, bestIndex)
                    selectedOnSide += 1
            if selectedOnSide == 0
                int fallbackIndex = -1
                float fallbackDistance = na
                if array.size(fvgTops) > 0
                    for i = 0 to array.size(fvgTops) - 1
                        if not array.includes(selected, i)
                            float top = array.get(fvgTops, i)
                            float bottom = array.get(fvgBottoms, i)
                            float mid = (top + bottom) / 2.0
                            bool isAbove = bottom > close or (bottom <= close and top >= close and mid >= close)
                            bool matchesSide = side == 0 ? isAbove : not isAbove
                            float distance = math.max(math.max(bottom - close, close - top), 0.0)
                            if matchesSide and (na(fallbackDistance) or distance < fallbackDistance)
                                fallbackDistance := distance
                                fallbackIndex := i
                if fallbackIndex >= 0
                    array.push(selected, fallbackIndex)
        int totalDashes = 0
        if array.size(selected) > 0
            for n = 0 to array.size(selected) - 1
                int i = array.get(selected, n)
                float zoneTop = array.get(fvgTops, i)
                float zoneBottom = array.get(fvgBottoms, i)
                float midPrice = (zoneTop + zoneBottom) / 2.0
                int startTime = array.get(fvgStartTimes, i)
                int safeStartTime = math.min(startTime, boxRight - 1)
                int boxWidthMs = boxRight - safeStartTime
                int centerTime = safeStartTime + int(boxWidthMs / 2)
                box bx = box.new(
                     left=safeStartTime,
                     top=zoneTop,
                     right=boxRight,
                     bottom=zoneBottom,
                     xloc=xloc.bar_time,
                     border_color=levelColor,
                     border_width=1,
                     bgcolor=color.new(levelColor, fvgTransparency))
                array.push(visibleBoxes, bx)
                if showFVGMidline and totalDashes < maxFVGDashes
                    int dashMs = math.max(1, int(chartMs * fvgDashLength))
                    int spacingMs = math.max(dashMs + 1, int(chartMs * fvgDashSpacing))
                    int clearanceMs = int(chartMs * fvgTextClearance / 2.0)
                    int gapLeft = centerTime - clearanceMs
                    int gapRight = centerTime + clearanceMs
                    int dashCount = math.min(100, int(math.ceil(boxWidthMs * 1.0 / spacingMs)))
                    if dashCount > 0
                        for d = 0 to dashCount - 1
                            if totalDashes < maxFVGDashes
                                int dashStart = safeStartTime + d * spacingMs
                                int dashEnd = math.min(dashStart + dashMs, boxRight)
                                bool overlapsText = dashStart < gapRight and dashEnd > gapLeft
                                if dashStart < boxRight and not overlapsText
                                    line dash = line.new(
                                         x1=dashStart,
                                         y1=midPrice,
                                         x2=dashEnd,
                                         y2=midPrice,
                                         xloc=xloc.bar_time,
                                         color=color.new(levelColor, 30),
                                         width=1)
                                    array.push(visibleMidlines, dash)
                                    totalDashes += 1
                label fvgLabel = label.new(
                     x=centerTime,
                     y=midPrice,
                     text="4H FVG",
                     xloc=xloc.bar_time,
                     yloc=yloc.price,
                     style=label.style_label_center,
                     color=color.new(levelColor, 100),
                     textcolor=textColor,
                     textalign=text.align_center,
                     size=size.small,
                     text_formatting=text.format_italic)
                array.push(visibleFVGLabels, fvgLabel)
//════════════════════════════════════════════
// ALL TIME HIGH
//════════════════════════════════════════════
var float ath = na
var int athTime = na
var line athLine = na
var label athLabel = na
if na(ath) or high > ath
    ath := high
    athTime := time
//════════════════════════════════════════════
// UPDATE ALL LEVELS
//════════════════════════════════════════════
if barstate.islast
    int endTime = time + chartMs * keyLevelEndOffset
    int textTime = labelX(endTime)
    // Daily / Weekly / Monthly
    if array.size(prices) > 0
        for i = 0 to array.size(prices) - 1
            line ln = array.get(lines, i)
            label lb = array.get(labels, i)
            float price = array.get(prices, i)
            bool isHigh = array.get(isHighLevels, i)
            line.set_x2(ln, endTime)
            label.set_xy(lb, textTime, labelPrice(price, isHigh))
    // Equal Highs / Lows
    if array.size(eqPrices) > 0
        for i = 0 to array.size(eqPrices) - 1
            line ln = array.get(eqLines, i)
            label lb = array.get(eqLabels, i)
            float price = array.get(eqPrices, i)
            bool isHigh = array.get(eqIsHigh, i)
            if not na(ln)
                line.set_x2(ln, endTime)
            if not na(lb)
                label.set_xy(lb, textTime, labelPrice(price, isHigh))
    // Session levels
    for i = 0 to 2
        line openLn = array.get(sessionOpenLines, i)
        line highLn = array.get(sessionHighLines, i)
        line lowLn = array.get(sessionLowLines, i)
        label openLb = array.get(sessionOpenLabels, i)
        label highLb = array.get(sessionHighLabels, i)
        label lowLb = array.get(sessionLowLabels, i)
        float sOpen = array.get(sessionOpens, i)
        float sHigh = array.get(sessionHighs, i)
        float sLow = array.get(sessionLows, i)
        if not na(openLn)
            line.set_x2(openLn, endTime)
        if not na(openLb)
            label.set_xy(openLb, textTime, labelPrice(sOpen, true))
        if not na(highLn)
            line.set_x2(highLn, endTime)
        if not na(highLb)
            label.set_xy(highLb, textTime, labelPrice(sHigh, true))
        if not na(lowLn)
            line.set_x2(lowLn, endTime)
        if not na(lowLb)
            label.set_xy(lowLb, textTime, labelPrice(sLow, false))
    // Timed NY levels
    for i = 0 to 2
        line ln = array.get(nyLines, i)
        label lb = array.get(nyLabels, i)
        float price = array.get(nyPrices, i)
        if not na(ln)
            line.set_x2(ln, endTime)
        if not na(lb)
            label.set_xy(lb, textTime, labelPrice(price, true))
    // ATH
    if not showATH
        if not na(athLine)
            line.delete(athLine)
            athLine := na
        if not na(athLabel)
            label.delete(athLabel)
            athLabel := na
    else
        if na(athLine) and not na(ath)
            athLine := line.new(
                 x1=athTime, y1=ath,
                 x2=endTime, y2=ath,
                 xloc=xloc.bar_time,
                 color=levelColor,
                 width=1)
            athLabel := createLevelLabel(endTime, ath, "ATH", true)
        if not na(athLine)
            line.set_xy1(athLine, athTime, ath)
            line.set_xy2(athLine, endTime, ath)
            label.set_xy(athLabel, textTime, labelPrice(ath, true))

← 返回列表