本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MNQ / MES • NY Opening Range IFVG", overlay = true, max_labels_count = 500)
// ── Inputs
string TZ = "America/New_York"
atrLength = input.int(14, "ATR length", minval = 1)
minGapATR = input.float(0.05, "Minimum FVG size / ATR", minval = 0, step = 0.01)
stopATR = input.float(0.05, "Stop buffer / ATR", minval = 0, step = 0.01)
bufferTicks = input.int(2, "Minimum stop buffer in ticks", minval = 1)
minRR = input.float(2.0, "Minimum reward / risk", minval = 1, step = 0.1)
riskDollars = input.float(100, "Risk budget ($), before costs", minval = 1)
showZone = input.bool(true, "Show active IFVG")
showLevels = input.bool(true, "Show latest signal stop and target")
if barstate.isfirst and (not timeframe.isminutes or timeframe.multiplier != 15)
runtime.error("Use this indicator on a 15-minute chart.")
// Uses the symbol's contract specifications.
// MNQ: $2 per point. MES: $5 per point.
float tick = syminfo.mintick
float pointValue = syminfo.pointvalue
float atr = ta.atr(atrLength)
int nyHour = hour(time, TZ)
int nyMinute = minute(time, TZ)
int nyDate = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
bool newDay = barstate.isfirst or nyDate != nyDate[1]
bool openingBar = nyHour == 9 and nyMinute == 30
int cutoff = timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ), 11, 30)
// Signal is confirmed at this candle's close.
// The planned next-bar entry must be before 11:30.
bool entryWindow = time_close < cutoff
// ── Session state
var float rangeHigh = na
var float rangeLow = na
var int rangeBar = na
var bool sweptHigh = false
var bool sweptLow = false
var bool traded = false
var bool blocked = false
// Latest qualifying FVG in each direction.
var float bullBottom = na
var float bullTop = na
var float bearBottom = na
var float bearTop = na
// state: 0 = waiting, 1 = waiting for inversion, 2 = waiting for retest
// direction: 1 = long, -1 = short
var int state = 0
var int direction = 0
var float zoneBottom = na
var float zoneTop = na
var float sweepExtreme = na
var float setupBuffer = na
var int inversionBar = na
var float lastStop = na
var float lastTarget = na
bool longSignal = false
bool shortSignal = false
if barstate.isconfirmed
if newDay
rangeHigh := na
rangeLow := na
rangeBar := na
sweptHigh := false
sweptLow := false
traded := false
blocked := false
bullBottom := na
bullTop := na
bearBottom := na
bearTop := na
state := 0
direction := 0
zoneBottom := na
zoneTop := na
sweepExtreme := na
setupBuffer := na
inversionBar := na
lastStop := na
lastTarget := na
// Lock only once the 9:30–9:45 candle has closed.
if openingBar
rangeHigh := high
rangeLow := low
rangeBar := bar_index
bool rangeReady = not na(rangeBar) and bar_index > rangeBar
bool active = rangeReady and entryWindow and not traded and not blocked
if active
// All three FVG candles must start at or after the opening candle.
bool eligibleGap = bar_index >= rangeBar + 2 and not na(atr)
if eligibleGap
if low > high[2] and low - high[2] >= atr * minGapATR
bullBottom := high[2]
bullTop := low
if high < low[2] and low[2] - high >= atr * minGapATR
bearBottom := high
bearTop := low[2]
sweptHigh := sweptHigh or high > rangeHigh
sweptLow := sweptLow or low < rangeLow
// Both sides swept before entry: no trade for this session.
if sweptHigh and sweptLow
blocked := true
state := 0
direction := 0
zoneBottom := na
zoneTop := na
else
bool closeInside = close > rangeLow and close < rangeHigh
// Require the sweep candle itself to close back inside.
bool shortSweep = high > rangeHigh and closeInside
bool longSweep = low < rangeLow and closeInside
if state == 0
// Gap must overlap the move above/below the opening range.
if shortSweep and not na(bullBottom) and bullTop > rangeHigh
direction := -1
zoneBottom := bullBottom
zoneTop := bullTop
sweepExtreme := high
setupBuffer := math.max(tick * bufferTicks, atr * stopATR)
state := 1
else if longSweep and not na(bearBottom) and bearBottom < rangeLow
direction := 1
zoneBottom := bearBottom
zoneTop := bearTop
sweepExtreme := low
setupBuffer := math.max(tick * bufferTicks, atr * stopATR)
state := 1
if state > 0
sweepExtreme := direction == -1 ? math.max(sweepExtreme, high) : math.min(sweepExtreme, low)
// Sweep and inversion may confirm on the same candle.
if state == 1
bool inverted = direction == -1 ? close < zoneBottom : close > zoneTop
if inverted
state := 2
inversionBar := bar_index
// Retest must happen on a later candle.
if state == 2 and bar_index > inversionBar
bool invalidated = direction == -1 ? close > zoneTop : close < zoneBottom
bool targetReached = direction == -1 ? low <= rangeLow : high >= rangeHigh
if invalidated or targetReached
state := 0
direction := 0
zoneBottom := na
zoneTop := na
else
bool touchesZone = high >= zoneBottom and low <= zoneTop
bool closesAway = direction == -1 ? close < zoneBottom : close > zoneTop
if touchesZone and closesAway
float rawStop = direction == -1 ? sweepExtreme + setupBuffer : sweepExtreme - setupBuffer
float stop = direction == -1 ? math.ceil(rawStop / tick) * tick : math.floor(rawStop / tick) * tick
float target = direction == -1 ? rangeLow : rangeHigh
float riskPoints = direction == -1 ? stop - close : close - stop
float rewardPoints = direction == -1 ? close - target : target - close
float rr = riskPoints > 0 ? rewardPoints / riskPoints : na
if riskPoints > 0 and rewardPoints > 0 and rr >= minRR
longSignal := direction == 1
shortSignal := direction == -1
traded := true
lastStop := stop
lastTarget := target
float contractRisk = riskPoints * pointValue
int quantity = contractRisk > 0 ? int(math.floor(riskDollars / contractRisk)) : 0
string side = direction == 1 ? "LONG" : "SHORT"
string details = side
details := details + "\nReference: " + str.tostring(close, format.mintick)
details := details + "\nStop: " + str.tostring(stop, format.mintick)
details := details + "\nTarget: " + str.tostring(target, format.mintick)
details := details + "\nR:R: " + str.tostring(rr, "#.##")
details := details + "\nContracts: " + str.tostring(quantity)
label.new(
bar_index,
direction == 1 ? low : high,
details,
style = direction == 1 ? label.style_label_up : label.style_label_down,
color = direction == 1 ? color.green : color.red,
textcolor = color.white,
size = size.small
)
alert(syminfo.ticker + " | " + details, alert.freq_once_per_bar_close)
state := 0
else
// First confirmed retest failed the R:R filter.
state := 0
direction := 0
zoneBottom := na
zoneTop := na
if not entryWindow
state := 0
// ── Chart display
plot(rangeHigh, "Opening range high", color = color.orange, linewidth = 2, style = plot.style_linebr)
plot(rangeLow, "Opening range low", color = color.aqua, linewidth = 2, style = plot.style_linebr)
bool visibleZone = showZone and state > 0
zoneUpper = plot(visibleZone ? zoneTop : na, "IFVG upper", color = color.purple, style = plot.style_linebr)
zoneLower = plot(visibleZone ? zoneBottom : na, "IFVG lower", color = color.purple, style = plot.style_linebr)
fill(zoneUpper, zoneLower, color = color.new(color.purple, 85))
plot(showLevels ? lastStop : na, "Signal stop", color = color.red, style = plot.style_linebr)
plot(showLevels ? lastTarget : na, "Signal target", color = color.lime, style = plot.style_linebr)
alertcondition(longSignal, "LONG signal", "LONG IFVG signal on {{ticker}}, 15-minute chart.")
alertcondition(shortSignal, "SHORT signal", "SHORT IFVG signal on {{ticker}}, 15-minute chart.")