本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Momentum Impulse Follow Through Map [Pineify]", shorttitle="Impulse Path [Pineify]", overlay=false, max_bars_back=400, precision=1)
string G1 = "Impulse detection"
int baselineLength = input.int(60, "Prior baseline bars", minval=20, maxval=240, group=G1)
float impulseThreshold = input.float(3.0, "Residual impulse / robust scale", minval=1.5, maxval=8.0, step=0.25, group=G1)
float minimumMove = input.float(2.0, "Absolute move / robust scale", minval=1.0, maxval=6.0, step=0.25, group=G1)
string G2 = "Post-impulse evidence"
int minimumAge = input.int(3, "Earliest acceptance or stall (bars)", minval=2, maxval=8, group=G2)
int maximumAge = input.int(40, "Observation limit (bars)", minval=12, maxval=120, group=G2)
int confirmationBars = input.int(2, "Consecutive candidate closes", minval=1, maxval=5, group=G2)
float extensionGate = input.float(0.25, "Minimum extension / impulse", minval=0.1, maxval=1.0, step=0.05, group=G2)
float residenceGate = input.float(0.60, "Minimum closes beyond impulse end", minval=0.5, maxval=0.9, step=0.05, group=G2)
float efficiencyGate = input.float(0.25, "Minimum signed path efficiency", minval=0.1, maxval=0.8, step=0.05, group=G2)
float givebackGate = input.float(0.50, "Maximum whole-event giveback", minval=0.2, maxval=0.8, step=0.05, group=G2)
float adverseGate = input.float(0.35, "Maximum adverse close excursion / impulse", minval=0.1, maxval=0.8, step=0.05, group=G2)
float reversalGate = input.float(0.10, "Reversal beyond origin / impulse", minval=0.0, maxval=0.5, step=0.05, group=G2)
string G3 = "Visual layers"
bool showBand = input.bool(true, "Acceptance evidence band", group=G3)
bool showExhaustion = input.bool(true, "Giveback columns", group=G3)
bool showBackground = input.bool(true, "Confirmed state wash", group=G3)
bool showEvents = input.bool(true, "Confirmed event markers", group=G3)
bool showTable = input.bool(true, "Live evidence summary", group=G3)
// Baseline excludes the entire current impulse. Missing slots are never compressed.
float change = close - close[1]
float trueRange = ta.tr(true)
bool priceValid = chart.is_standard and not na(open) and not na(high) and not na(low) and not na(close) and not na(close[1]) and high >= math.max(open, close) and low <= math.min(open, close) and high >= low
array<float> changes = array.new_float(0)
array<float> ranges = array.new_float(0)
bool baselineValid = bar_index >= baselineLength + 1
for i = 1 to baselineLength
bool slotValid = not na(open[i]) and not na(high[i]) and not na(low[i]) and not na(close[i]) and not na(close[i + 1]) and high[i] >= math.max(open[i], close[i]) and low[i] <= math.min(open[i], close[i]) and high[i] >= low[i]
baselineValid := baselineValid and slotValid and not na(trueRange[i])
array.push(changes, change[i])
array.push(ranges, trueRange[i])
float center = baselineValid ? array.median(changes) : na
array<float> deviations = array.new_float(0)
if baselineValid
for i = 0 to baselineLength - 1
array.push(deviations, math.abs(array.get(changes, i) - center))
float priorRange = baselineValid ? array.median(ranges) : na
float robustScale = baselineValid ? math.max(1.4826 * array.median(deviations), math.max(0.25 * priorRange, syminfo.mintick)) : na
bool detectionValid = priceValid and baselineValid and priorRange > 0 and robustScale > 0
float impulseScore = detectionValid ? math.abs(change - center) / robustScale : na
bool eligible = detectionValid and impulseScore >= impulseThreshold and math.abs(change) / robustScale >= minimumMove and change * (change - center) > 0
// All persistent event data and state are committed only on a confirmed close.
var bool active = false
var bool armed = false
var int direction = 0
var int age = 0
var float origin = na
var float impulseEnd = na
var float amplitude = na
var float frozenScale = na
var float previousClose = na
var float favorable = 0.0
var float adverse = 0.0
var float travel = 0.0
var int residenceCount = 0
var int state = 0
var int candidate = 0
var int candidateRun = 0
bool newImpulse = false
bool acceptedEvent = false
bool stalledEvent = false
bool reversedEvent = false
bool expiredEvent = false
bool interruptedEvent = false
bool liveEvent = active and priceValid
int liveAge = age
float liveFavorable = favorable
float liveAdverse = adverse
float liveTravel = travel
int liveResidence = residenceCount
float forward = na
float extension = na
float residence = na
float efficiency = na
float giveback = na
float trajectory = na
float quality = na
if active and priceValid
liveAge := age + 1
forward := direction * (close - impulseEnd)
liveFavorable := math.max(favorable, forward)
liveAdverse := math.max(adverse, -forward)
liveTravel := travel + math.abs(close - previousClose)
liveResidence := residenceCount + (forward > 0 ? 1 : 0)
extension := forward / amplitude
residence := float(liveResidence) / liveAge
efficiency := liveTravel > 0 ? forward / liveTravel : 0.0
giveback := (liveFavorable - forward) / (amplitude + liveFavorable)
trajectory := direction * (close - origin) / amplitude
quality := math.min(1.0, math.max(0.0, extension) / extensionGate) * residence * math.max(0.0, efficiency) * (1.0 - math.min(1.0, giveback)) * (1.0 - math.min(1.0, liveAdverse / amplitude))
if barstate.isconfirmed
if active
if not priceValid
active := false
armed := false
state := 0
candidate := 0
candidateRun := 0
interruptedEvent := true
else
age := liveAge
favorable := liveFavorable
adverse := liveAdverse
travel := liveTravel
residenceCount := liveResidence
previousClose := close
bool reversing = direction * (close - origin) / amplitude <= -reversalGate
bool entering = extension >= extensionGate and residence >= residenceGate and efficiency >= efficiencyGate and giveback <= givebackGate and liveAdverse / amplitude <= adverseGate
// Hysteresis retains acceptance at weaker evidence than its entry gate.
bool retaining = state == 2 and extension >= 0.6 * extensionGate and residence >= residenceGate - 0.15 and efficiency >= 0.5 * efficiencyGate and giveback <= math.min(0.95, givebackGate + 0.20) and liveAdverse / amplitude <= math.min(0.95, adverseGate + 0.20)
int desired = reversing ? 4 : liveAge < minimumAge ? 1 : entering or retaining ? 2 : 3
if desired != state
if desired == candidate
candidateRun += 1
else
candidate := desired
candidateRun := 1
if candidateRun >= confirmationBars
state := desired
candidateRun := 0
acceptedEvent := state == 2
stalledEvent := state == 3
reversedEvent := state == 4
else
candidate := state
candidateRun := 0
if reversedEvent
active := false
else if liveAge >= maximumAge
active := false
expiredEvent := true
else
state := 0
candidate := 0
candidateRun := 0
if not detectionValid
armed := false
else if not eligible
armed := true
else if armed
direction := change > 0 ? 1 : -1
origin := close[1]
impulseEnd := close
amplitude := math.abs(change)
frozenScale := robustScale
previousClose := close
age := 0
favorable := 0.0
adverse := 0.0
travel := 0.0
residenceCount := 0
state := 1
active := true
armed := false
newImpulse := true
liveEvent := true
liveAge := 0
liveFavorable := 0.0
liveAdverse := 0.0
extension := 0.0
residence := 0.0
efficiency := 0.0
giveback := 0.0
trajectory := 1.0
quality := 0.0
color upColor = #1976D2
color downColor = #C43D70
color stallColor = #A56500
color reverseColor = #7952C7
color pendingColor = #687787
color directionColor = direction == 1 ? upColor : downColor
color stateColor = state == 2 ? directionColor : state == 3 ? stallColor : state == 4 ? reverseColor : pendingColor
float track = liveEvent ? math.max(-250.0, math.min(250.0, 100.0 * direction * trajectory)) : na
float bandWidth = liveEvent ? 12.0 * quality : na
// Direction is applied twice: trajectory is impulse-oriented, track is price-oriented.
plot(track, "Event path (origin 0; impulse end +/-100; clipped +/-250)", color=directionColor, linewidth=3, style=plot.style_linebr)
pUpper = plot(showBand and liveEvent ? track + bandWidth : na, "Acceptance band upper", color=color.new(stateColor, 100), style=plot.style_linebr)
pLower = plot(showBand and liveEvent ? track - bandWidth : na, "Acceptance band lower", color=color.new(stateColor, 100), style=plot.style_linebr)
fill(pUpper, pLower, color=showBand ? color.new(stateColor, 72) : na, title="Post-impulse acceptance evidence")
plot(liveEvent ? 100.0 * direction : na, "Frozen impulse end", color=color.new(directionColor, 55), style=plot.style_linebr)
plot(showExhaustion and liveEvent ? -direction * 100.0 * math.min(1.0, giveback) : na, "Giveback opposite impulse (capped 100%)", color=color.new(stallColor, 45), style=plot.style_columns)
hline(0, "Pre-impulse origin", color=color.new(pendingColor, 55))
bgcolor(showBackground and liveEvent ? color.new(stateColor, 94) : na, title="Last confirmed event state")
plotshape(showEvents and newImpulse ? track : na, "New impulse", style=shape.diamond, location=location.absolute, color=directionColor, size=size.tiny, text="I", textcolor=chart.fg_color)
plotshape(showEvents and acceptedEvent ? track : na, "Accepted", style=shape.triangleup, location=location.absolute, color=directionColor, size=size.tiny, text="A", textcolor=chart.fg_color)
plotshape(showEvents and stalledEvent ? track : na, "Stalled", style=shape.square, location=location.absolute, color=stallColor, size=size.tiny, text="S", textcolor=chart.fg_color)
plotshape(showEvents and reversedEvent ? track : na, "Reversed", style=shape.xcross, location=location.absolute, color=reverseColor, size=size.tiny, text="R", textcolor=chart.fg_color)
plotshape(showEvents and expiredEvent ? track : na, "Expired", style=shape.circle, location=location.absolute, color=pendingColor, size=size.tiny, text="E", textcolor=chart.fg_color)
plot(impulseScore, "Live detector residual / scale", display=display.data_window)
plot(liveEvent ? 100.0 * direction * trajectory : na, "Unclipped price-oriented event path", display=display.data_window)
plot(liveEvent ? 100.0 * residence : na, "Post-impulse residence %", display=display.data_window)
plot(liveEvent ? efficiency : na, "Post-impulse signed efficiency", display=display.data_window)
plot(liveEvent ? giveback : na, "Whole-event giveback ratio (uncapped)", display=display.data_window)
plot(liveEvent ? liveFavorable / amplitude : na, "Maximum favorable close excursion / impulse", display=display.data_window)
plot(liveEvent ? liveAdverse / amplitude : na, "Maximum adverse close excursion / impulse", display=display.data_window)
plot(liveEvent ? frozenScale : na, "Frozen pre-impulse scale", display=display.data_window)
plot(liveEvent ? amplitude / frozenScale : na, "Frozen impulse amplitude / scale", display=display.data_window)
plot(liveEvent ? state : na, "Confirmed state: 1 pending, 2 accepted, 3 stalled, 4 reversed", display=display.data_window)
var table summary = table.new(position.top_right, 2, 7, border_width=0)
if barstate.islast
table.clear(summary, 0, 0, 1, 6)
if showTable
string statusText = not chart.is_standard ? "Standard chart required" : interruptedEvent ? "Interrupted" : liveEvent ? expiredEvent ? "Expired" : state == 1 ? "Pending" : state == 2 ? "Accepted" : state == 3 ? "Stalled" : "Reversed" : not detectionValid ? "Baseline unavailable" : "Idle / rearming"
color cellBg = color.new(chart.bg_color, 8)
table.cell(summary, 0, 0, "IMPULSE PATH", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 0, statusText, text_color=liveEvent ? stateColor : chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 0, 1, "Age / limit", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 1, liveEvent ? str.tostring(liveAge) + " / " + str.tostring(maximumAge) : "--", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 0, 2, "Extension / impulse", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 2, liveEvent ? str.tostring(extension, "#.00") : "--", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 0, 3, "Residence / efficiency", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 3, liveEvent ? str.tostring(100 * residence, "#") + "% / " + str.tostring(efficiency, "#.00") : "--", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 0, 4, "Giveback / evidence", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 4, liveEvent ? str.tostring(100 * giveback, "#") + "% / " + str.tostring(100 * quality, "#") : "--", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 0, 5, "Favorable / adverse", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 5, liveEvent ? str.tostring(liveFavorable / amplitude, "#.00") + " / " + str.tostring(liveAdverse / amplitude, "#.00") : "--", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 0, 6, "Metrics live; state closed", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
table.cell(summary, 1, 6, "Evidence != probability", text_color=chart.fg_color, bgcolor=cellBg, text_size=size.small)
alertcondition(newImpulse, "New momentum impulse", "A confirmed momentum impulse started a frozen event. Later impulses will not reset it.")
alertcondition(acceptedEvent, "Follow through accepted", "Post-impulse extension, residence, path efficiency and giveback confirmed acceptance.")
alertcondition(stalledEvent, "Momentum impulse stalled", "The post-impulse path confirmed insufficient follow through. Stall does not imply reversal.")
alertcondition(reversedEvent, "Momentum impulse reversed", "Consecutive closes confirmed reversal beyond the frozen pre-impulse origin. The event ended.")
alertcondition(expiredEvent, "Impulse observation expired", "The finite observation limit ended the event. Expiry is not a reversal.")
alertcondition(interruptedEvent, "Impulse data interrupted", "Invalid chart OHLC interrupted the active event. No failure outcome was inferred.")