本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Smart FVG Zones v3.1", shorttitle="Smart FVG v3.1", overlay=true, max_boxes_count=500, max_lines_count=500)
// ─────────────────────────────────────────────
// 1. FVG DISPLAY SETTINGS
// ─────────────────────────────────────────────
groupDisplay = "1. FVG Display"
showBull = input.bool(true, "Show Bullish FVG", group=groupDisplay)
showBear = input.bool(true, "Show Bearish FVG", group=groupDisplay)
showMid = input.bool(true, "Show Dotted 50% Line", group=groupDisplay)
extendBars = input.int(30, "Extend FVG Right (Bars)", minval=1, maxval=500, group=groupDisplay)
lookbackBars = input.int(100, "Show FVGs From Last N Candles", minval=3, maxval=5000, group=groupDisplay)
maxZones = input.int(50, "Maximum Active FVGs", minval=1, maxval=200, group=groupDisplay)
// ─────────────────────────────────────────────
// 2. FVG COLORS
// ─────────────────────────────────────────────
groupColors = "2. FVG Colors"
bullColor = input.color(color.green, "Bullish FVG Color", group=groupColors)
bearColor = input.color(color.red, "Bearish FVG Color", group=groupColors)
zoneTransp = input.int(85, "Zone Transparency", minval=0, maxval=100, group=groupColors)
midTransp = input.int(20, "Midline Transparency", minval=0, maxval=100, group=groupColors)
// ─────────────────────────────────────────────
// 3. FVG TEXT SETTINGS
// ─────────────────────────────────────────────
groupText = "3. FVG Text Settings"
showText = input.bool(true, "Show FVG Text", group=groupText)
fvgText = input.string("FVG", "Text to Display", group=groupText)
textPosition = input.string("Right", "Text Position", options=["Left", "Center", "Right"], group=groupText)
textSizeInput = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=groupText)
textTransp = input.int(0, "Text Transparency", minval=0, maxval=100, group=groupText)
// Text alignment
textAlign = switch textPosition
"Left" => text.align_left
"Center" => text.align_center
=> text.align_right
// Text size
fvgTextSize = switch textSizeInput
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
=> size.large
// ─────────────────────────────────────────────
// 4. MITIGATION SETTINGS
// ─────────────────────────────────────────────
groupMitigation = "4. Mitigation Settings"
mitigation = input.string("Full Fill", "Hide FVG When", options=["Never", "First Touch", "50% Fill", "Full Fill"], group=groupMitigation)
shrinkZones = input.bool(false, "Shrink Zone During Partial Fill", group=groupMitigation)
// ─────────────────────────────────────────────
// 5. DETECTION FILTERS
// ─────────────────────────────────────────────
groupFilter = "5. Detection Filters"
minGapTicks = input.int(1, "Minimum FVG Size (Ticks)", minval=1, group=groupFilter)
// ─────────────────────────────────────────────
// 6. STORAGE ARRAYS
// ─────────────────────────────────────────────
var array<box> zones = array.new<box>()
var array<line> mids = array.new<line>()
var array<float> tops = array.new<float>()
var array<float> bottoms = array.new<float>()
var array<float> fillLevels = array.new<float>()
var array<bool> bullish = array.new<bool>()
var array<int> createdBars = array.new<int>()
// ─────────────────────────────────────────────
// 7. FVG DETECTION
// ─────────────────────────────────────────────
minGap = minGapTicks * syminfo.mintick
// Formation still requires confirmed candles
bullFVG = barstate.isconfirmed and bar_index >= 2 and low > high[2] and (low - high[2]) >= minGap
bearFVG = barstate.isconfirmed and bar_index >= 2 and high < low[2] and (low[2] - high) >= minGap
// Restrict FVG creation to selected candle history
withinRange = bar_index >= 2 and (bar_index - 2) >= (last_bar_index - lookbackBars + 1)
// ─────────────────────────────────────────────
// 8. CREATE BULLISH FVG
// ─────────────────────────────────────────────
if bullFVG and showBull and withinRange
float topPrice = low
float bottomPrice = high[2]
float midpoint = (topPrice + bottomPrice) / 2.0
color zoneColor = color.new(bullColor, zoneTransp)
color labelColor = color.new(bullColor, textTransp)
box newZone = box.new(
left=bar_index - 2,
top=topPrice,
right=bar_index + extendBars,
bottom=bottomPrice,
bgcolor=zoneColor,
border_color=color.new(bullColor, 100),
text=showText ? fvgText : "",
text_color=labelColor,
text_size=fvgTextSize,
text_halign=textAlign,
text_valign=text.align_center)
line newMid = line.new(
x1=bar_index - 2,
y1=midpoint,
x2=bar_index + extendBars,
y2=midpoint,
color=color.new(bullColor, showMid ? midTransp : 100),
style=line.style_dotted,
width=1)
array.push(zones, newZone)
array.push(mids, newMid)
array.push(tops, topPrice)
array.push(bottoms, bottomPrice)
array.push(fillLevels, topPrice)
array.push(bullish, true)
array.push(createdBars, bar_index)
// ─────────────────────────────────────────────
// 9. CREATE BEARISH FVG
// ─────────────────────────────────────────────
if bearFVG and showBear and withinRange
float topPrice = low[2]
float bottomPrice = high
float midpoint = (topPrice + bottomPrice) / 2.0
color zoneColor = color.new(bearColor, zoneTransp)
color labelColor = color.new(bearColor, textTransp)
box newZone = box.new(
left=bar_index - 2,
top=topPrice,
right=bar_index + extendBars,
bottom=bottomPrice,
bgcolor=zoneColor,
border_color=color.new(bearColor, 100),
text=showText ? fvgText : "",
text_color=labelColor,
text_size=fvgTextSize,
text_halign=textAlign,
text_valign=text.align_center)
line newMid = line.new(
x1=bar_index - 2,
y1=midpoint,
x2=bar_index + extendBars,
y2=midpoint,
color=color.new(bearColor, showMid ? midTransp : 100),
style=line.style_dotted,
width=1)
array.push(zones, newZone)
array.push(mids, newMid)
array.push(tops, topPrice)
array.push(bottoms, bottomPrice)
array.push(fillLevels, bottomPrice)
array.push(bullish, false)
array.push(createdBars, bar_index)
// ─────────────────────────────────────────────
// 10. LIVE INTRABAR FVG MITIGATION
// ─────────────────────────────────────────────
if array.size(zones) > 0
for i = array.size(zones) - 1 to 0
box currentZone = array.get(zones, i)
line currentMid = array.get(mids, i)
float topPrice = array.get(tops, i)
float bottomPrice = array.get(bottoms, i)
float previousFill = array.get(fillLevels, i)
bool isBull = array.get(bullish, i)
int created = array.get(createdBars, i)
float midpoint = (topPrice + bottomPrice) / 2.0
// Remove old FVGs
bool tooOld = (bar_index - (created - 2)) >= lookbackBars
// IMPORTANT:
// No barstate.isconfirmed here.
// Check mitigation on every realtime update.
// Exclude the original formation candle.
bool eligible = bar_index > created
bool touched = false
bool halfFilled = false
bool fullyFilled = false
if eligible
if isBull
touched := low <= topPrice
halfFilled := low <= midpoint
fullyFilled := low <= bottomPrice
else
touched := high >= bottomPrice
halfFilled := high >= midpoint
fullyFilled := high >= topPrice
bool mitigated = false
if mitigation == "First Touch"
mitigated := touched
else if mitigation == "50% Fill"
mitigated := halfFilled
else if mitigation == "Full Fill"
mitigated := fullyFilled
bool removeZone = tooOld or mitigated
if removeZone
// Delete immediately during running candle
box.delete(currentZone)
line.delete(currentMid)
array.remove(zones, i)
array.remove(mids, i)
array.remove(tops, i)
array.remove(bottoms, i)
array.remove(fillLevels, i)
array.remove(bullish, i)
array.remove(createdBars, i)
else
// Extend FVG to the right
box.set_right(currentZone, bar_index + extendBars)
line.set_x2(currentMid, bar_index + extendBars)
color baseColor = isBull ? bullColor : bearColor
// Midpoint line
line.set_color(currentMid, color.new(baseColor, showMid ? midTransp : 100))
// FVG text
box.set_text(currentZone, showText ? fvgText : "")
box.set_text_color(currentZone, color.new(baseColor, textTransp))
box.set_text_size(currentZone, fvgTextSize)
box.set_text_halign(currentZone, textAlign)
box.set_text_valign(currentZone, text.align_center)
// Live partial mitigation shrink
if shrinkZones and eligible
if isBull
float newFill = math.min(previousFill, low)
newFill := math.max(bottomPrice, newFill)
array.set(fillLevels, i, newFill)
box.set_top(currentZone, newFill)
else
float newFill = math.max(previousFill, high)
newFill := math.min(topPrice, newFill)
array.set(fillLevels, i, newFill)
box.set_bottom(currentZone, newFill)
// ─────────────────────────────────────────────
// 11. MAXIMUM FVG LIMIT
// ─────────────────────────────────────────────
while array.size(zones) > maxZones
box.delete(array.shift(zones))
line.delete(array.shift(mids))
array.shift(tops)
array.shift(bottoms)
array.shift(fillLevels)
array.shift(bullish)
array.shift(createdBars)
// ─────────────────────────────────────────────
// 12. ALERTS
// ─────────────────────────────────────────────
alertcondition(
bullFVG and showBull,
title="Bullish FVG Formed",
message="New Bullish FVG detected on {{ticker}} - {{interval}}")
alertcondition(
bearFVG and showBear,
title="Bearish FVG Formed",
message="New Bearish FVG detected on {{ticker}} - {{interval}}")