Gap Recovery Participation Map [Pineify]

Pineify · study · 203 行 · 点赞 139 · TradingView 原页

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

Pine Script

//@version=6
indicator("Gap Recovery Participation Map [Pineify]", shorttitle="Gap Recovery Map", overlay=true, max_bars_back=1000)
 
// Session-slot alignment adapts our Session Volume Pace Curve [Pineify].
// One exchange-local, same-day session; only fully observed sessions provide anchors.
string sessionHours = input.session("0930-1600", "Session (exchange time)")
string sessionDays = input.string("23456", "Session days", options=["23456", "1234567"])
int atrLength = input.int(14, "Prior ATR length", minval=2, maxval=100)
float minGapATR = input.float(0.25, "Minimum gap / prior ATR", minval=0.05, maxval=5.0, step=0.05)
int volumeLength = input.int(20, "Prior volume baseline bars", minval=5, maxval=100)
float minParticipation = input.float(0.75, "Minimum mean volume / baseline", minval=0.1, maxval=5.0, step=0.05)
int watchBars = input.int(10, "Maximum observation bars", minval=3, maxval=50)
int confirmation = input.int(2, "Consecutive state confirmations", minval=1, maxval=5)
bool showCorridor = input.bool(true, "Show gap corridor")
bool showMarkers = input.bool(true, "Show state markers")
bool showTable = input.bool(true, "Show status table")
 
int startHour = int(str.tonumber(str.substring(sessionHours, 0, 2)))
int startMinute = int(str.tonumber(str.substring(sessionHours, 2, 4)))
int endHour = int(str.tonumber(str.substring(sessionHours, 5, 7)))
int endMinute = int(str.tonumber(str.substring(sessionHours, 7, 9)))
int sessionMinutes = endHour * 60 + endMinute - startHour * 60 - startMinute
int chartMinutes = timeframe.isminutes ? timeframe.multiplier : 0
bool configOK = chartMinutes >= 1 and chartMinutes <= 30 and sessionMinutes > 0 and sessionMinutes % math.max(chartMinutes, 1) == 0
int slots = configOK ? int(sessionMinutes / chartMinutes) : 0
int slotMs = chartMinutes * 60000
bool inSession = configOK and not na(time(timeframe.period, sessionHours + ":" + sessionDays, syminfo.timezone))
int startTime = timestamp(syminfo.timezone, year, month, dayofmonth, startHour, startMinute)
int slot = inSession ? int(math.floor((time - startTime) / slotMs)) : -1
bool aligned = inSession and slot >= 0 and slot < slots and time == startTime + slot * slotMs and time_close == startTime + (slot + 1) * slotMs
bool validPrice = not na(open) and not na(high) and not na(low) and not na(close) and open > 0 and high >= low and low > 0 and close > 0
bool validVolume = not na(volume) and volume > 0
float priorATR = ta.atr(atrLength)[1]
float priorVolumeMean = ta.sma(volume, volumeLength)[1]
float priorVolumeCoverage = ta.sma(validVolume ? 1.0 : 0.0, volumeLength)[1]
bool baselineOK = not na(priorATR) and priorATR > syminfo.mintick and not na(priorVolumeMean) and priorVolumeMean > 0 and priorVolumeCoverage == 1.0
 
var int activeStart = na
var int lastSlot = -1
var bool priceIntact = false
var bool lastComplete = false
var float lastSessionClose = na
var float referenceClose = na
var float sessionOpen = na
var float frozenATR = na
var float frozenVolume = na
var float gapSize = na
var int gapDirection = 0
var bool eligible = false
var bool trackingIntact = false
var int age = 0
var int outsideCloses = 0
var float pathLength = 0.0
var float lastTrackedClose = na
var float maxRecovery = 0.0
var float volumeSum = 0.0
var float outsideVolume = 0.0
var float insideVolume = 0.0
// 0 observing/unavailable, 1 acceptance, 2 recovery, 3 tug-of-war, 4 expired/invalid.
var int state = 0
var int pending = 0
var int pendingCount = 0
bool enteredAcceptance = false
bool enteredRecovery = false
bool enteredTug = false
bool expired = false
 
