Golden Cross Engine [Quantum Algo]

Quantum-Algo · study · 278 行 · 点赞 106 · TradingView 原页

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

Pine Script

//@version=6
indicator("Golden Cross Engine [Quantum Algo]", overlay = true, max_lines_count = 100, max_labels_count = 200, max_bars_back = 500)
 
// ════════════════════════════════════════════════════════════════
//  Concept credits: the golden cross and death cross are
//  classical moving-average crossover concepts in the public
//  domain of technical analysis. The Wilson score interval is
//  by Edwin B. Wilson (1927); shrinkage estimation is standard
//  public statistics. The convergence countdown, projection
//  geometry, per-symbol outcome statistics, outcome-settled
//  markers and all code in this script are original work.
// ════════════════════════════════════════════════════════════════
 
// ════════════════════════════════════════════════════════════════
//  INPUTS
// ════════════════════════════════════════════════════════════════
grpM = "Moving Averages"
maTypeS = input.string("SMA", "Average Type", options = ["SMA", "EMA"], group = grpM)
fastLen = input.int(50, "Fast Length", minval = 2, group = grpM)
slowLen = input.int(200, "Slow Length", minval = 5, group = grpM)
 
grpP = "Cross Countdown"
showProj = input.bool(true, "Show Convergence Projection", group = grpP, tooltip = "Projects both averages forward at their current slopes and marks where and when they would meet. A projection at current speed — not a forecast.")
horizon  = input.int(60, "Countdown Horizon (Bars)", minval = 10, maxval = 200, group = grpP)
alertLead = input.int(10, "Approach Alert Lead (Bars)", minval = 2, maxval = 60, group = grpP)
 
grpS = "Statistics Engine"
cap     = input.int(200, "Samples Per Cross Type (FIFO Cap)", minval = 20, maxval = 500, group = grpS)
minN    = input.int(8, "Minimum Samples To Grade", minval = 3, maxval = 50, group = grpS)
shrinkK = input.int(6, "Shrinkage Strength", minval = 0, maxval = 50, group = grpS)
zConf   = input.float(1.645, "Wilson z-Score", step = 0.005, group = grpS)
keepCross = input.int(12, "Cross Markers To Keep", minval = 4, maxval = 40, group = grpS)
 
