Gold Forward Read

fundbyshaoor · study · 219 行 · 点赞 14 · TradingView 原页

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

Pine Script

//@version=6
indicator("Gold Forward", "XAU Forward", overlay=false, precision=2, max_bars_back=500)
 
// Put this on an XAUUSD CFD chart, 1 to 15 minutes.
//
// How these markets usually react with gold:
//   DXY down, gold up. The dollar link is inverse.
//   Yields down, gold up. The yield link is inverse.
//   Oil up, gold up, when they are moving together.
// The script measures that link on recent bars. If it flips, the color flips.
// Green means that market is pushing gold up. Red means it is pushing gold down.
//
// Columns are the part of that push gold has not priced yet.
// UNSTABLE means gold and the dollar are rising or falling together.
//
// Defaults are the free TradingView CFD/index quotes, not futures:
//   Dollar  TVC:DXY
//   Yield   TVC:US10Y
//   Oil     TVC:USOIL
// If a row says invalid, open that instrument from the same place you
// open XAUUSD and paste that exact symbol into the input.
// If you replace US10Y with a price that rises when yields fall, turn off
// "Falling yield is gold-bullish".
 
grpSym = "Symbols"
grpImp = "Impulse"
grpSig = "Signal"
grpSes = "Session"
 
dxySym     = input.symbol("TVC:DXY", "Dollar CFD", group=grpSym, tooltip="US dollar index. If invalid, pick DXY from the same broker as your XAUUSD.")
yldSym     = input.symbol("TVC:US10Y", "US 10-year yield", group=grpSym, tooltip="Yield quote, not a CFD you trade. If invalid, search US10Y and pick the one that opens.")
oilSym     = input.symbol("TVC:USOIL", "WTI oil CFD", group=grpSym, tooltip="WTI crude CFD. If invalid, search USOIL or WTI from your broker.")
invertYld  = input.bool(true, "Falling yield is gold-bullish", group=grpSym, tooltip="On for US10Y. Off if the yield symbol is a price that rises when yields fall.")
 
impLen     = input.int(3, "Impulse length", minval=2, group=grpImp, tooltip="Bars in the current burst. On a 1-minute chart, 3 is three minutes.")
zLen       = input.int(120, "Z-score lookback", minval=30, group=grpImp)
corrLen    = input.int(30, "Correlation length", minval=10, group=grpImp)
wDxy       = input.float(1.0, "Dollar weight", minval=0.0, step=0.1, group=grpImp)
wYld       = input.float(1.0, "Yield weight", minval=0.0, step=0.1, group=grpImp)
wOil       = input.float(0.35, "Oil confirm weight", minval=0.0, step=0.05, group=grpImp)
 
agreeThr   = input.float(0.85, "Agreement z", minval=0.1, step=0.05, group=grpSig, tooltip="Both the dollar and the yield must clear this before a marker can print.")
lagThr     = input.float(1.25, "Catch-up lag", minval=0.2, step=0.05, group=grpSig, tooltip="How far gold must trail the macro impulse.")
breakThr   = input.float(0.35, "Correlation break", minval=0.05, maxval=0.95, step=0.05, group=grpSig, tooltip="Gold vs dollar correlation above this means they are moving together.")
breakBand  = input.float(0.15, "Break exit band", minval=0.05, step=0.05, group=grpSig)
coolBars   = input.int(10, "Min bars between markers", minval=1, group=grpSig)
useReal    = input.bool(true, "Block against daily real yield", group=grpSig)
realDead   = input.float(0.02, "Real-yield deadzone", minval=0.0, step=0.005, group=grpSig, tooltip="Daily change, in yield points. Inside this band both sides are allowed. 0.02 is 2 basis points.")
onClose    = input.bool(true, "Alerts on bar close", group=grpSig)
 
useSess    = input.bool(false, "London to New York only", group=grpSes)
sess       = input.session("0300-1700", "Session (New York hours)", group=grpSes)
 
chg(src, len) =>
    na(src) or na(src[len]) ? na : src - src[len]
 
zscore(src, len) =>
    dev = ta.stdev(src, len)
    avg = ta.sma(src, len)
    na(src) or na(dev) or dev == 0.0 ? na : (src - avg) / dev
 
