本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Opening Range — Break and Retest", overlay=true, max_labels_count=50)
// ── General display settings
showLevels = input.bool(true, "Show opening-range levels", group="Display")
showBreakSignals = input.bool(true, "Show breakout signals", group="Display")
showRetestSignals = input.bool(true, "Show retest signals", group="Display")
showTags = input.bool(true, "Show right-edge level tags", group="Display")
boxTransparency = input.int(
92,
"Range box transparency",
minval=0,
maxval=100,
tooltip="0 = opaque, 100 = transparent",
group="Display")
tagSizeChoice = input.string(
"Tiny",
"Tag size",
options=["Tiny", "Small", "Normal", "Large"],
group="Display")
tagOffsetBars = input.int(2, "Tag horizontal offset (bars)", minval=1, maxval=100, group="Display")
tagGapTicks = input.int(0, "Tag vertical offset (ticks)", minval=0, maxval=100, group="Display")
tagTransparency = input.int(82, "Tag background transparency", minval=0, maxval=100, group="Display")
tagSize = switch tagSizeChoice
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
// ── New York settings
enableNY = input.bool(true, "Enable New York", group="New York")
nySession = input.string("0930-1600:23456", "Session hours (HHMM-HHMM:days)", group="New York")
nyTimezone = input.string("America/New_York", "Time zone (IANA)", group="New York")
nyRangeMinutes = input.int(15, "Opening range duration (minutes)", minval=1, group="New York")
nyBreakBufferTicks = input.int(2, "Breakout buffer (ticks)", minval=0, group="New York")
nyToleranceTicks = input.int(4, "Retest tolerance (ticks)", minval=0, group="New York")
nyMaxWaitBars = input.int(30, "Maximum bars to wait for retest", minval=1, group="New York")
nyColor = input.color(color.orange, "Color", group="New York")
// ── London settings
enableLondon = input.bool(true, "Enable London", group="London")
londonSession = input.string("0800-1630:23456", "Session hours (HHMM-HHMM:days)", group="London")
londonTimezone = input.string("Europe/London", "Time zone (IANA)", group="London")
londonRangeMinutes = input.int(15, "Opening range duration (minutes)", minval=1, group="London")
londonBreakBufferTicks = input.int(2, "Breakout buffer (ticks)", minval=0, group="London")
londonToleranceTicks = input.int(4, "Retest tolerance (ticks)", minval=0, group="London")
londonMaxWaitBars = input.int(30, "Maximum bars to wait for retest", minval=1, group="London")
londonColor = input.color(color.fuchsia, "Color", group="London")
// ── Tokyo settings
enableTokyo = input.bool(true, "Enable Tokyo", group="Tokyo")
tokyoSession = input.string("0900-1500:23456", "Session hours (HHMM-HHMM:days)", group="Tokyo")
tokyoTimezone = input.string("Asia/Tokyo", "Time zone (IANA)", group="Tokyo")
tokyoRangeMinutes = input.int(15, "Opening range duration (minutes)", minval=1, group="Tokyo")
tokyoBreakBufferTicks = input.int(2, "Breakout buffer (ticks)", minval=0, group="Tokyo")
tokyoToleranceTicks = input.int(4, "Retest tolerance (ticks)", minval=0, group="Tokyo")
tokyoMaxWaitBars = input.int(30, "Maximum bars to wait for retest", minval=1, group="Tokyo")
tokyoColor = input.color(color.yellow, "Color", group="Tokyo")
// ── Build an opening range and detect its breakouts and retests
processSession(string sessionHours, string timezone, bool enabled, int rangeMinutes, int breakBufferTicks, int toleranceTicks, int maxWaitBars) =>
bool inSession = enabled and not na(time(timeframe.period, sessionHours, timezone))
bool sessionStarts = inSession and not inSession[1]
var int sessionStartTime = na
var bool rangeBuilding = false
var bool rangeReady = false
var float rangeHigh = na
var float rangeLow = na
var bool upBreakUsed = false
var bool downBreakUsed = false
var bool upRetestPending = false
var bool downRetestPending = false
var int upBreakBar = na
var int downBreakBar = na
if sessionStarts
sessionStartTime := time
rangeBuilding := true
rangeReady := false
rangeHigh := high
rangeLow := low
upBreakUsed := false
downBreakUsed := false
upRetestPending := false
downRetestPending := false
upBreakBar := na
downBreakBar := na
if inSession and rangeBuilding
if time < sessionStartTime + rangeMinutes * 60 * 1000
rangeHigh := math.max(rangeHigh, high)
rangeLow := math.min(rangeLow, low)
else
rangeBuilding := false
rangeReady := true
float tolerance = toleranceTicks * syminfo.mintick
float breakBuffer = breakBufferTicks * syminfo.mintick
bool canSignal = inSession and rangeReady
bool breakUp = canSignal and not upBreakUsed and close > rangeHigh + breakBuffer
bool breakDown = canSignal and not downBreakUsed and close < rangeLow - breakBuffer
if breakUp
upBreakUsed := true
upRetestPending := true
upBreakBar := bar_index
if breakDown
downBreakUsed := true
downRetestPending := true
downBreakBar := bar_index
bool retestUp = (
canSignal and
upRetestPending and
bar_index > upBreakBar and
low <= rangeHigh + tolerance and
high >= rangeHigh - tolerance and
close > rangeHigh
)
bool retestDown = (
canSignal and
downRetestPending and
bar_index > downBreakBar and
high >= rangeLow - tolerance and
low <= rangeLow + tolerance and
close < rangeLow
)
if retestUp
upRetestPending := false
if retestDown
downRetestPending := false
if upRetestPending and (bar_index - upBreakBar > maxWaitBars or close < rangeHigh - tolerance)
upRetestPending := false
if downRetestPending and (bar_index - downBreakBar > maxWaitBars or close > rangeLow + tolerance)
downRetestPending := false
[rangeHigh, rangeLow, rangeReady, breakUp, breakDown, retestUp, retestDown]
// ── Calculate each session independently
[nyHigh, nyLow, nyReady, nyBreakUp, nyBreakDown, nyRetestUp, nyRetestDown] = processSession(nySession, nyTimezone, enableNY, nyRangeMinutes, nyBreakBufferTicks, nyToleranceTicks, nyMaxWaitBars)
[londonHigh, londonLow, londonReady, londonBreakUp, londonBreakDown, londonRetestUp, londonRetestDown] = processSession(londonSession, londonTimezone, enableLondon, londonRangeMinutes, londonBreakBufferTicks, londonToleranceTicks, londonMaxWaitBars)
[tokyoHigh, tokyoLow, tokyoReady, tokyoBreakUp, tokyoBreakDown, tokyoRetestUp, tokyoRetestDown] = processSession(tokyoSession, tokyoTimezone, enableTokyo, tokyoRangeMinutes, tokyoBreakBufferTicks, tokyoToleranceTicks, tokyoMaxWaitBars)
// ── Plot opening-range levels
nyHighPlot = plot(showLevels and enableNY and nyReady ? nyHigh : na, "NY Opening Range High", color=nyColor, linewidth=1, style=plot.style_stepline, display=display.all - display.price_scale - display.status_line)
nyLowPlot = plot(showLevels and enableNY and nyReady ? nyLow : na, "NY Opening Range Low", color=nyColor, linewidth=1, style=plot.style_stepline, display=display.all - display.price_scale - display.status_line)
fill(nyHighPlot, nyLowPlot, color=color.new(nyColor, boxTransparency), title="NY Opening Range")
londonHighPlot = plot(showLevels and enableLondon and londonReady ? londonHigh : na, "London Opening Range High", color=londonColor, linewidth=1, style=plot.style_stepline, display=display.all - display.price_scale - display.status_line)
londonLowPlot = plot(showLevels and enableLondon and londonReady ? londonLow : na, "London Opening Range Low", color=londonColor, linewidth=1, style=plot.style_stepline, display=display.all - display.price_scale - display.status_line)
fill(londonHighPlot, londonLowPlot, color=color.new(londonColor, boxTransparency), title="London Opening Range")
tokyoHighPlot = plot(showLevels and enableTokyo and tokyoReady ? tokyoHigh : na, "Tokyo Opening Range High", color=tokyoColor, linewidth=1, style=plot.style_stepline, display=display.all - display.price_scale - display.status_line)
tokyoLowPlot = plot(showLevels and enableTokyo and tokyoReady ? tokyoLow : na, "Tokyo Opening Range Low", color=tokyoColor, linewidth=1, style=plot.style_stepline, display=display.all - display.price_scale - display.status_line)
fill(tokyoHighPlot, tokyoLowPlot, color=color.new(tokyoColor, boxTransparency), title="Tokyo Opening Range")
// ── Breakout and retest markers
plotshape(showBreakSignals and nyBreakUp, title="NY Break Up", text="NY Break", style=shape.labelup, location=location.belowbar, color=nyColor, textcolor=color.black, size=size.tiny)
plotshape(showBreakSignals and nyBreakDown, title="NY Break Down", text="NY Break", style=shape.labeldown, location=location.abovebar, color=nyColor, textcolor=color.black, size=size.tiny)
plotshape(showRetestSignals and nyRetestUp, title="NY Bullish Retest", text="NY Retest", style=shape.labelup, location=location.belowbar, color=color.new(nyColor, 10), textcolor=color.black, size=size.tiny)
plotshape(showRetestSignals and nyRetestDown, title="NY Bearish Retest", text="NY Retest", style=shape.labeldown, location=location.abovebar, color=color.new(nyColor, 10), textcolor=color.black, size=size.tiny)
plotshape(showBreakSignals and londonBreakUp, title="London Break Up", text="London Break", style=shape.labelup, location=location.belowbar, color=londonColor, textcolor=color.black, size=size.tiny)
plotshape(showBreakSignals and londonBreakDown, title="London Break Down", text="London Break", style=shape.labeldown, location=location.abovebar, color=londonColor, textcolor=color.black, size=size.tiny)
plotshape(showRetestSignals and londonRetestUp, title="London Bullish Retest", text="London Retest", style=shape.labelup, location=location.belowbar, color=color.new(londonColor, 10), textcolor=color.black, size=size.tiny)
plotshape(showRetestSignals and londonRetestDown, title="London Bearish Retest", text="London Retest", style=shape.labeldown, location=location.abovebar, color=color.new(londonColor, 10), textcolor=color.black, size=size.tiny)
plotshape(showBreakSignals and tokyoBreakUp, title="Tokyo Break Up", text="Tokyo Break", style=shape.labelup, location=location.belowbar, color=tokyoColor, textcolor=color.black, size=size.tiny)
plotshape(showBreakSignals and tokyoBreakDown, title="Tokyo Break Down", text="Tokyo Break", style=shape.labeldown, location=location.abovebar, color=tokyoColor, textcolor=color.black, size=size.tiny)
plotshape(showRetestSignals and tokyoRetestUp, title="Tokyo Bullish Retest", text="Tokyo Retest", style=shape.labelup, location=location.belowbar, color=color.new(tokyoColor, 10), textcolor=color.black, size=size.tiny)
plotshape(showRetestSignals and tokyoRetestDown, title="Tokyo Bearish Retest", text="Tokyo Retest", style=shape.labeldown, location=location.abovebar, color=color.new(tokyoColor, 10), textcolor=color.black, size=size.tiny)
// ── Create or update right-edge tags
updateTag(label currentTag, bool visible, float price, string tagText, color tagColor, int direction) =>
label result = currentTag
if barstate.islast
if visible and not na(price)
float tagPrice = price + syminfo.mintick * tagGapTicks * direction
if na(result)
result := label.new(bar_index + tagOffsetBars, tagPrice, tagText, style=label.style_label_left, color=color.new(tagColor, tagTransparency), textcolor=tagColor, size=tagSize)
else
label.set_xy(result, bar_index + tagOffsetBars, tagPrice)
label.set_text(result, tagText)
label.set_color(result, color.new(tagColor, tagTransparency))
label.set_textcolor(result, tagColor)
label.set_size(result, tagSize)
else
if not na(result)
label.delete(result)
result := na
result
// ── Right-edge tags
var label nyHighTag = na
var label nyLowTag = na
var label londonHighTag = na
var label londonLowTag = na
var label tokyoHighTag = na
var label tokyoLowTag = na
nyHighTag := updateTag(nyHighTag, showTags and showLevels and enableNY and nyReady, nyHigh, "NY ORH", nyColor, 1)
nyLowTag := updateTag(nyLowTag, showTags and showLevels and enableNY and nyReady, nyLow, "NY ORL", nyColor, -1)
londonHighTag := updateTag(londonHighTag, showTags and showLevels and enableLondon and londonReady, londonHigh, "LON ORH", londonColor, 1)
londonLowTag := updateTag(londonLowTag, showTags and showLevels and enableLondon and londonReady, londonLow, "LON ORL", londonColor, -1)
tokyoHighTag := updateTag(tokyoHighTag, showTags and showLevels and enableTokyo and tokyoReady, tokyoHigh, "TKY ORH", tokyoColor, 1)
tokyoLowTag := updateTag(tokyoLowTag, showTags and showLevels and enableTokyo and tokyoReady, tokyoLow, "TKY ORL", tokyoColor, -1)
// ── Alerts
alertcondition(nyBreakUp, "NY Break Up", "Price closed above the New York opening range.")
alertcondition(nyBreakDown, "NY Break Down", "Price closed below the New York opening range.")
alertcondition(nyRetestUp, "NY Bullish Retest", "Price retested and closed above the New York opening-range high.")
alertcondition(nyRetestDown, "NY Bearish Retest", "Price retested and closed below the New York opening-range low.")
alertcondition(londonBreakUp, "London Break Up", "Price closed above the London opening range.")
alertcondition(londonBreakDown, "London Break Down", "Price closed below the London opening range.")
alertcondition(londonRetestUp, "London Bullish Retest", "Price retested and closed above the London opening-range high.")
alertcondition(londonRetestDown, "London Bearish Retest", "Price retested and closed below the London opening-range low.")
alertcondition(tokyoBreakUp, "Tokyo Break Up", "Price closed above the Tokyo opening range.")
alertcondition(tokyoBreakDown, "Tokyo Break Down", "Price closed below the Tokyo opening range.")
alertcondition(tokyoRetestUp, "Tokyo Bullish Retest", "Price retested and closed above the Tokyo opening-range high.")
alertcondition(tokyoRetestDown, "Tokyo Bearish Retest", "Price retested and closed below the Tokyo opening-range low.")