本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("OPEN RANGE VELTRIX", shorttitle = "OR VELTRIX", overlay = true, max_labels_count = 500)
// On a 1m chart: completed 09:30-09:35 5m range -> confirmed break ->
// later confirmed retest -> still-later LIVE EMA200 touch/cross.
// Opening direction/TP use the COMPLETED 5m candle's EMA50/200/800 alignment.
// The plotted EMAs always use the current chart timeframe and stay live.
// This indicator sends alerts and draws reference levels. It places no orders.
// Historical touch markers use bar ranges, not unavailable historical ticks.
string TZ = "America/New_York"
string RISK = "Risk distances"
float pipSize = input.float(1.0, "Price points per pip", minval = 0.00000001, tooltip = "Default for this NAS100 setup: 1 pip = 1.0 index price point. Change this conversion for a different instrument.", group = RISK)
float slPips = input.float(100.0, "Stop loss (pips)", minval = 1.0, group = RISK)
float alignedTpPips = input.float(250.0, "Green / red opening TP (pips)", minval = 1.0, group = RISK)
float blueTpPips = input.float(100.0, "Blue opening TP (pips)", minval = 1.0, group = RISK)
float bePips = input.float(60.0, "Break-even trigger (pips)", minval = 1.0, tooltip = "After a favorable move of 60 pips, the BE guide moves the stop to the entry reference. This does not modify a broker order.", group = RISK)
string DISPLAY = "Display"
bool showStages = input.bool(true, "Show breakout and retest markers", group = DISPLAY)
bool showGuides = input.bool(true, "Show entry / SL / TP / BE guides", group = DISPLAY)
bool showPanel = input.bool(true, "Show setup panel", group = DISPLAY)
string ALERTS = "Alerts"
bool sendEntryAlerts = input.bool(true, "Enable BUY / SELL alert() calls", tooltip = "Create one TradingView alert for OPEN RANGE VELTRIX and choose Any alert() function call. Enable app notifications in the alert dialog.", group = ALERTS)
bool sendCloseReminder = input.bool(true, "15:00 NY close reminder", tooltip = "After a signal, the same running alert sends one reminder at the first update available at or after15:00 NY. It cannot close a broker position.", group = ALERTS)
int chartSeconds = timeframe.in_seconds()
if barstate.isfirst and (not chart.is_standard or (chartSeconds != 60 and chartSeconds != 300))
runtime.error("Use regular 1-minute candles for entry alerts. A 5-minute chart is supported for viewing the opening candle only.")
bool signalChart = chartSeconds == 60
float ema50 = ta.ema(close, 50)
float ema200 = ta.ema(close, 200)
float ema800 = ta.ema(close, 800)
int liveAlignment = ema50 > ema200 and ema200 > ema800 ? 1 : ema50 < ema200 and ema200 < ema800 ? -1 : 0
color blue = color.rgb(0, 0, 139)
color ema800Color = liveAlignment == 1 ? color.green : liveAlignment == -1 ? color.red : blue
f_fiveMinuteOpening() =>
float e50 = ta.ema(close, 50)
float e200 = ta.ema(close, 200)
float e800 = ta.ema(close, 800)
int alignment = e50 > e200 and e200 > e800 ? 1 : e50 < e200 and e200 < e800 ? -1 : 0
[time, time_close, high, low, alignment, not na(e800)]
// No future data: consume this tuple ONLY on the confirmed chart bar that
// closes at09:35, when the requested09:30 5m candle is also complete.
[fiveOpen, fiveClose, fiveHigh, fiveLow, fiveAlignment, fiveEmaReady] = request.security(syminfo.tickerid, "5", f_fiveMinuteOpening(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
int yr = year(time, TZ)
int mo = month(time, TZ)
int dy = dayofmonth(time, TZ)
int dateKey = yr * 10000 + mo * 100 + dy
int openingStart = timestamp(TZ, yr, mo, dy, 9, 30)
int openingEnd = timestamp(TZ, yr, mo, dy, 9, 35)
int entryCutoff = timestamp(TZ, yr, mo, dy, 12, 0)
int sessionEnd = timestamp(TZ, yr, mo, dy, 15, 0)
bool weekday = dayofweek(time, TZ) >= dayofweek.monday and dayofweek(time, TZ) <= dayofweek.friday
// Primitive varip state survives realtime rollback: an intrabar alert must
// never disappear and rearm on the next update of the same candle.
varip int storedDate = na
varip float rangeHigh = na
varip float rangeLow = na
varip int openingAlignment = na
varip bool rangeReady = false
varip int buyBreakBar = na
varip int sellBreakBar = na
varip int armedSide = 0
varip int retestBar = na
varip bool signalledToday = false
varip int entrySide = 0
varip int entryBar = na
varip float entryReference = na
varip float stopReference = na
varip float targetReference = na
varip float beReference = na
varip float tradeTpPips = na
varip bool beArmed = false
varip int lastProcessedClose = na
varip int buyBreakMarkBar = na
varip int sellBreakMarkBar = na
varip int buyRetestMarkBar = na
varip int sellRetestMarkBar = na
varip float previousEligibleDelta = na
varip int updateCounter = 0
varip int entryUpdate = na
varip bool closeReminderSent = false
if na(storedDate) or dateKey != storedDate
storedDate := dateKey
rangeHigh := na
rangeLow := na
openingAlignment := na
rangeReady := false
buyBreakBar := na
sellBreakBar := na
armedSide := 0
retestBar := na
signalledToday := false
entrySide := 0
entryBar := na
entryReference := na
stopReference := na
targetReference := na
beReference := na
tradeTpPips := na
beArmed := false
lastProcessedClose := na
buyBreakMarkBar := na
sellBreakMarkBar := na
buyRetestMarkBar := na
sellRetestMarkBar := na
previousEligibleDelta := na
updateCounter := 0
entryUpdate := na
closeReminderSent := false
if barstate.isnew
previousEligibleDelta := na
updateCounter += 1
if barstate.isconfirmed and (na(lastProcessedClose) or lastProcessedClose != bar_index)
lastProcessedClose := bar_index
bool completedOpening = weekday and time_close == openingEnd and fiveOpen == openingStart and fiveClose == openingEnd
if completedOpening and fiveEmaReady and fiveHigh > fiveLow
rangeHigh := fiveHigh
rangeLow := fiveLow
openingAlignment := fiveAlignment
rangeReady := true
bool setupWindow = signalChart and weekday and rangeReady and not signalledToday and time >= openingEnd and time_close < entryCutoff
if setupWindow and armedSide == 0
bool buyAllowed = openingAlignment >= 0
bool sellAllowed = openingAlignment <= 0
// A later candle's range must touch the broken boundary. No extra
// reclaim-close filter is imposed on the requested wick retest.
bool buyRetest = buyAllowed and not na(buyBreakBar) and bar_index > buyBreakBar and low <= rangeHigh and high >= rangeHigh
bool sellRetest = sellAllowed and not na(sellBreakBar) and bar_index > sellBreakBar and high >= rangeLow and low <= rangeLow
// If a BLUE day touches both boundaries, prefer the older breakout.
if buyRetest and (not sellRetest or buyBreakBar < sellBreakBar)
armedSide := 1
retestBar := bar_index
buyRetestMarkBar := bar_index
else if sellRetest
armedSide := -1
retestBar := bar_index
sellRetestMarkBar := bar_index
else
if buyAllowed and close > rangeHigh and na(buyBreakBar)
buyBreakBar := bar_index
buyBreakMarkBar := bar_index
if sellAllowed and close < rangeLow and na(sellBreakBar)
sellBreakBar := bar_index
sellBreakMarkBar := bar_index
int observedTime = barstate.isrealtime ? timenow : time_close
bool entryEligible = signalChart and weekday and rangeReady and not signalledToday and armedSide != 0 and not na(retestBar) and bar_index > retestBar and time >= openingEnd and observedTime < entryCutoff and not na(ema200)
float currentDelta = close - ema200
// Realtime: current observed price near EMA200, or a crossing between TWO
// eligible updates in this same candle. Never use an earlier realtime wick.
bool observedTouch = math.abs(currentDelta) <= syminfo.mintick or (not na(previousEligibleDelta) and (currentDelta > 0 and previousEligibleDelta < 0 or currentDelta < 0 and previousEligibleDelta > 0))
bool historicalTouch = low <= ema200 and high >= ema200
bool entryNow = entryEligible and (barstate.isrealtime ? observedTouch : barstate.isconfirmed and historicalTouch)
if entryNow
signalledToday := true
entrySide := armedSide
entryBar := bar_index
entryUpdate := updateCounter
// Live alert uses the observed market price, never assumes an EMA limit fill.
// Historical bars use the close as a signal reference, not a backtest fill.
entryReference := close
tradeTpPips := openingAlignment == 0 ? blueTpPips : alignedTpPips
stopReference := math.round_to_mintick(entryReference - entrySide * slPips * pipSize)
targetReference := math.round_to_mintick(entryReference + entrySide * tradeTpPips * pipSize)
beReference := math.round_to_mintick(entryReference + entrySide * bePips * pipSize)
beArmed := false
if sendEntryAlerts
string directionText = entrySide == 1 ? "BUY" : "SELL"
string openingText = openingAlignment == 1 ? "GREEN" : openingAlignment == -1 ? "RED" : "BLUE"
string message = "OPEN RANGE VELTRIX | " + directionText + " | " + syminfo.tickerid + " | 1m EMA200 TOUCH | Entry reference " + str.tostring(entryReference, format.mintick) + " | EMA200 " + str.tostring(ema200, format.mintick) + " | SL " + str.tostring(slPips) + " pips @ " + str.tostring(stopReference, format.mintick) + " | TP " + str.tostring(tradeTpPips) + " pips @ " + str.tostring(targetReference, format.mintick) + " | BE at +" + str.tostring(bePips) + " pips: move stop to entry | Opening 5m color " + openingText + " | 1 pip=" + str.tostring(pipSize) + " price points"
alert(message, alert.freq_once_per_bar)
previousEligibleDelta := entryEligible and not entryNow ? currentDelta : na
// BE is a visual guide only. Live mode uses a LATER observed price, never the
// earlier high/low of the entry candle. Historical mode uses later bar closes.
if signalledToday and not beArmed and observedTime < sessionEnd
bool laterObservation = barstate.isrealtime ? updateCounter > entryUpdate : bar_index > entryBar
if laterObservation and entrySide * (close - entryReference) >= bePips * pipSize
beArmed := true
stopReference := entryReference
if signalledToday and not closeReminderSent and observedTime >= sessionEnd
closeReminderSent := true
if sendCloseReminder
alert("OPEN RANGE VELTRIX | 15:00 NEW YORK | Close any remaining position from today's signal. This is a reminder; no broker order was sent.", alert.freq_once_per_bar)
plot(ema50, "EMA 50", color = color.rgb(135, 206, 250), linewidth = 2)
plot(ema200, "EMA 200", color = color.white, linewidth = 2)
plot(ema800, "EMA 800 live alignment", color = ema800Color, linewidth = 2)
bool showRange = rangeReady and time >= openingStart and time < sessionEnd
plot(showRange ? rangeHigh : na, "09:30-09:35 HIGH", color = color.green, linewidth = 2, style = plot.style_linebr)
plot(showRange ? rangeLow : na, "09:30-09:35 LOW", color = color.red, linewidth = 2, style = plot.style_linebr)
bool guidesVisible = showGuides and signalledToday and time < sessionEnd
plot(guidesVisible ? entryReference : na, "Entry reference", color = color.yellow, style = plot.style_linebr)
plot(guidesVisible ? stopReference : na, "SL / BE stop guide", color = color.red, linewidth = 2, style = plot.style_linebr)
plot(guidesVisible ? targetReference : na, "TP guide", color = color.lime, linewidth = 2, style = plot.style_linebr)
plot(guidesVisible ? beReference : na, "BE trigger guide", color = color.orange, style = plot.style_linebr)
plotshape(showStages and buyBreakMarkBar == bar_index, "BUY break", shape.triangleup, location.belowbar, color.green, size = size.tiny, text = "BREAK")
plotshape(showStages and sellBreakMarkBar == bar_index, "SELL break", shape.triangledown, location.abovebar, color.red, size = size.tiny, text = "BREAK")
plotshape(showStages and buyRetestMarkBar == bar_index, "BUY retest", shape.circle, location.belowbar, color.green, size = size.tiny, text = "RETEST")
plotshape(showStages and sellRetestMarkBar == bar_index, "SELL retest", shape.circle, location.abovebar, color.red, size = size.tiny, text = "RETEST")
bool buyMarker = entryBar == bar_index and entrySide == 1
bool sellMarker = entryBar == bar_index and entrySide == -1
plotshape(buyMarker, "BUY EMA200 touch", shape.labelup, location.belowbar, color.green, text = "BUY\nEMA200", textcolor = color.white, size = size.small)
plotshape(sellMarker, "SELL EMA200 touch", shape.labeldown, location.abovebar, color.red, text = "SELL\nEMA200", textcolor = color.white, size = size.small)
var table panel = table.new(position.top_right, 2, 7, bgcolor = color.new(color.black, 15))
if barstate.islast
if showPanel
string route = not rangeReady ? "Waiting for09:35 NY" : openingAlignment == 1 ? "GREEN: BUY only" : openingAlignment == -1 ? "RED: SELL only" : "BLUE: BUY or SELL"
string state = not signalChart ? "Switch to1m for signals" : observedTime >= sessionEnd ? "Session finished" : signalledToday ? (entrySide == 1 ? "BUY signalled" : "SELL signalled") : observedTime >= entryCutoff ? "No new entries after12:00 NY" : armedSide == 1 ? "BUY: wait for EMA200" : armedSide == -1 ? "SELL: wait for EMA200" : "Wait for break + retest"
color routeColor = not rangeReady ? color.gray : openingAlignment == 1 ? color.green : openingAlignment == -1 ? color.red : blue
table.cell(panel, 0, 0, "OPEN RANGE VELTRIX", text_color = color.white, bgcolor = blue)
table.cell(panel, 1, 0, "5m OR -> 1m ENTRY", text_color = color.white, bgcolor = blue)
table.cell(panel, 0, 1, "Opening5m color", text_color = color.white)
table.cell(panel, 1, 1, route, text_color = color.white, bgcolor = routeColor)
table.cell(panel, 0, 2, "Setup", text_color = color.white)
table.cell(panel, 1, 2, state, text_color = color.white)
table.cell(panel, 0, 3, "SL / TP (pips)", text_color = color.white)
table.cell(panel, 1, 3, str.tostring(slPips) + " / " + (rangeReady ? str.tostring(openingAlignment == 0 ? blueTpPips : alignedTpPips) : "250 color /100 blue"), text_color = color.white)
table.cell(panel, 0, 4, "BE guide", text_color = color.white)
table.cell(panel, 1, 4, beArmed ? "Triggered: stop at entry" : "+" + str.tostring(bePips) + " pips -> entry", text_color = color.orange)
table.cell(panel, 0, 5, "Signal schedule", text_color = color.white)
table.cell(panel, 1, 5, "1/day; entry<12 / close15 NY", text_color = color.white)
table.cell(panel, 0, 6, "Levels / alerts", text_color = color.white)
table.cell(panel, 1, 6, "Reference only; no orders", text_color = color.white)
else
table.clear(panel, 0, 0, 1, 6)