grpV = "Chart Visuals"
bullCol = input.color(#00a89d, "Bullish Color", group = grpV)
bearCol = input.color(#e8365f, "Bearish Color", group = grpV)
goldCol = input.color(#d99b1e, "Gold Accent", group = grpV)
silvCol = input.color(#5c6b80, "Neutral Color", group = grpV)
showFill = input.bool(true, "Gradient Fill Between Averages", group = grpV)
flashBg  = input.bool(true, "Highlight Cross Bars", group = grpV)
 
grpB = "Dashboard"
showDash    = input.bool(true, "Show Dashboard", group = grpB)
dashPos     = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpB)
dashSize    = input.string("Small", "Text Size", options = ["Tiny", "Small", "Normal", "Large"], group = grpB)
dashTitleBg = input.color(color.new(#231a08, 0), "Title Band", group = grpB)
dashBg      = input.color(color.new(#171207, 8), "Background", group = grpB)
dashFrame   = input.color(color.new(#d99b1e, 15), "Frame", group = grpB)
dashGrid    = input.color(color.new(#d99b1e, 80), "Grid", group = grpB)
dashHeadTxt = input.color(color.new(#f0c05a, 0), "Header Text", group = grpB)
dashBodyTxt = input.color(color.new(#eef2f7, 5), "Body Text", group = grpB)
dashMuteTxt = input.color(color.new(#cdb87e, 15), "Muted Text", group = grpB)
 
// ════════════════════════════════════════════════════════════════
//  AVERAGES + REGIME
// ════════════════════════════════════════════════════════════════
atr  = ta.atr(14)
fMA  = maTypeS == "EMA" ? ta.ema(close, fastLen) : ta.sma(close, fastLen)
sMA  = maTypeS == "EMA" ? ta.ema(close, slowLen) : ta.sma(close, slowLen)
volMa = ta.sma(volume, 50)
volSd = ta.stdev(volume, 50)
volZ  = na(volSd) or volSd == 0 ? 0.0 : (volume - volMa) / volSd
 
golden = ta.crossover(fMA, sMA)
death  = ta.crossunder(fMA, sMA)
bullRegime = fMA > sMA
var int lastCrossBar = na
if golden or death
    lastCrossBar := bar_index
 
f_rep(string s, int n) =>
    out = ""
    if n > 0
        for i = 1 to n
            out += s
    out
fw(float p) => str.tostring(p * 100, "#") + "%"
fr(float r) => (r >= 0 ? "+" : "") + str.tostring(r * 100, "#.#") + "%"
 
// plots + living gradient fill (intensity breathes with the gap)
pF = plot(fMA, "Fast Average", color = color.new(goldCol, 0), linewidth = 2)
pS = plot(sMA, "Slow Average", color = color.new(silvCol, 10), linewidth = 2)
gapT = 96 - int(math.min(math.abs(fMA - sMA) / atr * 4, 14))
fill(pF, pS, color = showFill ? color.new(bullRegime ? bullCol : bearCol, gapT) : na, title = "Regime Fill")
bgcolor(flashBg and (golden or death) ? color.new(goldCol, 80) : na, title = "Cross Flash")
 
// ════════════════════════════════════════════════════════════════
//  PER-SYMBOL CROSS STATISTICS  ·  5 / 20 / 60 bar outcomes
// ════════════════════════════════════════════════════════════════
var array<float> g5  = array.new<float>()
var array<float> g20 = array.new<float>()
var array<float> g60 = array.new<float>()
var array<float> d5  = array.new<float>()
var array<float> d20 = array.new<float>()
var array<float> d60 = array.new<float>()
 
pushF(array<float> a, float v) =>
    a.push(v)
    if a.size() > cap
        a.shift()
 
f_feed(array<float> a5, array<float> a20, array<float> a60, bool cond, int dir) =>
    if barstate.isconfirmed and bar_index > 60
        if cond[5]
            pushF(a5, (close / close[5] - 1) * dir)
        if cond[20]
            pushF(a20, (close / close[20] - 1) * dir)
        if cond[60]
            pushF(a60, (close / close[60] - 1) * dir)
 
f_feed(g5, g20, g60, golden, 1)
f_feed(d5, d20, d60, death, -1)
 
statsOf(array<float> a) =>
    int n = a.size()
    float pS_ = 0.5
    float wLB = 0.5
    float avg = 0.0
    if n > 0
        int w = 0
        float s = 0.0
        for i = 0 to n - 1
            r = a.get(i)
            s += r
            if r > 0
                w += 1
        float pHat = w * 1.0 / n
        pS_ := (pHat * n + 0.5 * shrinkK) / (n + shrinkK)
        wLB := (pHat + zConf * zConf / (2 * n) - zConf * math.sqrt(math.max(pHat * (1 - pHat) / n + zConf * zConf / (4 * n * n), 0.0))) / (1 + zConf * zConf / n)
        avg := s / n
    [pS_, wLB, n, avg]
 
// ════════════════════════════════════════════════════════════════
//  CROSS MARKERS THAT SETTLE INTO THEIR OUTCOME
// ════════════════════════════════════════════════════════════════
var array<label> doneLbs = array.new<label>()
var array<label> pendLbs = array.new<label>()
var array<int>   pendBar = array.new<int>()
var array<int>   pendDir = array.new<int>()
var array<float> pendPx  = array.new<float>()
var int lastGradeN = 0
var string lastGrade = "—"
bool evGoldA = false
 
if barstate.isconfirmed and (golden or death)
    dir = golden ? 1 : -1
    slopeF = (fMA - fMA[5]) / 5
    steep  = math.abs(slopeF) / atr > 0.04
    momOK  = golden ? close > fMA : close < fMA
    gradeN = 1 + (volZ > 1 ? 1 : 0) + (steep ? 1 : 0) + (momOK ? 1 : 0)
    grade  = gradeN >= 4 ? "A" : gradeN == 3 ? "B" : "C"
    lastGradeN := gradeN
    lastGrade := grade
    evGoldA := gradeN >= 4
    [p5, w5x, n5, a5x]    = statsOf(golden ? g5 : d5)
    [p20, w20x, n20, a20x] = statsOf(golden ? g20 : d20)
    [p60, w60x, n60, a60x] = statsOf(golden ? g60 : d60)
    graded = n20 >= minN
    txt = (golden ? "Golden Cross" : "Death Cross") + (graded ? "  " + fw(p20) : "")
    tip = (golden ? "Golden Cross ▲" : "Death Cross ▼") + " on " + syminfo.ticker + " " + timeframe.period + " · Grade " + grade + " " + f_rep("◆", gradeN) + (graded ? "\n" + "Favorable 5 / 20 / 60 bars: " + fw(p5) + " / " + fw(p20) + " / " + fw(p60) + "\nSamples: " + str.tostring(n20) + " · Wilson bound: " + fw(w20x) + "\nAverage 20-bar move: " + fr(a20x * dir) + "\nMarker settles into its outcome after 20 bars" : "\nCollecting history (" + str.tostring(n20) + "/" + str.tostring(minN) + ") — not yet graded")
    lb = label.new(bar_index, golden ? low : high, txt, style = golden ? label.style_label_up : label.style_label_down, color = color.new(goldCol, 20), textcolor = #231a08, size = size.small, tooltip = tip)
    pendLbs.push(lb)
    pendBar.push(bar_index)
    pendDir.push(dir)
    pendPx.push(close)
 
// settle pending markers into teal / rose (worked) or faded gray (failed)
if barstate.isconfirmed and pendLbs.size() > 0
    while pendLbs.size() > 0 and bar_index - pendBar.get(0) >= 20
        lb = pendLbs.shift()
        pendBar.shift()
        d_ = pendDir.shift()
        px = pendPx.shift()
        ret = (close / px - 1) * d_
        if ret > 0
            label.set_color(lb, color.new(d_ == 1 ? bullCol : bearCol, 0))
            label.set_textcolor(lb, d_ == 1 ? #06222b : #ffffff)
        else
            label.set_color(lb, color.new(silvCol, 45))
            label.set_textcolor(lb, color.new(#eef2f7, 15))
        doneLbs.push(lb)
        while doneLbs.size() > keepCross
            label.delete(doneLbs.shift())
 
// ════════════════════════════════════════════════════════════════
//  CONVERGENCE COUNTDOWN + PROJECTION GEOMETRY
// ════════════════════════════════════════════════════════════════
slF = (fMA - fMA[3]) / 3
slS = (sMA - sMA[3]) / 3
diff = fMA - sMA
rel  = slF - slS
float nBars = na
int   cdDir = 0
if diff < 0 and rel > 0
    nBars := -diff / rel
    cdDir := 1
else if diff > 0 and rel < 0
    nBars := diff / -rel
    cdDir := -1
cdOn = not na(nBars) and nBars >= 1 and nBars <= horizon
crossPx = cdOn ? fMA + slF * nBars : na
 
var array<line>  prLn = array.new<line>()
var array<label> prLb = array.new<label>()
while prLn.size() > 0
    line.delete(prLn.shift())
while prLb.size() > 0
    label.delete(prLb.shift())
if showProj and barstate.islast and cdOn
    nB = int(math.round(nBars))
    prLn.push(line.new(bar_index, fMA, bar_index + nB, crossPx, color = color.new(goldCol, 20), style = line.style_dashed, width = 2))
    prLn.push(line.new(bar_index, sMA, bar_index + nB, crossPx, color = color.new(silvCol, 35), style = line.style_dashed))
    prLb.push(label.new(bar_index + nB, crossPx, "◆ " + (cdDir == 1 ? "Golden Cross" : "Death Cross") + " ≈ " + str.tostring(nB) + " bars", style = cdDir == 1 ? label.style_label_lower_left : label.style_label_upper_left, color = color.new(color.black, 100), textcolor = color.new(goldCol, 0), size = size.small, tooltip = "Where the averages meet if both keep their current slopes — a projection at current speed, not a forecast. Level ≈ " + str.tostring(crossPx, format.mintick)))
 
// approach alerts fire once when the countdown first enters the lead window
var bool inLeadG = false
var bool inLeadD = false
leadGNow = cdOn and cdDir == 1 and nBars <= alertLead
leadDNow = cdOn and cdDir == -1 and nBars <= alertLead
evLeadG = leadGNow and not inLeadG
evLeadD = leadDNow and not inLeadD
inLeadG := leadGNow
inLeadD := leadDNow
 
// ════════════════════════════════════════════════════════════════
//  MULTI-TIMEFRAME CROSS STATE
// ════════════════════════════════════════════════════════════════
f_state(string tf) =>
    request.security(syminfo.tickerid, tf, (maTypeS == "EMA" ? ta.ema(close, fastLen) : ta.sma(close, fastLen)) > (maTypeS == "EMA" ? ta.ema(close, slowLen) : ta.sma(close, slowLen)), lookahead = barmerge.lookahead_off)
s15 = f_state("15")
s60 = f_state("60")
s240 = f_state("240")
sD  = f_state("D")
sW  = f_state("W")
 
// ════════════════════════════════════════════════════════════════
//  PREMIUM DASHBOARD
// ════════════════════════════════════════════════════════════════
dPos = dashPos == "Top Left" ? position.top_left : dashPos == "Bottom Right" ? position.bottom_right : dashPos == "Bottom Left" ? position.bottom_left : position.top_right
bodySize = dashSize == "Tiny" ? size.tiny : dashSize == "Normal" ? size.normal : dashSize == "Large" ? size.large : size.small
headSize = dashSize == "Tiny" ? size.small : dashSize == "Large" ? size.large : dashSize == "Normal" ? size.normal : size.small
 
var table tb = table.new(dPos, 2, 9, bgcolor = dashBg, frame_color = dashFrame, frame_width = 1, border_color = dashGrid, border_width = 1)
 
if showDash and barstate.islast
    [gp, gw, gn, ga] = statsOf(g20)
    [dp, dw, dn, da] = statsOf(d20)
    stateTxt = (bullRegime ? "Golden Regime" : "Death Regime") + (na(lastCrossBar) ? "" : " · " + str.tostring(bar_index - lastCrossBar) + " bars")
    cdTxt = cdOn ? (cdDir == 1 ? "Golden ≈ " : "Death ≈ ") + str.tostring(int(math.round(nBars))) + " bars" : "Diverging"
    mtf = (s15 ? "15m▲" : "15m▼") + "  " + (s60 ? "1h▲" : "1h▼") + "  " + (s240 ? "4h▲" : "4h▼")
    mtf2 = (sD ? "1D▲" : "1D▼") + "  " + (sW ? "1W▲" : "1W▼")
    table.cell(tb, 0, 0, "◈ GOLDEN CROSS ENGINE", bgcolor = dashTitleBg, text_color = dashHeadTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 0, f_rep("◆", math.min(4, lastGradeN)), bgcolor = dashTitleBg, text_color = dashHeadTxt, text_size = bodySize)
    table.cell(tb, 0, 1, "State", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 1, stateTxt, text_color = bullRegime ? bullCol : bearCol, text_size = bodySize)
    table.cell(tb, 0, 2, "Countdown", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 2, cdTxt, text_color = cdOn ? goldCol : dashBodyTxt, text_size = bodySize)
    table.cell(tb, 0, 3, "Projected Level", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 3, cdOn ? str.tostring(crossPx, format.mintick) : "—", text_color = dashBodyTxt, text_size = bodySize)
    table.cell(tb, 0, 4, "Last Grade", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 4, lastGrade, text_color = lastGradeN >= 4 ? goldCol : dashBodyTxt, text_size = bodySize)
    table.cell(tb, 0, 5, "Golden 20-Bar", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 5, gn > 0 ? fw(gp) + " ×" + str.tostring(gn) + " · " + fr(ga) : "—", text_color = gn >= minN and gp >= 0.55 ? goldCol : dashBodyTxt, text_size = bodySize)
    table.cell(tb, 0, 6, "Death 20-Bar", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 6, dn > 0 ? fw(dp) + " ×" + str.tostring(dn) + " · " + fr(-da) : "—", text_color = dn >= minN and dp >= 0.55 ? goldCol : dashBodyTxt, text_size = bodySize)
    table.cell(tb, 0, 7, "Timeframes", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 7, mtf, text_color = dashBodyTxt, text_size = bodySize)
    table.cell(tb, 0, 8, "Higher", text_color = dashMuteTxt, text_size = headSize, text_halign = text.align_left)
    table.cell(tb, 1, 8, mtf2, text_color = dashBodyTxt, text_size = bodySize)
 
// ════════════════════════════════════════════════════════════════
//  ALERTS
// ════════════════════════════════════════════════════════════════
alertcondition(golden, "Golden Cross", "The fast average crossed above the slow average.")
alertcondition(death, "Death Cross", "The fast average crossed below the slow average.")
alertcondition(evLeadG, "Golden Cross Approaching", "The convergence countdown projects a golden cross within the alert lead window at current slopes.")
alertcondition(evLeadD, "Death Cross Approaching", "The convergence countdown projects a death cross within the alert lead window at current slopes.")
alertcondition(evGoldA, "Grade A Cross", "A cross fired with full quality confluence: volume, steep slope, and momentum confirmation.")

← 返回列表