bool newSession = inSession and (na(activeStart) or activeStart != startTime)
if barstate.isconfirmed and inSession
    if newSession
        referenceClose := lastComplete ? lastSessionClose : na
        sessionOpen := slot == 0 and aligned and validPrice ? open : na
        frozenATR := priorATR
        frozenVolume := priorVolumeMean
        gapSize := not na(referenceClose) and not na(sessionOpen) ? sessionOpen - referenceClose : na
        gapDirection := not na(gapSize) ? (gapSize > 0 ? 1 : gapSize < 0 ? -1 : 0) : 0
        eligible := slot == 0 and aligned and validPrice and baselineOK and not na(gapSize) and math.abs(gapSize) >= minGapATR * frozenATR and math.abs(gapSize) > syminfo.mintick
        trackingIntact := eligible
        activeStart := startTime
        lastSlot := -1
        priceIntact := slot == 0 and aligned
        lastComplete := false
        lastSessionClose := na
        age := 0
        outsideCloses := 0
        pathLength := 0.0
        lastTrackedClose := sessionOpen
        maxRecovery := 0.0
        volumeSum := 0.0
        outsideVolume := 0.0
        insideVolume := 0.0
        state := 0
        pending := 0
        pendingCount := 0
    int oldState = state
    bool contiguous = priceIntact and aligned and slot == lastSlot + 1 and validPrice
    if not contiguous
        priceIntact := false
        if eligible and state != 4
            trackingIntact := false
            state := 4
            expired := true
    else
        lastSlot := slot
        if eligible and trackingIntact and state != 4
            if not validVolume
                trackingIntact := false
                state := 4
                expired := true
            else if age >= watchBars
                state := 4
                expired := true
            else
                age += 1
                float movement = math.abs(close - lastTrackedClose)
                pathLength += movement
                lastTrackedClose := close
                float penetration = gapDirection == 1 ? sessionOpen - low : high - sessionOpen
                maxRecovery := math.max(maxRecovery, math.max(0.0, math.min(1.0, penetration / math.abs(gapSize))))
                float side = gapDirection * (close - sessionOpen)
                outsideCloses += side > 0 ? 1 : 0
                volumeSum += volume
                outsideVolume += side > 0 ? volume : 0.0
                insideVolume += side < 0 ? volume : 0.0
                float outsideShare = float(outsideCloses) / age
                float outsideVolumeShare = outsideVolume / volumeSum
                float insideVolumeShare = insideVolume / volumeSum
                float efficiency = pathLength > syminfo.mintick ? gapDirection * (close - sessionOpen) / pathLength : 0.0
                float participation = volumeSum / (age * frozenVolume)
                bool participationOK = participation >= minParticipation
                bool acceptEnter = age >= 2 and participationOK and outsideShare >= 0.60 and outsideVolumeShare >= 0.55 and efficiency >= 0.35 and maxRecovery <= 0.50
                bool acceptRetain = state == 1 and participationOK and outsideShare >= 0.50 and outsideVolumeShare >= 0.45 and efficiency >= 0.20 and maxRecovery <= 0.65
                bool recoverEnter = age >= 2 and participationOK and maxRecovery >= 0.65 and insideVolumeShare >= 0.50 and efficiency <= -0.25
                bool recoverRetain = state == 2 and participationOK and maxRecovery >= 0.50 and insideVolumeShare >= 0.40 and efficiency <= -0.10
                bool tugEnter = age >= 3 and pathLength >= 0.20 * math.abs(gapSize) and math.abs(efficiency) <= 0.25
                bool tugRetain = state == 3 and age >= 3 and math.abs(efficiency) <= 0.40
                int candidate = acceptEnter or acceptRetain ? 1 : recoverEnter or recoverRetain ? 2 : tugEnter or tugRetain ? 3 : 0
                if candidate == 0
                    state := 0
                    pending := 0
                    pendingCount := 0
                else if candidate == state
                    pending := 0
                    pendingCount := 0
                else
                    if candidate == pending
                        pendingCount += 1
                    else
                        pending := candidate
                        pendingCount := 1
                    if pendingCount >= confirmation
                        state := candidate
                        pending := 0
                        pendingCount := 0
            if slot == slots - 1 and state != 4
                state := 4
                expired := true
        if slot == slots - 1
            lastComplete := priceIntact
            lastSessionClose := close
    enteredAcceptance := state == 1 and oldState != 1
    enteredRecovery := state == 2 and oldState != 2
    enteredTug := state == 3 and oldState != 3
 
