本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// [DAOL] Range Escape Quality v1.0
// Original implementation for this request; no Community script code copied.
// Foundations: Donchian-style prior-price channels and Perry Kaufman's ER.
// https://www.tradingview.com/support/solutions/43000502253-donchian-channels-dc/
// https://www.tradingview.com/support/solutions/43000773012-kaufman-s-adaptive-moving-average-kama/
// This event score is not Kaufman's original ER, a win probability or a trade signal.
// Standard OHLC charts only. All state mutations and alerts use confirmed bars.
// Breakout bar is the origin; observationBars means SUBSEQUENT closed bars.
// Only one event at a time. No new event on the bar an existing event ends.
// The latest completed result persists until a new breakout begins.
// Numerical validation only; TradingView compilation/market validation pending.
indicator("[DAOL] Range Escape Quality", shorttitle="DAOL Escape", overlay=true, max_bars_back=1000)
int rangeBars = input.int(20, "Prior range bars", minval=2, maxval=500, group="Event")
int observationBars = input.int(5, "Closed bars after breakout", minval=1, maxval=100, group="Event")
float minEfficiency = input.float(60.0, "Minimum directional efficiency (%)", minval=0, maxval=100, step=5, group="Quality")
float maxGiveback = input.float(30.0, "Maximum giveback (%)", minval=0, maxval=100, step=5, group="Quality")
bool showLive = input.bool(true, "Show prior rolling range", group="Display")
bool showFrozen = input.bool(true, "Show frozen event range", group="Display")
bool showMarkers = input.bool(true, "Show event markers", group="Display")
bool showDashboard = input.bool(true, "Show dashboard", group="Display")
if barstate.isfirst and not chart.is_standard
runtime.error("Use a standard OHLC/candlestick chart.")
float upper = ta.highest(high[1], rangeBars)
float lower = ta.lowest(low[1], rangeBars)
bool enough = bar_index >= rangeBars and not na(upper) and not na(lower) and upper > lower
var bool active = false
var int direction = 0
var int elapsed = 0
var float frozenUpper = na
var float frozenLower = na
var float origin = na
var float previousClose = na
var float path = 0.0
var float peakProgress = 0.0
var float progress = na
var float efficiency = na
var float giveback = na
var float score = na
// Result: 0 none, 1 quality up, -1 quality down, 2 weak, 3 returned.
var int result = 0
var string status = "Waiting for breakout"
bool startedUp = false
bool startedDown = false
bool completed = false
bool qualityUp = false
bool qualityDown = false
bool weakResult = false
bool returnedResult = false
if barstate.isconfirmed
if active
elapsed += 1
path += math.abs(close - previousClose)
previousClose := close
progress := direction * (close - origin)
peakProgress := math.max(peakProgress, progress)
// Directional ER clips adverse net progress to zero.
efficiency := path > 0 ? 100.0 * math.max(progress, 0.0) / path : 0.0
efficiency := math.min(100.0, efficiency)
// Peak is measured from the breakout CLOSE, not the old range boundary.
// No positive progress means 100% giveback by explicit convention.
giveback := peakProgress > 0 ? 100.0 * (peakProgress - math.max(progress, 0.0)) / peakProgress : 100.0
giveback := math.max(0.0, math.min(100.0, giveback))
score := efficiency * (1.0 - giveback / 100.0)
bool returned = direction == 1 ? close <= frozenUpper : close >= frozenLower
if returned or elapsed >= observationBars
active := false
completed := true
if returned
result := 3
score := 0.0
status := "Returned across breakout boundary"
returnedResult := true
else if progress > 0 and efficiency >= minEfficiency and giveback <= maxGiveback
result := direction
status := direction == 1 ? "Quality up escape" : "Quality down escape"
qualityUp := direction == 1
qualityDown := direction == -1
else
result := 2
status := "Outside / weak follow-through"
weakResult := true
else
status := "Tracking closed bars"
else if enough
// Previous close must be within the CURRENT prior range.
// This avoids treating every bar already beyond a rolling edge as a new escape.
bool wasInside = close[1] <= upper and close[1] >= lower
int newDirection = wasInside and close > upper ? 1 : wasInside and close < lower ? -1 : 0
if newDirection != 0
active := true
direction := newDirection
elapsed := 0
frozenUpper := upper
frozenLower := lower
origin := close
previousClose := close
path := 0.0
peakProgress := 0.0
progress := 0.0
efficiency := na
giveback := na
score := na
result := 0
status := "Tracking closed bars"
startedUp := direction == 1
startedDown := direction == -1
color eventColor = active ? color.blue : result == 1 ? color.lime : result == -1 ? color.red : result == 2 ? color.rgb(212, 175, 55) : color.gray
plot(showLive and enough ? upper : na, "Prior range high", color=color.new(color.gray, 65), style=plot.style_stepline)
plot(showLive and enough ? lower : na, "Prior range low", color=color.new(color.gray, 65), style=plot.style_stepline)
// Hide reset bar to avoid a sloping connection between different frozen ranges.
bool resetBar = startedUp or startedDown
plot(showFrozen and not resetBar ? frozenUpper : na, "Frozen range high", color=eventColor, linewidth=2, style=plot.style_linebr)
plot(showFrozen and not resetBar ? frozenLower : na, "Frozen range low", color=eventColor, linewidth=2, style=plot.style_linebr)
plot(showFrozen and not resetBar ? origin : na, "Breakout close origin", color=color.new(eventColor, 40), style=plot.style_linebr)
plot(score, "Quality score (0-100)", display=display.data_window)
plot(efficiency, "Directional efficiency (%)", display=display.data_window)
plot(giveback, "Giveback (%)", display=display.data_window)
plotshape(showMarkers and startedUp, title="Up breakout starts", style=shape.triangleup, location=location.belowbar, color=color.blue, size=size.tiny, text="Start")
plotshape(showMarkers and startedDown, title="Down breakout starts", style=shape.triangledown, location=location.abovebar, color=color.blue, size=size.tiny, text="Start")
plotshape(showMarkers and qualityUp, title="Quality up completed", style=shape.circle, location=location.belowbar, color=color.lime, size=size.small, text="Q")
plotshape(showMarkers and qualityDown, title="Quality down completed", style=shape.circle, location=location.abovebar, color=color.red, size=size.small, text="Q")
plotshape(showMarkers and weakResult, title="Weak escape completed", style=shape.diamond, location=location.abovebar, color=color.rgb(212, 175, 55), size=size.tiny, text="Weak")
plotshape(showMarkers and returnedResult, title="Returned across edge", style=shape.xcross, location=location.abovebar, color=color.gray, size=size.tiny, text="Back")
f_number(float value) =>
na(value) ? "--" : str.tostring(value, "#.0")
var table dashboard = table.new(position.top_right, 2, 9, border_width=1)
if barstate.islast and showDashboard
table.cell(dashboard, 0, 0, "DAOL Escape Quality", text_color=color.white, bgcolor=color.rgb(19, 62, 103))
table.cell(dashboard, 1, 0, active ? "LIVE EVENT" : result == 0 ? "WAITING" : "LAST RESULT", text_color=color.white, bgcolor=eventColor)
table.cell(dashboard, 0, 1, "Status")
table.cell(dashboard, 1, 1, status)
table.cell(dashboard, 0, 2, "Direction")
table.cell(dashboard, 1, 2, direction == 1 ? "Up" : direction == -1 ? "Down" : "--")
table.cell(dashboard, 0, 3, "Bars after breakout")
table.cell(dashboard, 1, 3, str.tostring(elapsed) + " / " + str.tostring(observationBars))
table.cell(dashboard, 0, 4, "Net progress (price)")
table.cell(dashboard, 1, 4, na(progress) ? "--" : str.tostring(progress, format.mintick))
table.cell(dashboard, 0, 5, "Close path (price)")
table.cell(dashboard, 1, 5, str.tostring(path, format.mintick))
table.cell(dashboard, 0, 6, "Efficiency (%)")
table.cell(dashboard, 1, 6, f_number(efficiency))
table.cell(dashboard, 0, 7, "Giveback (%)")
table.cell(dashboard, 1, 7, f_number(giveback))
table.cell(dashboard, 0, 8, "Quality (0-100)")
table.cell(dashboard, 1, 8, f_number(score), bgcolor=color.new(eventColor, 75))
alertcondition(qualityUp, "DAOL: Quality up escape", "[DAOL] Range Escape Quality: upward event completed with quality thresholds met. {{ticker}} {{interval}}")
alertcondition(qualityDown, "DAOL: Quality down escape", "[DAOL] Range Escape Quality: downward event completed with quality thresholds met. {{ticker}} {{interval}}")
alertcondition(weakResult, "DAOL: Weak escape", "[DAOL] Range Escape Quality: event completed outside the range with weak follow-through. {{ticker}} {{interval}}")
alertcondition(returnedResult, "DAOL: Returned across boundary", "[DAOL] Range Escape Quality: close returned across the frozen breakout boundary. {{ticker}} {{interval}}")
alertcondition(completed, "DAOL: Any completed event", "[DAOL] Range Escape Quality: an event completed. {{ticker}} {{interval}}")
// Reading/limitations:
// Default quality: positive progress, efficiency >=60%, giveback <=30%.
// Score = directional efficiency * retained fraction of peak progress.
// Returning to/touching the breakout edge ends immediately with score zero;
// a gap through the entire range also counts as a return across that edge.
// Price path uses closes only, excludes intrabar excursions and breakout jump.
// Blue = tracking; green/red = completed quality; gold = completed weak;
// gray = returned/no result. A retained result is not a fresh signal each bar.
// Fixed horizon means output delay; no additional two-bar confirmation.
// Continuous-futures rolls, session gaps and revised data can affect results.
// Set alerts Once Per Bar Close; recreate after changing code or inputs.
// Educational context only; no profitability or originality-of-basics claim.