本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jos-ProTrader
//@version=6
indicator("Mastering Map Fair Value Gap (FVG) [JPT]", shorttitle="Mastering FVG JPT ", overlay=true, max_boxes_count=300, max_labels_count=200)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupDetect = "FVG Detection"
minGapPct = input.float(0.06, "Minimum Gap Size (%)", minval=0.0, step=0.01, group=groupDetect)
atrFilter = input.bool(false, "Use ATR Filter", group=groupDetect)
atrLength = input.int(14, "ATR Length", minval=1, group=groupDetect)
atrMultiple = input.float(0.15, "Minimum Gap / ATR", minval=0.0, step=0.05, group=groupDetect)
confirmBars = input.bool(true, "Confirm FVG at Candle Close", group=groupDetect)
groupDisplay = "Display Settings"
showBull = input.bool(true, "Show Bullish FVGs", group=groupDisplay)
showBear = input.bool(true, "Show Bearish FVGs", group=groupDisplay)
showMid = input.bool(true, "Show 50% Equilibrium", group=groupDisplay)
showLabels = input.bool(true, "Show FVG Labels", group=groupDisplay)
extendBars = input.int(30, "Zone Extension (Bars)", minval=1, maxval=500, group=groupDisplay)
maxZones = input.int(40, "Maximum Active Zones", minval=1, maxval=100, group=groupDisplay)
groupMit = "Mitigation Settings"
mitMode = input.string("50% Fill", "Mitigation Mode", options=["Touch", "50% Fill", "Full Fill"], group=groupMit)
removeMitigated = input.bool(true, "Remove Mitigated Zones", group=groupMit)
showMitigated = input.bool(false, "Keep Mitigated Zones Visible", group=groupMit)
groupColors = "Colors"
bullColor = input.color(color.rgb(0, 200, 140), "Bullish FVG", group=groupColors)
bearColor = input.color(color.rgb(240, 70, 90), "Bearish FVG", group=groupColors)
mitColor = input.color(color.gray, "Mitigated FVG", group=groupColors)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ARRAYS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var box[] fvgBoxes = array.new_box()
var line[] midLines = array.new_line()
var label[] fvgLabels = array.new_label()
var float[] zoneTops = array.new_float()
var float[] zoneBottoms = array.new_float()
var bool[] zoneBullish = array.new_bool()
var bool[] zoneActive = array.new_bool()
var int[] zoneStarts = array.new_int()
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// FVG DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
atrValue = ta.atr(atrLength)
// Bullish FVG: current candle low is above the high two candles ago.
bullGap = low - high[2]
bullGapPct = close != 0 ? bullGap / close * 100 : 0
// Bearish FVG: current candle high is below the low two candles ago.
bearGap = low[2] - high
bearGapPct = close != 0 ? bearGap / close * 100 : 0
bullAtrOK = not atrFilter or bullGap >= atrValue * atrMultiple
bearAtrOK = not atrFilter or bearGap >= atrValue * atrMultiple
bullFVG = bar_index >= 2 and low > high[2] and bullGapPct >= minGapPct and bullAtrOK
bearFVG = bar_index >= 2 and high < low[2] and bearGapPct >= minGapPct and bearAtrOK
canCreate = not confirmBars or barstate.isconfirmed
newBull = bullFVG and canCreate and showBull
newBear = bearFVG and canCreate and showBear
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CREATE BULLISH FVG
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if newBull
float top = low
float bottom = high[2]
float equilibrium = (top + bottom) / 2
box b = box.new(left=bar_index - 2, top=top, right=bar_index + extendBars, bottom=bottom, xloc=xloc.bar_index, bgcolor=color.new(bullColor, 85), border_color=color.new(bullColor, 25), border_width=1)
line ml = na
if showMid
ml := line.new(bar_index - 2, equilibrium, bar_index + extendBars, equilibrium, xloc=xloc.bar_index, color=color.new(bullColor, 15), style=line.style_dotted, width=1)
label lb = na
if showLabels
lb := label.new(bar_index, top , "bullish FVG", xloc=xloc.bar_index, style=label.style_label_left, color=bullColor, textcolor=color.white, size=size.tiny)
array.push(fvgBoxes, b)
array.push(midLines, ml)
array.push(fvgLabels, lb)
array.push(zoneTops, top)
array.push(zoneBottoms, bottom)
array.push(zoneBullish, true)
array.push(zoneActive, true)
array.push(zoneStarts, bar_index)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CREATE BEARISH FVG
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if newBear
float top = low[2]
float bottom = high
float equilibrium = (top + bottom) / 2
box b = box.new(left=bar_index - 2, top=top, right=bar_index + extendBars, bottom=bottom, xloc=xloc.bar_index, bgcolor=color.new(bearColor, 85), border_color=color.new(bearColor, 25), border_width=1)
line ml = na
if showMid
ml := line.new(bar_index - 2, equilibrium, bar_index + extendBars, equilibrium, xloc=xloc.bar_index, color=color.new(bearColor, 15), style=line.style_dotted, width=1)
label lb = na
if showLabels
lb := label.new(bar_index, bottom, "bearish FVG", xloc=xloc.bar_index, style=label.style_label_left, color=bearColor, textcolor=color.white, size=size.tiny)
array.push(fvgBoxes, b)
array.push(midLines, ml)
array.push(fvgLabels, lb)
array.push(zoneTops, top)
array.push(zoneBottoms, bottom)
array.push(zoneBullish, false)
array.push(zoneActive, true)
array.push(zoneStarts, bar_index)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MAINTAIN ZONES & TRACK MITIGATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if array.size(fvgBoxes) > 0
for i = array.size(fvgBoxes) - 1 to 0
box b = array.get(fvgBoxes, i)
line ml = array.get(midLines, i)
label lb = array.get(fvgLabels, i)
float top = array.get(zoneTops, i)
float bottom = array.get(zoneBottoms, i)
bool bullish = array.get(zoneBullish, i)
bool active = array.get(zoneActive, i)
int startBar = array.get(zoneStarts, i)
if active
box.set_right(b, bar_index + extendBars)
if not na(ml)
line.set_x2(ml, bar_index + extendBars)
if not na(lb)
label.set_x(lb, startBar + 2)
float mid = (top + bottom) / 2
bool touched = bullish ? low <= top : high >= bottom
bool halfFilled = bullish ? low <= mid : high >= mid
bool fullyFilled = bullish ? low <= bottom : high >= top
bool mitigated = mitMode == "Touch" ? touched : mitMode == "50% Fill" ? halfFilled : fullyFilled
if mitigated
array.set(zoneActive, i, false)
if removeMitigated
if showMitigated
box.set_bgcolor(b, color.new(mitColor, 90))
box.set_border_color(b, color.new(mitColor, 65))
box.set_right(b, bar_index)
if not na(ml)
line.set_color(ml, color.new(mitColor, 60))
line.set_x2(ml, bar_index)
if not na(lb)
label.set_text(lb, "MITIGATED")
label.set_color(lb, mitColor)
else
box.delete(b)
if not na(ml)
line.delete(ml)
if not na(lb)
label.delete(lb)
array.remove(fvgBoxes, i)
array.remove(midLines, i)
array.remove(fvgLabels, i)
array.remove(zoneTops, i)
array.remove(zoneBottoms, i)
array.remove(zoneBullish, i)
array.remove(zoneActive, i)
array.remove(zoneStarts, i)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LIMIT STORED ZONES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
while array.size(fvgBoxes) > maxZones
box oldBox = array.shift(fvgBoxes)
line oldLine = array.shift(midLines)
label oldLabel = array.shift(fvgLabels)
box.delete(oldBox)
if not na(oldLine)
line.delete(oldLine)
if not na(oldLabel)
label.delete(oldLabel)
array.shift(zoneTops)
array.shift(zoneBottoms)
array.shift(zoneBullish)
array.shift(zoneActive)
array.shift(zoneStarts)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
alertcondition(newBull, title="Bullish FVG Detected", message="New Bullish Fair Value Gap detected on {{ticker}} | Timeframe: {{interval}}")
alertcondition(newBear, title="Bearish FVG Detected", message="New Bearish Fair Value Gap detected on {{ticker}} | Timeframe: {{interval}}")
alertcondition(newBull or newBear, title="New FVG Detected", message="New Fair Value Gap detected on {{ticker}} | Timeframe: {{interval}}")