本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Return Autocorrelation Stability Map [Pineify]", shorttitle="Return AC Stability", overlay=false, max_bars_back=1000)
// Each return is bounded using only the preceding return sample. Missing samples
// remain missing, so a pair never jumps across a gap in the original bar sequence.
int baselineLen = input.int(40, "Prior robust baseline", minval=15, maxval=150, group="Calculation")
int windowLen = input.int(80, "Pair window (even)", options=[40, 60, 80, 100, 120, 160], group="Calculation")
string lagChoice = input.string("1, 2, 3", "Lag set (bars)", options=["1, 2, 3", "1, 3, 5", "1, 2, 5"], group="Calculation")
float capWidth = input.float(3.0, "Clip width (scaled MAD)", minval=0.5, maxval=10.0, step=0.5, group="Calculation")
float minCoverage = input.float(0.90, "Minimum pair coverage per half", minval=0.70, maxval=1.0, step=0.05, group="State")
float enterLevel = input.float(0.20, "Entry correlation", minval=0.05, maxval=0.80, step=0.05, group="State")
int confirmBars = input.int(2, "Confirmation closes", minval=1, maxval=10, group="State")
bool showBand = input.bool(true, "Show lag range", group="Display")
bool showBg = input.bool(false, "Show state background", group="Display")
bool showTable = input.bool(true, "Show lag diagnostics", group="Display")
int lag1 = 1
int lag2 = lagChoice == "1, 3, 5" ? 3 : 2
int lag3 = lagChoice == "1, 2, 3" ? 3 : 5
int half = int(windowLen / 2)
float exitLevel = enterLevel * 0.60
float ret = not na(close) and not na(close[1]) and close > 0 and close[1] > 0 ? math.log(close / close[1]) : na
float clean = na
if barstate.isconfirmed
array<float> prior = array.new_float()
for i = 1 to baselineLen
if not na(ret[i])
array.push(prior, ret[i])
if array.size(prior) == baselineLen and not na(ret)
float med = array.median(prior)
array<float> deviations = array.new_float()
for i = 0 to baselineLen - 1
array.push(deviations, math.abs(array.get(prior, i) - med))
float scale = 1.4826 * array.median(deviations)
if not na(scale) and scale > 0
clean := math.max(med - capWidth * scale, math.min(med + capWidth * scale, ret))
// Pearson correlation is undefined for a constant side or insufficient pairs.
f_pearson(float sx, float sy, float sxx, float syy, float sxy, int n) =>
float vx = n * sxx - sx * sx
float vy = n * syy - sy * sy
float answer = na
if n >= 3 and vx > 1e-18 and vy > 1e-18
answer := math.max(-1.0, math.min(1.0, (n * sxy - sx * sy) / math.sqrt(vx * vy)))
answer
// Recent and older halves are partitioned by the later endpoint of each pair.
// Lagged partners may lie across the split, and are never moved to another slot.
f_lag(float values, int lag, int n, int split) =>
float sx0 = 0.0
float sy0 = 0.0
float sxx0 = 0.0
float syy0 = 0.0
float sxy0 = 0.0
float sx1 = 0.0
float sy1 = 0.0
float sxx1 = 0.0
float syy1 = 0.0
float sxy1 = 0.0
int n0 = 0
int n1 = 0
for i = 0 to n - 1
float x = values[i]
float y = values[i + lag]
if not na(x) and not na(y)
if i < split
n0 += 1
sx0 += x
sy0 += y
sxx0 += x * x
syy0 += y * y
sxy0 += x * y
else
n1 += 1
sx1 += x
sy1 += y
sxx1 += x * x
syy1 += y * y
sxy1 += x * y
float total = f_pearson(sx0 + sx1, sy0 + sy1, sxx0 + sxx1, syy0 + syy1, sxy0 + sxy1, n0 + n1)
float recent = f_pearson(sx0, sy0, sxx0, syy0, sxy0, n0)
float older = f_pearson(sx1, sy1, sxx1, syy1, sxy1, n1)
float coverage = math.min(n0 / float(split), n1 / float(n - split))
[total, recent, older, coverage]
f_vote(float total, float recent, float older, float fullGate, float halfGate, int direction) =>
direction * total >= fullGate and direction * recent >= halfGate and direction * older >= halfGate ? 1 : 0
var int state = -2
var int pending = -2
var int pendingCount = 0
var float held1 = na
var float held2 = na
var float held3 = na
var float heldRecent1 = na
var float heldRecent2 = na
var float heldRecent3 = na
var float heldOlder1 = na
var float heldOlder2 = na
var float heldOlder3 = na
var float heldCoverage = na
[a, ar, ao, ac] = f_lag(clean, lag1, windowLen, half)
[b, br, bo, bc] = f_lag(clean, lag2, windowLen, half)
[c, cr, co, cc] = f_lag(clean, lag3, windowLen, half)
if barstate.isconfirmed
float coverage = math.min(ac, math.min(bc, cc))
bool valid = coverage >= minCoverage and not na(a) and not na(ar) and not na(ao) and not na(b) and not na(br) and not na(bo) and not na(c) and not na(cr) and not na(co)
heldCoverage := coverage
held1 := valid ? a : na
held2 := valid ? b : na
held3 := valid ? c : na
heldRecent1 := valid ? ar : na
heldRecent2 := valid ? br : na
heldRecent3 := valid ? cr : na
heldOlder1 := valid ? ao : na
heldOlder2 := valid ? bo : na
heldOlder3 := valid ? co : na
if not valid
state := -2
pending := -2
pendingCount := 0
else
float positiveGate = state == 1 ? exitLevel : enterLevel
float negativeGate = state == -1 ? exitLevel : enterLevel
int positive = f_vote(a, ar, ao, positiveGate, exitLevel, 1) + f_vote(b, br, bo, positiveGate, exitLevel, 1) + f_vote(c, cr, co, positiveGate, exitLevel, 1)
int negative = f_vote(a, ar, ao, negativeGate, exitLevel, -1) + f_vote(b, br, bo, negativeGate, exitLevel, -1) + f_vote(c, cr, co, negativeGate, exitLevel, -1)
float strongest = math.max(math.abs(a), math.max(math.abs(b), math.abs(c)))
float fragileGate = state == 2 ? exitLevel : enterLevel
int candidate = positive >= 2 ? 1 : negative >= 2 ? -1 : strongest >= fragileGate ? 2 : 0
if candidate == state
pending := state
pendingCount := 0
else
pendingCount := candidate == pending ? pendingCount + 1 : 1
pending := candidate
if pendingCount >= confirmBars
state := candidate
pendingCount := 0
float center = (held1 + held2 + held3) / 3.0
float bandHigh = math.max(held1, math.max(held2, held3))
float bandLow = math.min(held1, math.min(held2, held3))
color stateColor = state == 1 ? color.rgb(0, 145, 130) : state == -1 ? color.rgb(192, 65, 120) : state == 2 ? color.rgb(186, 115, 0) : color.rgb(100, 110, 125)
int bandTransparency = int(math.round(82 + 15 * (1 - nz(heldCoverage, 0))))
hline(1, "Upper bound", color=color.new(color.gray, 85))
hline(0, "Zero", color=color.new(color.gray, 40))
hline(-1, "Lower bound", color=color.new(color.gray, 85))
plot(center, "Mean lag correlation", color=stateColor, linewidth=3, style=plot.style_linebr)
upperPlot = plot(showBand ? bandHigh : na, "Highest lag", color=color.new(stateColor, 60), style=plot.style_linebr)
lowerPlot = plot(showBand ? bandLow : na, "Lowest lag", color=color.new(stateColor, 60), style=plot.style_linebr)
fill(upperPlot, lowerPlot, color=showBand ? color.new(stateColor, bandTransparency) : na, title="Lag range / coverage")
bgcolor(showBg and state != -2 ? color.new(stateColor, 92) : na, title="Confirmed state")
var table panel = table.new(position.top_right, 4, 5, border_width=1)
if barstate.islast
if showTable
string name = state == 1 ? "Continuation" : state == -1 ? "Reversal" : state == 2 ? "Fragile" : state == 0 ? "No strong dependence" : "Insufficient"
color paper = color.rgb(245, 246, 248)
color ink = color.rgb(35, 40, 48)
table.cell(panel, 0, 0, "Lag", text_color=ink, bgcolor=paper)
table.cell(panel, 1, 0, "Full", text_color=ink, bgcolor=paper)
table.cell(panel, 2, 0, "Recent", text_color=ink, bgcolor=paper)
table.cell(panel, 3, 0, "Older", text_color=ink, bgcolor=paper)
table.cell(panel, 0, 1, str.tostring(lag1), text_color=ink, bgcolor=paper)
table.cell(panel, 1, 1, str.tostring(held1, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 2, 1, str.tostring(heldRecent1, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 3, 1, str.tostring(heldOlder1, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 0, 2, str.tostring(lag2), text_color=ink, bgcolor=paper)
table.cell(panel, 1, 2, str.tostring(held2, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 2, 2, str.tostring(heldRecent2, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 3, 2, str.tostring(heldOlder2, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 0, 3, str.tostring(lag3), text_color=ink, bgcolor=paper)
table.cell(panel, 1, 3, str.tostring(held3, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 2, 3, str.tostring(heldRecent3, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 3, 3, str.tostring(heldOlder3, "#.##"), text_color=ink, bgcolor=paper)
table.cell(panel, 0, 4, "State", text_color=ink, bgcolor=paper)
table.cell(panel, 1, 4, name, text_color=ink, bgcolor=paper)
table.cell(panel, 2, 4, "Min pairs", text_color=ink, bgcolor=paper)
table.cell(panel, 3, 4, str.tostring(100 * heldCoverage, "#.0") + "%", text_color=ink, bgcolor=paper)
else
table.clear(panel, 0, 0, 3, 4)
alertcondition(barstate.isconfirmed and state == 1 and state[1] != 1, "Stable continuation", "Confirmed multi-lag continuation state")
alertcondition(barstate.isconfirmed and state == -1 and state[1] != -1, "Stable reversal", "Confirmed multi-lag reversal state")
alertcondition(barstate.isconfirmed and state == 2 and state[1] != 2, "Fragile dependence", "Confirmed fragile return dependence state")
alertcondition(barstate.isconfirmed and state == -2 and state[1] != -2, "Sample support lost", "Pair coverage or variation is insufficient")