本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("FVG only by Sammy", overlay=true, max_boxes_count=500)
// SETTINGS
maxZones = input.int(450, "Historical FVGs to Keep", minval=10, maxval=500)
maxExtend = input.int(50, "Maximum Extension (Bars)", minval=1, maxval=500)
minOverlap = input.float(50.0, "Minimum Candle 3 Body Inside Candle 2 (%)", minval=0, maxval=100, step=5)
gold = input.color(color.rgb(212, 160, 23), "FVG Colour")
// CANDLE 3 BODY
bodyTop = math.max(open, close)
bodyBottom = math.min(open, close)
bodySize = bodyTop - bodyBottom
// OVERLAP BETWEEN CANDLE 3 BODY AND CANDLE 2 RANGE
overlapTop = math.min(bodyTop, high[1])
overlapBottom = math.max(bodyBottom, low[1])
overlapSize = math.max(0.0, overlapTop - overlapBottom)
// PERCENTAGE OF CANDLE 3 BODY INSIDE CANDLE 2
overlapPercent = bodySize > 0 ? (overlapSize / bodySize) * 100.0 : 0.0
validBodyOverlap = overlapPercent >= minOverlap
// YOUR THREE-CANDLE FVG RULE
// Candle 1 and Candle 3 must leave a gap.
// At least 50% of Candle 3 body overlaps Candle 2 range.
bullFVG = high[2] < low and validBodyOverlap
bearFVG = low[2] > high and validBodyOverlap
// FVG 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
b = box.new(
left=leftBar,
top=top,
right=bar_index,
bottom=bottom,
border_color=color.new(gold, 20),
bgcolor=color.new(gold, 88))
array.push(zones, b)
array.push(createdBars, bar_index)
array.push(active, true)
while array.size(zones) > maxZones
box.delete(array.shift(zones))
array.shift(createdBars)
array.shift(active)
// EXTEND UNTIL FIRST RETEST OR EXPIRY
if array.size(zones) > 0
for i = 0 to array.size(zones) - 1
if array.get(active, i)
b = array.get(zones, i)
born = array.get(createdBars, i)
if bar_index > born
touched = low <= box.get_top(b) and high >= box.get_bottom(b)
expired = bar_index - born >= maxExtend
if touched or expired
box.set_right(b, bar_index)
array.set(active, i, false)
else
box.set_right(b, bar_index)
// DRAW FVGs ON ALL HISTORICAL CANDLES
if barstate.isconfirmed
if bullFVG
f_addFVG(bar_index - 2, low, high[2])
if bearFVG
f_addFVG(bar_index - 2, low[2], high)