Gold Pulse v6

pandahen · study · 388 行 · 点赞 73 · TradingView 原页

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

Pine Script

//@version=6
indicator("Gold Pulse v6", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ===== INPUT =====
lookback    = input.int(50, "Periode Korelasi", minval=10, maxval=500)
layoutMode  = input.string("Compact 2-kolom", "Layout", options=["Compact 2-kolom","Vertikal bertumpuk","Ringkas 3-kolom"])
showChange  = input.bool(true, "Tampilkan %")
showPoint   = input.bool(true, "Tampilkan Point")
showBias    = input.bool(true, "Tampilkan GOLD BIAS")
sortMode    = input.string("Absolut (kuat→lemah)", "Urutkan", options=["Absolut (kuat→lemah)","Positif dulu","Negatif dulu","Tidak diurutkan"])
pos         = input.string("Bottom Right", "Posisi", options=["Top Right","Top Left","Bottom Right","Bottom Left","Middle Right"])
textSize    = input.string("Small", "Ukuran Teks", options=["Tiny","Small","Normal","Large"])
bgColor     = input.color(color.new(#1e222d, 10), "Background")
headerColor = input.color(#2962ff, "Warna Header")
bullColor   = input.color(#26a69a, "Bullish")
bearColor   = input.color(#ef5350, "Bearish")
neutralCol  = input.color(#787b86, "Netral")
 
// ===== HELPER =====
secPair(sym) =>
    request.security(sym, timeframe.period, [close, close[1]])
 
// ===== GOLD =====
[gC, gP] = request.security("FX_IDC:XAUUSD", timeframe.period, [close, close[1]])
gCSafe = na(gC) ? close : gC
gPSafe = na(gP) ? close[1] : gP
rGOLD  = gCSafe / gPSafe - 1
dGOLD  = gCSafe - gPSafe
 
// ===== 12 PAIR =====
[dDXYc,  dDXYp]  = secPair("TVC:DXY")
[dU10c,  dU10p]  = secPair("TVC:US10Y")
[dU02c,  dU02p]  = secPair("TVC:US02Y")
[dSILc,  dSILp]  = secPair("TVC:SILVER")
[dPLTc,  dPLTp]  = secPair("TVC:PLATINUM")
[dBTCc,  dBTCp]  = secPair("BITSTAMP:BTCUSD")
[dSPXc,  dSPXp]  = secPair("TVC:SPX")
[dVIXc,  dVIXp]  = secPair("TVC:VIX")
[dEURc,  dEURp]  = secPair("FX_IDC:EURUSD")
[dJPYc,  dJPYp]  = secPair("FX_IDC:USDJPY")
[dCHFc,  dCHFp]  = secPair("FX_IDC:USDCHF")
[dOILc,  dOILp]  = secPair("TVC:USOIL")
 
// ===== COPPER fallback =====
[copperC1, copperP1] = request.security("COMEX:HG1!", timeframe.period, [close, close[1]])
[copperC2, copperP2] = request.security("CAPITALCOM:COPPER", timeframe.period, [close, close[1]])
copperC = na(copperC1) ? copperC2 : copperC1
copperP = na(copperP1) ? copperP2 : copperP1
 
// ===== RETURN & DELTA =====
rDXY    = nz(dDXYc)   / nz(dDXYp,   1) - 1
rUS10Y  = nz(dU10c)   / nz(dU10p,   1) - 1
rUS02Y  = nz(dU02c)   / nz(dU02p,   1) - 1
rSILVER = nz(dSILc)   / nz(dSILp,   1) - 1
rPLAT   = nz(dPLTc)   / nz(dPLTp,   1) - 1
rBTC    = nz(dBTCc)   / nz(dBTCp,   1) - 1
rSPX    = nz(dSPXc)   / nz(dSPXp,   1) - 1
rVIX    = nz(dVIXc)   / nz(dVIXp,   1) - 1
rEURUSD = nz(dEURc)   / nz(dEURp,   1) - 1
rUSDJPY = nz(dJPYc)   / nz(dJPYp,   1) - 1
rUSDCHF = nz(dCHFc)   / nz(dCHFp,   1) - 1
rUSOIL  = nz(dOILc)   / nz(dOILp,   1) - 1
rCOPPER = nz(copperC) / nz(copperP, 1) - 1
 
dDXY    = nz(dDXYc)   - nz(dDXYp)
dUS10Y  = nz(dU10c)   - nz(dU10p)
dUS02Y  = nz(dU02c)   - nz(dU02p)
dSILVER = nz(dSILc)   - nz(dSILp)
dPLAT   = nz(dPLTc)   - nz(dPLTp)
dBTC    = nz(dBTCc)   - nz(dBTCp)
dSPX    = nz(dSPXc)   - nz(dSPXp)
dVIX    = nz(dVIXc)   - nz(dVIXp)
dEURUSD = nz(dEURc)   - nz(dEURp)
dUSDJPY = nz(dJPYc)   - nz(dJPYp)
dUSDCHF = nz(dCHFc)   - nz(dCHFp)
dUSOIL  = nz(dOILc)   - nz(dOILp)
dCOPPER = nz(copperC) - nz(copperP)
 
cDXY    = rDXY    * 100
cUS10Y  = rUS10Y  * 100
cUS02Y  = rUS02Y  * 100
cGOLD   = rGOLD   * 100
cSILVER = rSILVER * 100
cPLAT   = rPLAT   * 100
cCOPPER = rCOPPER * 100
cBTC    = rBTC    * 100
cSPX    = rSPX    * 100
cVIX    = rVIX    * 100
cEURUSD = rEURUSD * 100
cUSDJPY = rUSDJPY * 100
cUSDCHF = rUSDCHF * 100
cUSOIL  = rUSOIL  * 100
 
// ===== FORMAT POINT (adaptif) =====
fmtPoint(float v) =>
    av = math.abs(v)
    av >= 100 ? str.tostring(v, "#") :
     av >= 10  ? str.tostring(v, "#.##") :
     av >= 1   ? str.tostring(v, "#.###") :
     av >= 0.1 ? str.tostring(v, "#.####") :
                 str.tostring(v, "#.#####")
 
// ===== KORELASI =====
corr(series float a, series float b, int len) =>
    meanA = ta.sma(a, len)
    meanB = ta.sma(b, len)
    cov   = ta.sma(a * b, len) - meanA * meanB
    sdA   = math.sqrt(math.max(ta.sma(a * a, len) - meanA * meanA, 0))
    sdB   = math.sqrt(math.max(ta.sma(b * b, len) - meanB * meanB, 0))
    den   = sdA * sdB
    den == 0 or na(den) ? 0.0 : cov / den
 
kDXY    = corr(rDXY,    rGOLD, lookback)
kUS10Y  = corr(rUS10Y,  rGOLD, lookback)
kUS02Y  = corr(rUS02Y,  rGOLD, lookback)
kSILVER = corr(rSILVER, rGOLD, lookback)
kPLAT   = corr(rPLAT,   rGOLD, lookback)
kCOPPER = corr(rCOPPER, rGOLD, lookback)
kBTC    = corr(rBTC,    rGOLD, lookback)
kSPX    = corr(rSPX,    rGOLD, lookback)
kVIX    = corr(rVIX,    rGOLD, lookback)
kEURUSD = corr(rEURUSD, rGOLD, lookback)
kUSDJPY = corr(rUSDJPY, rGOLD, lookback)
kUSDCHF = corr(rUSDCHF, rGOLD, lookback)
kUSOIL  = corr(rUSOIL,  rGOLD, lookback)
 
// ===== GOLD BIAS =====
biasScore = 0
biasScore += cDXY    < 0 ? 1 : cDXY    > 0 ? -1 : 0
biasScore += cUS10Y  < 0 ? 1 : cUS10Y  > 0 ? -1 : 0
biasScore += cSILVER > 0 ? 1 : cSILVER < 0 ? -1 : 0
biasScore += cEURUSD > 0 ? 1 : cEURUSD < 0 ? -1 : 0
biasScore += cVIX    > 0 ? 1 : cVIX    < 0 ? -1 : 0
 
biasTxtShort = biasScore >= 3 ? "▲ BULL " + str.tostring(biasScore) + "/5" : 
               biasScore == 2 ? "▲ BULL " + str.tostring(biasScore) + "/5" : 
               biasScore == 1 ? "↗ Bull " + str.tostring(biasScore) + "/5" : 
               biasScore == 0 ? "◆ NEUT 0/5" : 
               biasScore == -1 ? "↘ Bear " + str.tostring(biasScore) + "/5" : 
               biasScore == -2 ? "▼ BEAR " + str.tostring(biasScore) + "/5" : "▼ BEAR " + str.tostring(biasScore) + "/5"
 
biasCol = biasScore >= 2 ? bullColor : biasScore <= -2 ? bearColor : neutralCol
biasBg  = biasScore >= 3 ? color.new(bullColor, 70) : biasScore <= -3 ? color.new(bearColor, 70) : color.new(neutralCol, 70)
 
// ===== ARRAY =====
var string[] pShort = array.new<string>(0)
var float[]  pCorr  = array.new<float>(0)
var float[]  pChg   = array.new<float>(0)
var float[]  pPoint = array.new<float>(0)
 
if barstate.islast
    array.clear(pShort)
    array.clear(pCorr)
    array.clear(pChg)
    array.clear(pPoint)
    
    array.push(pShort, "DXY")
    array.push(pCorr, kDXY)
    array.push(pChg,  cDXY)
    array.push(pPoint, dDXY)
    
    array.push(pShort, "US10Y")
    array.push(pCorr, kUS10Y)
    array.push(pChg,  cUS10Y)
    array.push(pPoint, dUS10Y)
    
    array.push(pShort, "US2Y")
    array.push(pCorr, kUS02Y)
    array.push(pChg,  cUS02Y)
    array.push(pPoint, dUS02Y)
    
    array.push(pShort, "Silver")
    array.push(pCorr, kSILVER)
    array.push(pChg,  cSILVER)
    array.push(pPoint, dSILVER)
    
    array.push(pShort, "Platinum")
    array.push(pCorr, kPLAT)
    array.push(pChg,  cPLAT)
    array.push(pPoint, dPLAT)
    
    array.push(pShort, "Copper")
    array.push(pCorr, kCOPPER)
    array.push(pChg,  cCOPPER)
    array.push(pPoint, dCOPPER)
    
    array.push(pShort, "BTC")
    array.push(pCorr, kBTC)
    array.push(pChg,  cBTC)
    array.push(pPoint, dBTC)
    
    array.push(pShort, "SPX")
    array.push(pCorr, kSPX)
    array.push(pChg,  cSPX)
    array.push(pPoint, dSPX)
    
    array.push(pShort, "VIX")
    array.push(pCorr, kVIX)
    array.push(pChg,  cVIX)
    array.push(pPoint, dVIX)
    
    array.push(pShort, "EURUSD")
    array.push(pCorr, kEURUSD)
    array.push(pChg,  cEURUSD)
    array.push(pPoint, dEURUSD)
    
    array.push(pShort, "USDJPY")
    array.push(pCorr, kUSDJPY)
    array.push(pChg,  cUSDJPY)
    array.push(pPoint, dUSDJPY)
    
    array.push(pShort, "USDCHF")
    array.push(pCorr, kUSDCHF)
    array.push(pChg,  cUSDCHF)
    array.push(pPoint, dUSDCHF)
    
    array.push(pShort, "Oil")
    array.push(pCorr, kUSOIL)
    array.push(pChg,  cUSOIL)
    array.push(pPoint, dUSOIL)
 
    // ===== SORTIR =====
    n = array.size(pCorr)
    if sortMode != "Tidak diurutkan"
        for i = 0 to n - 2
            for j = 0 to n - 2 - i
                a = math.abs(array.get(pCorr, j))
                b = math.abs(array.get(pCorr, j + 1))
                swap = false
                if sortMode == "Absolut (kuat→lemah)"
                    swap := a < b
                else if sortMode == "Positif dulu"
                    av = array.get(pCorr, j)
                    bv = array.get(pCorr, j + 1)
                    swap := (bv > 0 and av <= 0) or (math.sign(av) == math.sign(bv) and a < b)
                else if sortMode == "Negatif dulu"
                    av = array.get(pCorr, j)
                    bv = array.get(pCorr, j + 1)
                    swap := (bv < 0 and av >= 0) or (math.sign(av) == math.sign(bv) and a < b)
                
                if swap
                    tmpC = array.get(pCorr, j)
                    array.set(pCorr, j, array.get(pCorr, j + 1))
                    array.set(pCorr, j + 1, tmpC)
                    
                    tmpG = array.get(pChg, j)
                    array.set(pChg, j, array.get(pChg, j + 1))
                    array.set(pChg, j + 1, tmpG)
                    
                    tmpP = array.get(pPoint, j)
                    array.set(pPoint, j, array.get(pPoint, j + 1))
                    array.set(pPoint, j + 1, tmpP)
                    
                    tmpS = array.get(pShort, j)
                    array.set(pShort, j, array.get(pShort, j + 1))
                    array.set(pShort, j + 1, tmpS)
 
// ===== FORMAT KOMBINASI CHG & POINT =====
fmtCombo(float chg, float pt) =>
    chgTxt = na(chg) ? "-" : str.tostring(chg, "#.##") + "%"
    ptTxt  = na(pt)  ? "-" : fmtPoint(pt)
    showChange and showPoint ? chgTxt + " / " + ptTxt : showChange ? chgTxt : showPoint ? ptTxt : "-"
 
// ===== INISIALISASI TABEL =====
var table dash = table.new(position.bottom_right, 3, 40, bgcolor=bgColor, border_width=1, border_color=color.new(color.gray, 70))
 
if pos == "Top Right"
    table.set_position(dash, position.top_right)
else if pos == "Top Left"
    table.set_position(dash, position.top_left)
else if pos == "Bottom Right"
    table.set_position(dash, position.bottom_right)
else if pos == "Bottom Left"
    table.set_position(dash, position.bottom_left)
else if pos == "Middle Right"
    table.set_position(dash, position.middle_right)
 
ts = textSize == "Tiny" ? size.tiny : textSize == "Small" ? size.small : textSize == "Normal" ? size.normal : size.large
 
if barstate.islast and bar_index > lookback
    table.clear(dash, 0, 0, 2, 39)
    
    if layoutMode == "Compact 2-kolom"
        table.cell(dash, 0, 0, "GOLD", text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 1, 0, "CORR", text_color=color.white, bgcolor=headerColor, text_size=ts)
        
        if showBias
            table.cell(dash, 0, 1, "BIAS",       text_color=color.white, bgcolor=biasBg, text_size=ts)
            table.cell(dash, 1, 1, biasTxtShort, text_color=biasCol,     bgcolor=biasBg, text_size=ts)
            
        headerRow = showBias ? 2 : 1
        table.cell(dash, 0, headerRow, "PAIR", text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 1, headerRow, "KOR",  text_color=color.white, bgcolor=headerColor, text_size=ts)
        
        goldRow = headerRow + 1
        goldChg = str.tostring(cGOLD, "#.##") + "%"
        goldPt  = fmtPoint(dGOLD)
        goldCombo = (showChange and showPoint) ? goldChg + " / " + goldPt : showChange ? goldChg : goldPt
        
        table.cell(dash, 0, goldRow, "★ GOLD", text_color=color.yellow, bgcolor=bgColor, text_size=ts)
        table.cell(dash, 1, goldRow, "1.00 · " + goldCombo, text_color=bullColor, bgcolor=bgColor, text_size=ts)
        
        for i = 0 to array.size(pCorr) - 1
            rowIdx = goldRow + 1 + i
            lbl    = array.get(pShort, i)
            cVal   = array.get(pCorr, i)
            gVal   = array.get(pChg, i)
            pVal   = array.get(pPoint, i)
            
            corrCol  = cVal >= 0.5 ? bullColor : cVal <= -0.5 ? bearColor : neutralCol
            corrBg   = math.abs(cVal) >= 0.7 ? color.new(corrCol, 70) : bgColor
            arrow    = cVal > 0 ? "▲" : cVal < 0 ? "▼" : "◆"
            corrTxt  = na(cVal) ? "n/a" : arrow + " " + str.tostring(cVal, "#.##")
            combo    = fmtCombo(gVal, pVal)
            
            table.cell(dash, 0, rowIdx, lbl, text_color=color.white, bgcolor=bgColor, text_size=ts)
            table.cell(dash, 1, rowIdx, corrTxt + " · " + combo, text_color=corrCol, bgcolor=corrBg, text_size=ts)
            
    else if layoutMode == "Vertikal bertumpuk"
        table.cell(dash, 0, 0, "GOLD PULSE", text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 1, 0, "",           text_color=color.white, bgcolor=headerColor, text_size=ts)
        
        if showBias
            table.cell(dash, 0, 1, "BIAS", text_color=color.white, bgcolor=biasBg, text_size=ts)
            table.cell(dash, 1, 1, biasTxtShort, text_color=biasCol, bgcolor=biasBg, text_size=ts)
            
        headerRow = showBias ? 2 : 1
        table.cell(dash, 0, headerRow, "PAIR", text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 1, headerRow, "KORELASI", text_color=color.white, bgcolor=headerColor, text_size=ts)
        
        r = headerRow + 1
        table.cell(dash, 0, r,   "★ XAUUSD", text_color=color.yellow, bgcolor=bgColor, text_size=ts)
        table.cell(dash, 1, r,   "1.00 (acuan)", text_color=bullColor, bgcolor=bgColor, text_size=ts)
        table.cell(dash, 0, r+1, "  CHG/PT",  text_color=color.gray, bgcolor=bgColor, text_size=ts)
        table.cell(dash, 1, r+1, fmtCombo(cGOLD, dGOLD), text_color=cGOLD > 0 ? bullColor : bearColor, bgcolor=bgColor, text_size=ts)
        
        for i = 0 to array.size(pCorr) - 1
            baseRow = headerRow + 2 + i * 2
            lbl     = str.tostring(i + 1) + ". " + array.get(pShort, i)
            cVal    = array.get(pCorr, i)
            gVal    = array.get(pChg, i)
            pVal    = array.get(pPoint, i)
            
            corrCol = cVal >= 0.5 ? bullColor : cVal <= -0.5 ? bearColor : neutralCol
            corrTxt = na(cVal) ? "n/a" : str.tostring(cVal, "#.##")
            statusShort = na(cVal) ? "" : math.abs(cVal) >= 0.7 ? "★" : math.abs(cVal) >= 0.4 ? "●" : "○"
            ptCol   = na(gVal) ? neutralCol : gVal > 0 ? bullColor : gVal < 0 ? bearColor : neutralCol
            
            table.cell(dash, 0, baseRow,   lbl, text_color=color.white, bgcolor=bgColor, text_size=ts)
            table.cell(dash, 1, baseRow,   statusShort + " " + corrTxt, text_color=corrCol, bgcolor=bgColor, text_size=ts)
            table.cell(dash, 0, baseRow+1, "  CHG/PT", text_color=color.gray, bgcolor=bgColor, text_size=ts)
            table.cell(dash, 1, baseRow+1, fmtCombo(gVal, pVal), text_color=ptCol, bgcolor=bgColor, text_size=ts)
            
    else
        table.cell(dash, 0, 0, "GOLD",     text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 1, 0, "CORR",     text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 2, 0, "CHG / PT", text_color=color.white, bgcolor=headerColor, text_size=ts)
        
        if showBias
            table.cell(dash, 0, 1, "BIAS",       text_color=color.white, bgcolor=biasBg, text_size=ts)
            table.cell(dash, 1, 1, biasTxtShort, text_color=biasCol,     bgcolor=biasBg, text_size=ts)
            table.cell(dash, 2, 1, "",           text_color=color.white, bgcolor=biasBg, text_size=ts)
            
        headerRow = showBias ? 2 : 1
        table.cell(dash, 0, headerRow, "PAIR", text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 1, headerRow, "KOR",  text_color=color.white, bgcolor=headerColor, text_size=ts)
        table.cell(dash, 2, headerRow, "CHG / PT", text_color=color.white, bgcolor=headerColor, text_size=ts)
        
        goldRow = headerRow + 1
        table.cell(dash, 0, goldRow, "★ GOLD", text_color=color.yellow, bgcolor=bgColor, text_size=ts)
        table.cell(dash, 1, goldRow, "1.00",   text_color=bullColor,    bgcolor=bgColor, text_size=ts)
        table.cell(dash, 2, goldRow, fmtCombo(cGOLD, dGOLD), text_color=cGOLD > 0 ? bullColor : bearColor, bgcolor=bgColor, text_size=ts)
        
        for i = 0 to array.size(pCorr) - 1
            rowIdx = goldRow + 1 + i
            lbl    = array.get(pShort, i)
            cVal   = array.get(pCorr, i)
            gVal   = array.get(pChg, i)
            pVal   = array.get(pPoint, i)
            
            corrCol = cVal >= 0.5 ? bullColor : cVal <= -0.5 ? bearColor : neutralCol
            arrow   = cVal > 0 ? "▲" : cVal < 0 ? "▼" : "◆"
            corrTxt = na(cVal) ? "n/a" : arrow + str.tostring(cVal, "#.##")
            ptCol   = na(gVal) ? neutralCol : gVal > 0 ? bullColor : gVal < 0 ? bearColor : neutralCol
            
            table.cell(dash, 0, rowIdx, lbl,     text_color=color.white, bgcolor=bgColor, text_size=ts)
            table.cell(dash, 1, rowIdx, corrTxt, text_color=corrCol,     bgcolor=bgColor, text_size=ts)
            table.cell(dash, 2, rowIdx, fmtCombo(gVal, pVal), text_color=ptCol, bgcolor=bgColor, text_size=ts)

← 返回列表