Signed Excursion Recovery Oscillator [Pineify]

Pineify · study · 210 行 · 点赞 26 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Signed Excursion Recovery Oscillator [Pineify]", overlay = false, max_bars_back = 600)
 
// A rolling N-step path uses the close and ATR at its own start.
// Extremes exclude the anchor bar. No within-bar high/low order is inferred.
int pathLength = input.int(24, "Path length", minval = 5, maxval = 200, group = "Path")
int atrLength = input.int(14, "Anchor ATR length", minval = 2, maxval = 100, group = "Path")
float minExcursion = input.float(1.5, "Minimum favorable excursion (anchor ATR)", minval = 0.2, maxval = 10.0, step = 0.1, group = "State")
float highRetention = input.float(0.72, "High retention", minval = 0.55, maxval = 0.95, step = 0.01, group = "State")
float lowRetention = input.float(0.35, "Giveback retention", minval = 0.05, maxval = 0.45, step = 0.01, group = "State")
float adverseGate = input.float(0.30, "Recovery adverse share", minval = 0.10, maxval = 0.60, step = 0.01, group = "State")
float hysteresis = input.float(0.06, "Retention/share hysteresis", minval = 0.01, maxval = 0.09, step = 0.01, group = "State")
float deadZone = input.float(0.10, "Endpoint dead zone (anchor ATR)", minval = 0.01, maxval = 0.50, step = 0.01, group = "State")
int confirmBars = input.int(2, "Consecutive candidate closes", minval = 1, maxval = 5, group = "State")
bool showHalo = input.bool(true, "Retention halo", group = "Visual")
bool showBackground = input.bool(true, "Confirmed state background", group = "Visual")
bool showMarkers = input.bool(true, "Confirmed transition markers", group = "Visual")
bool showPreview = input.bool(false, "Live endpoint preview", group = "Visual")
bool showTable = input.bool(true, "Path summary", group = "Visual")
 
color upColor = color.rgb(0, 155, 143)
color downColor = color.rgb(215, 78, 91)
color recoveryColor = color.rgb(118, 89, 209)
color givebackColor = color.rgb(184, 125, 26)
color neutralColor = chart.fg_color
 
f_name(int state) =>
    int mode = math.abs(state)
    string side = state > 0 ? "Up " : "Down "
    mode == 1 ? side + "advance" : mode == 2 ? side + "giveback" : mode == 3 ? side + "recovery" : "Noise / mixed"
 
f_color(int state) =>
    int mode = math.abs(state)
    mode == 2 ? givebackColor : mode == 3 ? recoveryColor : mode == 1 ? (state > 0 ? upColor : downColor) : neutralColor
 
f_number(float value) =>
    na(value) ? "n/a" : str.tostring(value, "#.##")
 
float atr = ta.atr(atrLength)
float anchor = close[pathLength]
float scale = atr[pathLength]
float highestPath = anchor
float lowestPath = anchor
int highAge = na
int lowAge = na
bool valid = bar_index >= pathLength and not na(anchor) and not na(scale) and scale > 0 and syminfo.mintick > 0
if valid
    // Newest equal extreme wins: strict updates while scanning newest to oldest.
    for i = 0 to pathLength - 1
        bool sampleValid = not na(open[i]) and not na(high[i]) and not na(low[i]) and not na(close[i]) and high[i] >= math.max(open[i], close[i]) and low[i] <= math.min(open[i], close[i])
        if not sampleValid
            valid := false
        else
            if high[i] > highestPath
                highestPath := high[i]
                highAge := i
            if low[i] < lowestPath
                lowestPath := low[i]
                lowAge := i
 
float upExcursion = valid ? math.max(highestPath - anchor, 0.0) / scale : na
float downExcursion = valid ? math.max(anchor - lowestPath, 0.0) / scale : na
float net = valid ? (close - anchor) / scale : na
float effectiveDeadZone = valid ? math.max(deadZone, syminfo.mintick / scale) : na
int direction = valid ? (net > effectiveDeadZone ? 1 : net < -effectiveDeadZone ? -1 : 0) : 0
float favorable = valid ? (direction > 0 ? upExcursion : direction < 0 ? downExcursion : 0.0) : na
float adverse = valid ? (direction > 0 ? downExcursion : direction < 0 ? upExcursion : 0.0) : na
float retention = valid and direction != 0 and favorable > 0 ? math.min(1.0, math.abs(net) / favorable) : na
float adverseShare = valid and direction != 0 and favorable + adverse > 0 ? adverse / (favorable + adverse) : na
bool orderKnown = valid and not na(highAge) and not na(lowAge) and highAge != lowAge
bool recoveredOrder = orderKnown and (direction > 0 ? lowAge > highAge : direction < 0 ? highAge > lowAge : false)
 
var int phase = 0
var int pending = 0
var int pendingCount = 0
var bool covered = false
var float confirmedUp = na
var float confirmedDown = na
var float confirmedNet = na
var float confirmedRetention = na
var float confirmedAdverseShare = na
var float confirmedScale = na
var float confirmedAnchor = na
var bool confirmedOrder = false
var int confirmedDirection = 0
 
int candidate = 0
if valid and direction != 0
    bool sameSide = phase * direction > 0
    int oldMode = sameSide ? math.abs(phase) : 0
    float excursionGate = minExcursion * (oldMode != 0 ? 0.85 : 1.0)
    if favorable >= excursionGate
        // Recovery candidates require distinct-bar adverse -> favorable order.
        float recoveryRetention = highRetention - (oldMode == 3 ? hysteresis : 0.0)
        float recoveryShare = adverseGate - (oldMode == 3 ? hysteresis : 0.0)
        float advanceRetention = highRetention - (oldMode == 1 ? hysteresis : 0.0)
        float advanceShare = adverseGate + (oldMode == 1 ? hysteresis : 0.0)
        float givebackRetention = lowRetention + (oldMode == 2 ? hysteresis : 0.0)
        if retention >= recoveryRetention and adverseShare >= recoveryShare and recoveredOrder
            candidate := direction * 3
        else if retention >= advanceRetention and adverseShare < advanceShare
            candidate := direction
        else if retention <= givebackRetention
            candidate := direction * 2
 
bool advanceEvent = false
bool givebackEvent = false
bool recoveryEvent = false
bool noiseEvent = false
bool coverageLost = false
bool coverageRestored = false
if barstate.isconfirmed
    int previousPhase = phase
    bool previousCoverage = covered
    covered := valid
    coverageLost := previousCoverage and not valid
    coverageRestored := not previousCoverage and valid
    if valid
        confirmedUp := upExcursion
        confirmedDown := downExcursion
        confirmedNet := net
        confirmedRetention := retention
        confirmedAdverseShare := adverseShare
        confirmedScale := scale
        confirmedAnchor := anchor
        confirmedOrder := recoveredOrder
        confirmedDirection := direction
        if candidate == pending
            pendingCount := math.min(pendingCount + 1, confirmBars)
        else
            pending := candidate
            pendingCount := 1
        if pendingCount >= confirmBars
            phase := candidate
        if phase != previousPhase
            advanceEvent := math.abs(phase) == 1
            givebackEvent := math.abs(phase) == 2
            recoveryEvent := math.abs(phase) == 3
            noiseEvent := phase == 0
    else
        phase := 0
        pending := 0
        pendingCount := 0
        confirmedUp := na
        confirmedDown := na
        confirmedNet := na
        confirmedRetention := na
        confirmedAdverseShare := na
        confirmedScale := na
        confirmedAnchor := na
        confirmedOrder := false
        confirmedDirection := 0
 
color stateColor = f_color(phase)
pUp = plot(confirmedUp, "Upper path excursion", color.new(upColor, 18), 2, plot.style_linebr)
pDown = plot(-confirmedDown, "Lower path excursion", color.new(downColor, 18), 2, plot.style_linebr)
pZero = plot(covered ? 0.0 : na, "Origin", color.new(neutralColor, 62), 1, plot.style_linebr)
pNet = plot(confirmedNet, "Confirmed signed endpoint", stateColor, 3, plot.style_linebr)
fill(pUp, pNet, color = color.new(upColor, 91), title = "Upper path surrendered space", fillgaps = false)
fill(pDown, pNet, color = color.new(downColor, 91), title = "Lower path surrendered space", fillgaps = false)
float haloWidth = covered and not na(confirmedRetention) ? 0.04 + 0.18 * confirmedRetention : na
pHaloUp = plot(showHalo ? confirmedNet + haloWidth : na, "Retention halo upper", display = display.none)
pHaloDown = plot(showHalo ? confirmedNet - haloWidth : na, "Retention halo lower", display = display.none)
fill(pHaloUp, pHaloDown, color = color.new(stateColor, 72), title = "Retention halo", fillgaps = false)
plot(showPreview and not barstate.isconfirmed and valid ? net : na, "Live endpoint candidate", color.new(neutralColor, 30), 2, plot.style_circles)
bgcolor(showBackground and covered and phase != 0 ? color.new(stateColor, 94) : na, title = "Confirmed path state")
plotshape(showMarkers and advanceEvent and phase > 0 ? confirmedNet : na, "Up advance transition", shape.triangleup, location.absolute, upColor, size = size.tiny)
plotshape(showMarkers and advanceEvent and phase < 0 ? confirmedNet : na, "Down advance transition", shape.triangledown, location.absolute, downColor, size = size.tiny)
plotshape(showMarkers and givebackEvent ? confirmedNet : na, "Giveback transition", shape.diamond, location.absolute, givebackColor, size = size.tiny)
plotshape(showMarkers and recoveryEvent ? confirmedNet : na, "Recovery transition", shape.circle, location.absolute, recoveryColor, size = size.tiny)
 
var table summary = table.new(position.top_right, 3, 9, bgcolor = chart.bg_color, frame_color = color.new(neutralColor, 75), frame_width = 1)
if barstate.islast
    if showTable
        table.cell(summary, 0, 0, "Path / anchor ATR", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 0, "Confirmed", text_color = stateColor, text_size = size.small)
        table.cell(summary, 2, 0, "Candidate", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 1, "State", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 1, covered ? f_name(phase) : "Unavailable", text_color = stateColor, text_size = size.small)
        table.cell(summary, 2, 1, valid ? f_name(candidate) : "Unavailable", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 2, "Net", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 2, f_number(confirmedNet), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 2, f_number(net), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 3, "Up / down", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 3, f_number(confirmedUp) + " / " + f_number(confirmedDown), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 3, f_number(upExcursion) + " / " + f_number(downExcursion), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 4, "Retention %", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 4, f_number(confirmedRetention * 100), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 4, f_number(retention * 100), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 5, "Adverse share %", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 5, f_number(confirmedAdverseShare * 100), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 5, f_number(adverseShare * 100), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 6, "Recovery order", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 6, covered ? (confirmedOrder ? "Eligible" : "Not eligible") : "n/a", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 6, valid ? (recoveredOrder ? "Eligible" : "Not eligible") : "n/a", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 7, "Origin / scale", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 7, f_number(confirmedAnchor) + " / " + f_number(confirmedScale), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 7, f_number(anchor) + " / " + f_number(scale), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 0, 8, "Pending closes", text_color = neutralColor, text_size = size.small)
        table.cell(summary, 1, 8, str.tostring(pendingCount) + "/" + str.tostring(confirmBars), text_color = neutralColor, text_size = size.small)
        table.cell(summary, 2, 8, "Rolling " + str.tostring(pathLength), text_color = neutralColor, text_size = size.small)
    else
        table.clear(summary, 0, 0, 2, 8)
 
alertcondition(advanceEvent, "High retention advance", "Confirmed high-retention path advance. Inspect direction and anchor ATR.")
alertcondition(givebackEvent, "Significant path giveback", "Confirmed favorable excursion giveback. This is path context, not an order.")
alertcondition(recoveryEvent, "Ordered excursion recovery", "Confirmed recovery: adverse extreme preceded favorable extreme on distinct bars.")
alertcondition(noiseEvent, "Path returned to noise", "Confirmed path state returned to noise or mixed conditions.")
alertcondition(coverageLost, "Path coverage lost", "Complete OHLC path or positive anchor ATR is unavailable.")
alertcondition(coverageRestored, "Path coverage restored", "Complete OHLC path and positive anchor ATR are available.")

← 返回列表