bool sameSession = inSession and activeStart == startTime
bool showEpisode = sameSession and eligible and trackingIntact and slot > 0
float participationNow = age > 0 and not na(frozenVolume) ? volumeSum / (age * frozenVolume) : na
float efficiencyNow = age > 0 and pathLength > syminfo.mintick ? gapDirection * (lastTrackedClose - sessionOpen) / pathLength : 0.0
float outsideNow = age > 0 ? float(outsideCloses) / age : na
color stateColor = state == 1 ? color.rgb(0, 135, 120) : state == 2 ? color.rgb(195, 91, 25) : state == 3 ? color.rgb(118, 78, 180) : color.rgb(95, 104, 117)
int participationAlpha = not na(participationNow) ? int(math.max(12.0, math.min(75.0, 75.0 - 35.0 * participationNow))) : 75
float frontier = showEpisode ? sessionOpen - gapDirection * math.abs(gapSize) * maxRecovery : na
pOpen = plot(showEpisode and showCorridor ? sessionOpen : na, "Session open", color=color.new(color.rgb(80, 100, 130), 25), style=plot.style_linebr)
pReference = plot(showEpisode and showCorridor ? referenceClose : na, "Prior complete session close", color=color.new(color.rgb(80, 100, 130), 25), style=plot.style_linebr)
fill(pOpen, pReference, color=showCorridor ? color.new(stateColor, 90) : na, title="Opening gap corridor")
plot(frontier, "Maximum recovery frontier", color=color.new(stateColor, participationAlpha), linewidth=4, style=plot.style_linebr)
plotshape(showMarkers and enteredAcceptance, "Gap accepted", shape.circle, location.abovebar, color=color.rgb(0, 135, 120), size=size.tiny, text="A", textcolor=color.rgb(0, 135, 120))
plotshape(showMarkers and enteredRecovery, "Gap recovery", shape.circle, location.belowbar, color=color.rgb(195, 91, 25), size=size.tiny, text="R", textcolor=color.rgb(195, 91, 25))
plotshape(showMarkers and enteredTug, "Gap tug-of-war", shape.circle, location.abovebar, color=color.rgb(118, 78, 180), size=size.tiny, text="T", textcolor=color.rgb(118, 78, 180))
 
var table dashboard = table.new(position.top_right, 2, 6, bgcolor=color.new(color.rgb(20, 24, 31), 8), frame_color=color.new(color.gray, 50), frame_width=1)
if barstate.islast
    string status = not configOK ? "Use aligned 1–30m session" : not inSession ? "Outside session" : not sameSession ? "Awaiting confirmed bar" : not eligible ? "No supported opening gap" : not trackingIntact ? "Data support lost" : state == 1 ? "Accepted" : state == 2 ? "Recovery" : state == 3 ? "Tug-of-war" : state == 4 ? "Observation ended" : "Observing"
    if showTable
        table.cell(dashboard, 0, 0, "GAP RECOVERY", text_color=color.white, text_size=size.small)
        table.cell(dashboard, 1, 0, status, text_color=color.white, text_size=size.small)
        table.cell(dashboard, 0, 1, "Gap / prior ATR", text_color=color.silver, text_size=size.small)
        table.cell(dashboard, 1, 1, eligible ? str.tostring(gapSize / frozenATR, "#.00") : "--", text_color=color.white, text_size=size.small)
        table.cell(dashboard, 0, 2, "Recovery / outside", text_color=color.silver, text_size=size.small)
        table.cell(dashboard, 1, 2, eligible and age > 0 ? str.tostring(100 * maxRecovery, "#.0") + "% / " + str.tostring(100 * outsideNow, "#.0") + "%" : "--", text_color=color.white, text_size=size.small)
        table.cell(dashboard, 0, 3, "Efficiency / volume", text_color=color.silver, text_size=size.small)
        table.cell(dashboard, 1, 3, eligible and age > 0 ? str.tostring(efficiencyNow, "#.00") + " / " + str.tostring(participationNow, "#.00") + "x" : "--", text_color=color.white, text_size=size.small)
        table.cell(dashboard, 0, 4, "Outside / inward volume", text_color=color.silver, text_size=size.small)
        table.cell(dashboard, 1, 4, eligible and volumeSum > 0 ? str.tostring(100 * outsideVolume / volumeSum, "#.0") + "% / " + str.tostring(100 * insideVolume / volumeSum, "#.0") + "%" : "--", text_color=color.white, text_size=size.small)
        table.cell(dashboard, 0, 5, "Observed bars", text_color=color.silver, text_size=size.small)
        table.cell(dashboard, 1, 5, eligible ? str.tostring(age) + " / " + str.tostring(watchBars) : "--", text_color=color.white, text_size=size.small)
    else
        table.clear(dashboard, 0, 0, 1, 5)
 
alertcondition(enteredAcceptance, "Gap acceptance confirmed", "A supported opening gap entered the confirmed acceptance state.")
alertcondition(enteredRecovery, "Gap recovery confirmed", "A supported opening gap entered the confirmed recovery state.")
alertcondition(enteredTug, "Gap tug-of-war confirmed", "A supported opening gap entered the confirmed tug-of-war state.")
alertcondition(expired, "Gap observation ended", "The opening-gap observation ended or lost required data support.")

← 返回列表