本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("FVG [odanc]", overlay=true, max_boxes_count=500, max_labels_count=500, max_bars_back=500)
// ===== Inputs =====
grpC = "Colors"
grpS = "Settings"
lookback = input.int(50, "Lookback bars", minval=1, maxval=450, group=grpS)
swingL = input.int(1, "Swing left comparison bars", minval=1, maxval=10, group=grpS)
swingR = input.int(1, "Swing right comparison bars", minval=0, maxval=3, group=grpS)
minWicks = input.int(1, "Min past swings", minval=0, group=grpS)
useMaxAge = input.bool(true, "Limit extension bars (off = show all)", group=grpS)
maxAge = input.int(300, "Max extension bars after creation", minval=1, group=grpS)
showFilled = input.bool(false, "Show filled FVGs", group=grpS)
bullCol = input.color(color.new(#089981, 80), "Bullish FVG", group=grpC)
bearCol = input.color(color.new(#f23645, 80), "Bearish FVG", group=grpC)
touchCol = input.color(color.new(#ffb300, 85), "Touched FVG", group=grpC)
doneCol = input.color(color.new(color.gray, 85), "Filled FVG", group=grpC)
// ===== Swing detection (compare l bars left, r bars right) =====
f_swingHigh(int k, int l, int r) =>
bool ok = true
float h = high[k]
for j = 1 to l
if high[k + j] >= h
ok := false
break
if ok and r > 0
for j = 1 to r
if high[k - j] > h
ok := false
break
ok
f_swingLow(int k, int l, int r) =>
bool ok = true
float lo = low[k]
for j = 1 to l
if low[k + j] <= lo
ok := false
break
if ok and r > 0
for j = 1 to r
if low[k - j] < lo
ok := false
break
ok
// ===== Lookback scan =====
f_wicks(float zTop, float zBot, int lb, int l, int r, bool isBull) =>
int c = 0
int oldK = 0
for k = 3 to lb + 2
exceeds = isBull ? high[k] > zTop : low[k] < zBot
if exceeds
break
float px = isBull ? high[k] : low[k]
if px >= zBot and px <= zTop
bool isSwing = isBull ? f_swingHigh(k, l, r) : f_swingLow(k, l, r)
if isSwing
c += 1
oldK := k
[c, oldK, c > 0]
// ===== FVG detection =====
bullFvg = low > high[2]
bearFvg = high < low[2]
bullTop = low
bullBot = high[2]
bearTop = low[2]
bearBot = high
[bullW, bullOld, bullOk] = f_wicks(bullTop, bullBot, lookback, swingL, swingR, true)
[bearW, bearOld, bearOk] = f_wicks(bearTop, bearBot, lookback, swingL, swingR, false)
// ===== Box management =====
var boxes = array.new<box>()
var tops = array.new<float>()
var bots = array.new<float>()
var dirs = array.new<int>() // 1 = bullish, -1 = bearish
var born = array.new<int>() // creation bar index
if array.size(boxes) > 0
for i = array.size(boxes) - 1 to 0
b = array.get(boxes, i)
d = array.get(dirs, i)
t = array.get(tops, i)
bt = array.get(bots, i)
bn = array.get(born, i)
filled = d == 1 ? close < bt : close > t
expired = useMaxAge and (bar_index - bn >= maxAge)
if filled
if showFilled
box.set_right(b, bar_index)
box.set_bgcolor(b, doneCol)
else
box.delete(b)
array.remove(boxes, i)
array.remove(tops, i)
array.remove(bots, i)
array.remove(dirs, i)
array.remove(born, i)
else if expired
// N bars elapsed: stop extending only, keep color and length as is
array.remove(boxes, i)
array.remove(tops, i)
array.remove(bots, i)
array.remove(dirs, i)
array.remove(born, i)
else
box.set_right(b, bar_index)
if high >= bt and low <= t
box.set_bgcolor(b, touchCol)
newFvg(float t, float bt, int dir, int w, int oldK, color col) =>
int off = oldK > 0 ? oldK : 2
box b = box.new(bar_index - off, t, bar_index, bt, border_color=na, bgcolor=col)
array.push(boxes, b)
array.push(tops, t)
array.push(bots, bt)
array.push(dirs, dir)
array.push(born, bar_index)
if bullFvg and bullOk and bullW >= minWicks
newFvg(bullTop, bullBot, 1, bullW, bullOld, bullCol)
if bearFvg and bearOk and bearW >= minWicks
newFvg(bearTop, bearBot, -1, bearW, bearOld, bearCol)