OPEN-SOURCE SCRIPT

Fair Value Gap (FVG) Detector

102
//version=5
indicator("Fair Value Gap (FVG) Detector", overlay=true, max_boxes_count=500)

// === USER INPUTS ===
showBullish = input.bool(true, "Show Bullish FVG")
showBearish = input.bool(true, "Show Bearish FVG")
removeFilled = input.bool(true, "Remove Filled Gaps")
extendBars = input.int(20, "Extend Boxes (Bars)", minval=1)

// === STORAGE ARRAYS ===
var box[] bullishBoxes = array.new_box()
var box[] bearishBoxes = array.new_box()

// === BULLISH FVG CONDITION ===
// Candle structure: low of current > high of 2 candles ago
bullishFVG = low > high[2]

// === BEARISH FVG CONDITION ===
// Candle structure: high of current < low of 2 candles ago
bearishFVG = high < low[2]

// === DRAW BULLISH FVG ===
if showBullish and bullishFVG
top = low
bottom = high[2]
newBox = box.new(left=bar_index[2], right=bar_index + extendBars,
top=top, bottom=bottom,
border_color=color.green,
bgcolor=color.new(color.green, 85))
array.push(bullishBoxes, newBox)

// === DRAW BEARISH FVG ===
if showBearish and bearishFVG
top = low[2]
bottom = high
newBox = box.new(left=bar_index[2], right=bar_index + extendBars,
top=top, bottom=bottom,
border_color=color.red,
bgcolor=color.new(color.red, 85))
array.push(bearishBoxes, newBox)

// === REMOVE FILLED GAPS ===
if removeFilled
// Check Bullish
for i = array.size(bullishBoxes) - 1 to 0
b = array.get(bullishBoxes, i)
if close <= box.get_bottom(b)
box.delete(b)
array.remove(bullishBoxes, i)

// Check Bearish
for i = array.size(bearishBoxes) - 1 to 0
b = array.get(bearishBoxes, i)
if close >= box.get_top(b)
box.delete(b)
array.remove(bearishBoxes, i)

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน