本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("FVG only", overlay=true, max_boxes_count=500)
// ===== SETTINGS =====
maxZones = input.int(
450, "Historical FVGs to Keep",
minval=10, maxval=500)
// ===== APPEARANCE =====
gold = input.color(
color.rgb(212, 160, 23), "FVG Colour")
borderWidth = input.int(
1, "Dashed Border Width",
minval=1, maxval=4)
fillTransparency = input.int(
100, "Background Transparency",
minval=0, maxval=100)
// ===== CANDLE 2 FULL RANGE =====
candle2High = high[1]
candle2Low = low[1]
// ===== ONLY RULE: CANDLE 3 CLOSE INSIDE CANDLE 2 =====
// Includes Candle 2 body and both wicks.
validCandle3 = close >= candle2Low and close <= candle2High
// ===== THREE-CANDLE FVG DETECTION =====
bullFVG = high[2] < low and validCandle3
bearFVG = low[2] > high and validCandle3
// ===== BOX STORAGE =====
var array<box> zones = array.new<box>()
var array<int> createdBars = array.new<int>()
var array<bool> active = array.new<bool>()
// ===== CREATE FVG BOX =====
f_addFVG(int leftBar, float top, float bottom) =>
if not na(top) and not na(bottom) and top > bottom
box b = box.new(
left=leftBar,
top=top,
right=bar_index,
bottom=bottom,
border_color=gold,
border_width=borderWidth,
border_style=line.style_solid,
bgcolor=color.new(gold, fillTransparency))
array.push(zones, b)
array.push(createdBars, bar_index)
array.push(active, true)
// Remove oldest boxes when limit is reached.
while array.size(zones) > maxZones
box.delete(array.shift(zones))
array.shift(createdBars)
array.shift(active)
// ===== EXTEND UNTIL FIRST WICK TOUCH =====
if array.size(zones) > 0
for i = 0 to array.size(zones) - 1
if array.get(active, i)
box b = array.get(zones, i)
int born = array.get(createdBars, i)
// Don't count formation candle as retest.
if bar_index > born
bool tapped = (
low <= box.get_top(b) and
high >= box.get_bottom(b))
// Extend until first touch.
box.set_right(b, bar_index)
if tapped
array.set(active, i, false)
// ===== DRAW FVG AFTER CANDLE 3 CLOSES =====
if barstate.isconfirmed
if bullFVG
f_addFVG(
bar_index - 2,
low,
high[2])
if bearFVG
f_addFVG(
bar_index - 2,
low[2],
high)