本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Oreal | Consolidation Breakout Retest v1.0", overlay=true, max_boxes_count=50, max_lines_count=50, max_labels_count=100, max_bars_back=1000)
// Original implementation prepared for Oreal, 2026.
// Chart-timeframe OHLC only. Standard candles; no other-timeframe requests.
// A range is recognized AFTER N closed candles, then drawn back over that window.
// That historical left anchor is not an earlier-available signal.
// Range prices lock at detection. Wicks outside never expand the confirmed box.
// Breakout: qualifying candle body and CLOSE beyond a locked boundary.
// Retest: a LATER closed candle overlaps that boundary and closes outside again.
// One active range/break/retest sequence; no automatic orders or trade results.
rangeBars = input.int(12, "Candles needed for a range", minval=5, maxval=100, group="01 RANGE DETECTION")
atrLength = input.int(14, "ATR baseline length", minval=2, maxval=200, group="01 RANGE DETECTION")
maxWidthATR = input.float(3.0, "Maximum box height / pre-window ATR", minval=0.2, maxval=10, step=0.1, group="01 RANGE DETECTION", tooltip="ATR is sampled before the candidate window. Lower values require a tighter range.")
minHeightTicks = input.int(4, "Minimum box height (ticks)", minval=1, group="01 RANGE DETECTION")
maxEfficiency = input.float(0.35, "Maximum directional efficiency", minval=0, maxval=1, step=0.05, group="01 RANGE DETECTION", tooltip="Absolute first-to-last close change divided by the sum of close-to-close changes inside the window. Lower values require more sideways movement.")
minTouches = input.int(2, "Minimum candles near EACH edge", minval=1, maxval=10, group="01 RANGE DETECTION")
touchBandPct = input.float(15, "Edge-touch band (% of box height)", minval=1, maxval=40, group="01 RANGE DETECTION")
maxRangeAge = input.int(120, "Expire unbroken range after bars", minval=5, maxval=2000, group="01 RANGE DETECTION")
enableUp = input.bool(true, "Upward breakouts", group="02 BODY BREAKOUT")
enableDown = input.bool(true, "Downward breakouts", group="02 BODY BREAKOUT")
breakBufferTicks = input.int(1, "Close beyond boundary (ticks)", minval=1, group="02 BODY BREAKOUT")
minBreakBodyPct = input.float(50, "Minimum candle body / full range (%)", minval=0, maxval=100, group="02 BODY BREAKOUT")
minOutsideBodyPct = input.float(25, "Minimum body beyond boundary (%)", minval=0, maxval=100, group="02 BODY BREAKOUT", tooltip="25 means at least one quarter of the candle body must lie beyond the broken boundary. 100 requires the entire body outside.")
requireBreakDirection = input.bool(true, "Require matching breakout candle direction", group="02 BODY BREAKOUT")
retestBars = input.int(20, "Maximum bars to wait after breakout", minval=1, maxval=2000, group="03 RETEST")
toleranceTicks = input.int(4, "Boundary tolerance (ticks)", minval=0, group="03 RETEST")
minRetestBodyPct = input.float(20, "Minimum retest body / full range (%)", minval=0, maxval=100, group="03 RETEST")
requireRetestDirection = input.bool(true, "Require matching retest candle direction", group="03 RETEST")
keepRanges = input.int(8, "Keep recent range sequences", minval=1, maxval=40, group="04 DISPLAY")
extendBars = input.int(3, "Active drawing extension (bars)", minval=0, maxval=50, group="04 DISPLAY")
showBoxText = input.bool(true, "Timeframe name inside box", group="04 DISPLAY")
showRetestLine = input.bool(true, "Dashed broken-boundary line", group="04 DISPLAY")
showStatus = input.bool(true, "Compact status panel", group="04 DISPLAY")
textSizeInput = input.string("Small", "Text size", options=["Tiny", "Small", "Normal"], group="04 DISPLAY")
rangeColor = input.color(color.rgb(80, 145, 245), "Range border / text", group="05 COLORS")
rangeTransparency = input.int(91, "Range fill transparency", minval=0, maxval=100, group="05 COLORS")
upColor = input.color(color.rgb(0, 185, 155), "Upward BREAKOUT", group="05 COLORS")
downColor = input.color(color.rgb(240, 95, 115), "Downward BREAKOUT", group="05 COLORS")
retestColor = input.color(color.rgb(80, 205, 230), "RETEST", group="05 COLORS")
inactiveColor = input.color(color.rgb(135, 145, 155), "Expired / failed drawing", group="05 COLORS")
if barstate.isfirst and not chart.is_standard
runtime.error("Use standard OHLC candles. This indicator is designed for the active chart timeframe, not synthetic candle prices.")
string textSize = textSizeInput == "Tiny" ? size.tiny : textSizeInput == "Normal" ? size.normal : size.small
string tfText = timeframe.isseconds ? str.tostring(timeframe.multiplier)+"s" : timeframe.isminutes ? (timeframe.multiplier % 60 == 0 ? str.tostring(timeframe.multiplier/60, "#")+"H" : str.tostring(timeframe.multiplier)+"m") : timeframe.isdaily ? str.tostring(timeframe.multiplier)+"D" : timeframe.isweekly ? str.tostring(timeframe.multiplier)+"W" : timeframe.ismonthly ? str.tostring(timeframe.multiplier)+"M" : timeframe.period
float tick = syminfo.mintick
float atrValue = ta.atr(atrLength)
float baselineATR = atrValue[rangeBars]
float candidateHigh = ta.highest(high, rangeBars)
float candidateLow = ta.lowest(low, rangeBars)
float candidateHeight = candidateHigh - candidateLow
float closePath = ta.sma(math.abs(ta.change(close)), rangeBars-1)*(rangeBars-1)
float efficiency = closePath > 0 ? math.abs(close-close[rangeBars-1])/closePath : 0.0
float candleHeight = high-low
float candleBody = math.abs(close-open)
float bodyPct = candleHeight > 0 ? 100*candleBody/candleHeight : 0.0
float buffer = breakBufferTicks*tick
float tolerance = toleranceTicks*tick
type RangeDrawing
box area
line boundary
label breakout
label retest
var array<RangeDrawing> drawings = array.new<RangeDrawing>()
var RangeDrawing active = na
// 0: search, 1: locked range, 2: waiting for a later retest.
var int phase = 0
var int searchFrom = 0
var int formedBar = na
var int breakBar = na
var int direction = 0
var float rangeHigh = na
var float rangeLow = na
var string lastEvent = "NONE"
bool breakoutUp = false
bool breakoutDown = false
bool retestUp = false
bool retestDown = false
f_label(int side, string caption, color col) =>
label.new(bar_index, side == 1 ? low : high, caption+"\n"+tfText, yloc=side == 1 ? yloc.belowbar : yloc.abovebar, style=side == 1 ? label.style_label_up : label.style_label_down, color=color.new(col, 86), textcolor=col, size=textSize)
f_delete(RangeDrawing item) =>
box.delete(item.area)
if not na(item.boundary)
line.delete(item.boundary)
if not na(item.breakout)
label.delete(item.breakout)
if not na(item.retest)
label.delete(item.retest)
true
if barstate.isconfirmed
if phase == 1
bool closeAbove = close >= rangeHigh+buffer
bool closeBelow = close <= rangeLow-buffer
float bodyTop = math.max(open, close)
float bodyBottom = math.min(open, close)
float outsideUp = candleBody > 0 ? 100*math.max(0.0, bodyTop-math.max(bodyBottom, rangeHigh))/candleBody : 0.0
float outsideDown = candleBody > 0 ? 100*math.max(0.0, math.min(bodyTop, rangeLow)-bodyBottom)/candleBody : 0.0
bool upPass = enableUp and closeAbove and candleBody > 0 and bodyPct >= minBreakBodyPct and outsideUp >= minOutsideBodyPct and (not requireBreakDirection or close > open)
bool downPass = enableDown and closeBelow and candleBody > 0 and bodyPct >= minBreakBodyPct and outsideDown >= minOutsideBodyPct and (not requireBreakDirection or close < open)
if upPass or downPass
direction := upPass ? 1 : -1
breakBar := bar_index
phase := 2
breakoutUp := upPass
breakoutDown := downPass
box.set_right(active.area, bar_index)
active.breakout := f_label(direction, "BREAKOUT", upPass ? upColor : downColor)
if showRetestLine
float boundaryPrice = upPass ? rangeHigh : rangeLow
active.boundary := line.new(bar_index, boundaryPrice, bar_index+extendBars, boundaryPrice, color=upPass ? upColor : downColor, style=line.style_dashed)
lastEvent := upPass ? "BREAKOUT UP" : "BREAKOUT DOWN"
else if closeAbove or closeBelow or bar_index-formedBar >= maxRangeAge
// The first closing exit consumes the range even when body filters fail.
// Never label a later candle as a first breakout of an already exited box.
box.set_right(active.area, bar_index)
box.set_border_color(active.area, inactiveColor)
box.set_bgcolor(active.area, color.new(inactiveColor, rangeTransparency))
box.set_text_color(active.area, inactiveColor)
lastEvent := closeAbove or closeBelow ? "EXIT: BODY FILTER NOT MET" : "RANGE EXPIRED"
phase := 0
searchFrom := bar_index+1
active := na
else if phase == 2
float boundaryPrice = direction == 1 ? rangeHigh : rangeLow
bool expired = bar_index-breakBar > retestBars
bool backInside = direction == 1 ? close < boundaryPrice-tolerance : close > boundaryPrice+tolerance
bool overlaps = low <= boundaryPrice+tolerance and high >= boundaryPrice-tolerance
bool outsideClose = direction == 1 ? close >= boundaryPrice+buffer : close <= boundaryPrice-buffer
bool directionPass = not requireRetestDirection or (direction == 1 ? close > open : close < open)
bool retestPass = bar_index > breakBar and overlaps and outsideClose and candleBody > 0 and bodyPct >= minRetestBodyPct and directionPass
if expired or backInside
if not na(active.boundary)
line.set_x2(active.boundary, bar_index)
line.set_color(active.boundary, inactiveColor)
lastEvent := expired ? "RETEST WINDOW EXPIRED" : "RETEST INVALIDATED"
phase := 0
searchFrom := bar_index+1
active := na
else if retestPass
retestUp := direction == 1
retestDown := direction == -1
active.retest := f_label(direction, "RETEST", retestColor)
if not na(active.boundary)
line.set_x2(active.boundary, bar_index)
lastEvent := direction == 1 ? "RETEST UP CONFIRMED" : "RETEST DOWN CONFIRMED"
phase := 0
searchFrom := bar_index+1
active := na
bool enoughHistory = not na(baselineATR) and baselineATR > 0 and bar_index-rangeBars+1 >= searchFrom
bool compact = enoughHistory and candidateHeight >= minHeightTicks*tick and candidateHeight <= baselineATR*maxWidthATR and efficiency <= maxEfficiency
if phase == 0 and compact
float edgeBand = candidateHeight*touchBandPct/100.0
int upperTouches = 0
int lowerTouches = 0
for i = 0 to rangeBars-1
if high[i] >= candidateHigh-edgeBand
upperTouches += 1
if low[i] <= candidateLow+edgeBand
lowerTouches += 1
if upperTouches >= minTouches and lowerTouches >= minTouches
rangeHigh := candidateHigh
rangeLow := candidateLow
formedBar := bar_index
breakBar := na
direction := 0
box area = box.new(bar_index-rangeBars+1, rangeHigh, bar_index+extendBars, rangeLow, border_color=rangeColor, border_width=1, bgcolor=color.new(rangeColor, rangeTransparency), text=showBoxText ? "RANGE "+tfText : "", text_color=rangeColor, text_size=textSize, text_halign=text.align_left, text_valign=text.align_top)
active := RangeDrawing.new(area, na, na, na)
drawings.push(active)
if drawings.size() > keepRanges
f_delete(drawings.shift())
phase := 1
lastEvent := "RANGE CONFIRMED"
// Intrabar updates extend drawings horizontally, never change locked range prices.
if phase == 1 and not na(active)
box.set_right(active.area, bar_index+extendBars)
if phase == 2 and not na(active)
if not na(active.boundary)
line.set_x2(active.boundary, bar_index+extendBars)
var table status = table.new(position.bottom_right, 2, 3, border_width=1)
if barstate.islast
table.clear(status, 0, 0, 1, 2)
if showStatus
string phaseText = phase == 1 ? "RANGE ACTIVE" : phase == 2 ? (direction == 1 ? "WAIT RETEST UP" : "WAIT RETEST DOWN") : "SEARCHING"
color phaseColor = phase == 1 ? rangeColor : phase == 2 ? (direction == 1 ? upColor : downColor) : chart.fg_color
table.cell(status, 0, 0, "OREAL RANGE", text_color=rangeColor, bgcolor=color.new(chart.bg_color, 5), text_size=textSize)
table.cell(status, 1, 0, tfText, text_color=chart.fg_color, bgcolor=color.new(chart.bg_color, 5), text_size=textSize)
table.cell(status, 0, 1, "STATUS", text_color=chart.fg_color, bgcolor=color.new(chart.bg_color, 5), text_size=textSize)
table.cell(status, 1, 1, phaseText, text_color=phaseColor, bgcolor=color.new(chart.bg_color, 5), text_size=textSize)
table.cell(status, 0, 2, "LAST EVENT", text_color=chart.fg_color, bgcolor=color.new(chart.bg_color, 5), text_size=textSize)
table.cell(status, 1, 2, lastEvent, text_color=chart.fg_color, bgcolor=color.new(chart.bg_color, 5), text_size=textSize)
alertcondition(breakoutUp, "Upward body breakout", "Oreal: closed-body BREAKOUT UP from a confirmed consolidation box on {{ticker}} {{interval}}. Close: {{close}}.")
alertcondition(breakoutDown, "Downward body breakout", "Oreal: closed-body BREAKOUT DOWN from a confirmed consolidation box on {{ticker}} {{interval}}. Close: {{close}}.")
alertcondition(retestUp, "Upward retest confirmed", "Oreal: a later candle RETESTED the broken upper boundary and closed above it on {{ticker}} {{interval}}. Close: {{close}}.")
alertcondition(retestDown, "Downward retest confirmed", "Oreal: a later candle RETESTED the broken lower boundary and closed below it on {{ticker}} {{interval}}. Close: {{close}}.")