dxy = request.security(dxySym, timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol=true)
yld = request.security(yldSym, timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol=true)
oil = request.security(oilSym, timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol=true)
// Daily real yield. If this feed is missing, the filter stays off.
realChg = request.security("FRED:DFII10", "D", close[1] - close[2], barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol=true)
 
zGold = zscore(chg(close, impLen), zLen)
zDxy  = zscore(chg(dxy, impLen), zLen)
zYld  = zscore(chg(yld, impLen), zLen)
zOil  = zscore(chg(oil, impLen), zLen)
 
// Live link: positive means this market is moving with gold, negative means opposite.
cD = ta.correlation(chg(close, 1), chg(dxy, 1), corrLen)
cY = ta.correlation(chg(close, 1), chg(yld, 1), corrLen)
cO = ta.correlation(chg(close, 1), chg(oil, 1), corrLen)
corr = cD
 
linkMin = 0.15
dirOf(float c, float prior) =>
    na(c) or math.abs(c) < linkMin ? prior : c > 0 ? 1.0 : -1.0
 
dxyDir = dirOf(cD, -1.0)
yldDir = dirOf(cY, invertYld ? -1.0 : 1.0)
oilDir = na(cO) or math.abs(cO) < linkMin ? 0.0 : cO > 0 ? 1.0 : -1.0
 
dxyEffect = na(zDxy) ? na : dxyDir * zDxy
yldEffect = na(zYld) ? na : yldDir * zYld
oilEffect = na(zOil) ? 0.0 : oilDir * zOil
 
wSum = wDxy + wYld
core = wSum > 0.0 and not na(dxyEffect) and not na(yldEffect) ? (wDxy * dxyEffect + wYld * yldEffect) / wSum : na
oilPart = math.max(-0.5, math.min(0.5, wOil * oilEffect))
implied = na(core) ? na : core + oilPart
lag     = na(implied) or na(zGold) ? na : implied - zGold
 
var bool breakOn = false
arm = onClose ? barstate.isconfirmed : true
if arm and not na(corr)
    if corr >= breakThr
        breakOn := true
    else if corr <= breakThr - breakBand
        breakOn := false
 
regimeLongOk  = not useReal or na(realChg) or realChg <= realDead
regimeShortOk = not useReal or na(realChg) or realChg >= -realDead
inSess = not useSess or not na(time(timeframe.period, sess, "America/New_York"))
 
bullAgree = not na(dxyEffect) and not na(yldEffect) and dxyEffect >= agreeThr and yldEffect >= agreeThr
bearAgree = not na(dxyEffect) and not na(yldEffect) and dxyEffect <= -agreeThr and yldEffect <= -agreeThr
 
longSetup  = bullAgree and not breakOn and not na(lag) and lag >= lagThr
shortSetup = bearAgree and not breakOn and not na(lag) and lag <= -lagThr
rawLong    = longSetup and regimeLongOk and inSess
rawShort   = shortSetup and regimeShortOk and inSess
 
var int lastBar = na
coolOk = na(lastBar) ? true : bar_index - lastBar >= coolBars
 
longFire  = arm and coolOk and rawLong and not rawLong[1]
shortFire = arm and coolOk and rawShort and not rawShort[1] and not longFire
breakFire = arm and coolOk and breakOn and not breakOn[1] and not longFire and not shortFire
 
if longFire or shortFire or breakFire
    lastBar := bar_index
 
leanLong  = not breakOn and not rawLong and not na(dxyEffect) and not na(yldEffect) and dxyEffect > 0 and yldEffect > 0 and not na(lag) and lag > 0
leanShort = not breakOn and not rawShort and not na(dxyEffect) and not na(yldEffect) and dxyEffect < 0 and yldEffect < 0 and not na(lag) and lag < 0
banner = na(dxy) ? "FIX DOLLAR" : na(yld) ? "FIX YIELD" : breakOn ? "UNSTABLE" : rawLong ? "LONG" : rawShort ? "SHORT" : leanLong ? "LEAN LONG" : leanShort ? "LEAN SHORT" : "FLAT"
 
upC   = color.rgb(8, 153, 129)
dnC   = color.rgb(242, 54, 69)
muteC = color.rgb(130, 134, 145)
panel = color.rgb(14, 16, 20)
dead   = 0.40
expected = na(lag) ? na : math.abs(lag) < dead ? 0.0 : lag
// While the dollar link is unstable, draw the column on gold's own side.
tape = na(zGold) or math.abs(zGold) < dead ? 0.0 : zGold
shown = breakOn ? tape : expected
 
expCol = breakOn ? muteC : shown > 0 and rawLong ? upC : shown < 0 and rawShort ? dnC : shown > 0 ? color.new(upC, 30) : shown < 0 ? color.new(dnC, 30) : color.new(muteC, 55)
plot(shown, "Expected", style=plot.style_columns, color=expCol, linewidth=2)
plot(0.0, "Zero", color=color.new(muteC, 55), display=display.pane)
 
meter(float z) =>
    float mag = na(z) ? 0.0 : math.min(math.abs(z), 2.5) / 2.5 * 8.0
    int n = math.round(mag)
    n := math.min(8, math.max(0, n))
    string s = ""
    for i = 1 to 8
        s := s + (i <= n ? "█" : "░")
    s
 
sgn(float z) =>
    na(z) ? "—" : (z > 0 ? "+" : "") + str.tostring(z, "#.#")
 
tone(float z) =>
    na(z) or z == 0 ? muteC : z > 0 ? upC : dnC
 
linkWord(float c) =>
    na(c) or math.abs(c) < linkMin ? "weak" : c < 0 ? "inverse" : "with"
 
fmt(x) =>
    na(x) ? "—" : str.tostring(x, "#.#")
 
var table pan = table.new(position.top_right, 4, 4, bgcolor=panel, border_width=0, frame_width=0, force_overlay=true)
 
nameCell(int row, string txt) =>
    table.cell(pan, 0, row, txt, text_color=muteC, text_size=size.small, text_halign=text.align_left, bgcolor=panel)
linkCell(int row, string txt) =>
    table.cell(pan, 1, row, txt, text_color=muteC, text_size=size.small, text_halign=text.align_left, bgcolor=panel)
barCell(int row, string txt, color c) =>
    table.cell(pan, 2, row, txt, text_color=c, text_size=size.small, text_halign=text.align_left, bgcolor=panel)
numCell(int row, string txt, color c) =>
    table.cell(pan, 3, row, txt, text_color=c, text_size=size.small, text_halign=text.align_right, bgcolor=panel)
 
if barstate.islast
    nameCell(0, "DXY")
    linkCell(0, linkWord(cD))
    barCell(0, meter(dxyEffect), tone(dxyEffect))
    numCell(0, sgn(dxyEffect), tone(dxyEffect))
    nameCell(1, "US10Y")
    linkCell(1, linkWord(cY))
    barCell(1, meter(yldEffect), tone(yldEffect))
    numCell(1, sgn(yldEffect), tone(yldEffect))
    nameCell(2, "OIL")
    linkCell(2, linkWord(cO))
    barCell(2, meter(oilEffect), tone(oilEffect))
    numCell(2, oilDir == 0 ? "flat" : sgn(oilEffect), tone(oilEffect))
    callC = banner == "LONG" or banner == "LEAN LONG" ? upC : banner == "SHORT" or banner == "LEAN SHORT" ? dnC : muteC
    nameCell(3, "NEXT")
    linkCell(3, "")
    barCell(3, meter(shown), callC)
    table.cell(pan, 3, 3, banner, text_color=callC, text_size=size.normal, text_halign=text.align_right, bgcolor=panel)
 
if longFire
    msg = "XAU catch-up long. Dollar and yields fell together and gold is behind. Lag " + fmt(lag)
    if onClose
        alert(msg, alert.freq_once_per_bar_close)
    else
        alert(msg, alert.freq_once_per_bar)
 
if shortFire
    msg = "XAU catch-up short. Dollar and yields rose together and gold is behind. Lag " + fmt(lag)
    if onClose
        alert(msg, alert.freq_once_per_bar_close)
    else
        alert(msg, alert.freq_once_per_bar)
 
if breakFire
    msg = "Gold and DXY correlation broke. Fast tape, no direction. Corr " + fmt(corr)
    if onClose
        alert(msg, alert.freq_once_per_bar_close)
    else
        alert(msg, alert.freq_once_per_bar)
 
alertcondition(longFire, "Catch-up long", "Dollar and yields fell together. Gold is lagging.")
alertcondition(shortFire, "Catch-up short", "Dollar and yields rose together. Gold is lagging.")
alertcondition(breakFire, "Relationship break", "Gold and the dollar are moving together. No direction.")

← 返回列表