本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =====================================================================
// Gold Strength Basket
// Version: 1.0
//
// Is gold rising, or is the dollar falling? This script prices gold in
// five other currencies (EUR, GBP, JPY, CHF, AUD), measures each one's
// move over the same window as XAU/USD, and averages them into an
// "ex-dollar" gold return. The gap between the USD return and the
// basket return is the part of the move that belongs to the dollar.
//
// Two windows: since today's daily open (default) or a rolling number
// of bars. Sub-pane plot, zero line, compact dashboard, four alerts.
//
// No lookahead. Pine v6 strict.
// =====================================================================
indicator("Gold Strength Basket", shorttitle = "Gold Basket",
overlay = false, format = format.percent, precision = 2,
max_labels_count = 50)
// =============================
// INPUTS
// =============================
grpSym = "Symbols"
symUSD = input.symbol("OANDA:XAUUSD", "Gold in USD", group = grpSym)
symEUR = input.symbol("OANDA:XAUEUR", "Gold in EUR", group = grpSym)
symGBP = input.symbol("OANDA:XAUGBP", "Gold in GBP", group = grpSym)
symJPY = input.symbol("OANDA:XAUJPY", "Gold in JPY", group = grpSym)
symCHF = input.symbol("OANDA:XAUCHF", "Gold in CHF", group = grpSym)
symAUD = input.symbol("OANDA:XAUAUD", "Gold in AUD", group = grpSym)
grpWin = "Window"
winMode = input.string("Since daily open", "Return window",
options = ["Since daily open", "Rolling bars"], group = grpWin,
tooltip = "Since daily open: each return is measured from today's daily open. Rolling bars: from N bars ago on the chart timeframe.")
winBars = input.int(48, "Rolling window (bars)", minval = 2, maxval = 2000, group = grpWin)
grpRead = "Reading"
dollarThr = input.float(0.25, "Dollar-driven threshold (% of move)", minval = 0.05, maxval = 2.0, step = 0.05, group = grpRead,
tooltip = "When the gap between the USD return and the basket return exceeds this many percentage points, the dashboard reads the move as dollar-driven.")
grpUI = "Display"
showDash = input.bool(true, "Show dashboard", group = grpUI)
dashPos = input.string("top_right", "Dashboard position", options = ["top_right", "top_left", "bottom_right", "bottom_left", "middle_right"], group = grpUI)
dashSize = input.string("small", "Dashboard size", options = ["tiny", "small", "normal"], group = grpUI)
colUSD = input.color(#FFA726, "Gold in USD", group = grpUI)
colBask = input.color(#42A5F5, "Ex-dollar basket", group = grpUI)
colDlrUp = input.color(#26A69A, "Dollar effect (positive)", group = grpUI)
colDlrDn = input.color(#EF5350, "Dollar effect (negative)", group = grpUI)
// =============================
// DATA
// One request per symbol, on the chart timeframe. The daily open is
// fetched on the same symbol so the anchor is consistent per currency.
// =============================
f_ret(simple string sym) =>
float px = request.security(sym, timeframe.period, close,
lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
float dOpen = request.security(sym, "D", open,
lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
float base = winMode == "Since daily open" ? dOpen : px[winBars]
float r = (not na(px) and not na(base) and base > 0) ? 100.0 * (px / base - 1.0) : na
r
float rUSD = f_ret(symUSD)
float rEUR = f_ret(symEUR)
float rGBP = f_ret(symGBP)
float rJPY = f_ret(symJPY)
float rCHF = f_ret(symCHF)
float rAUD = f_ret(symAUD)
// Basket = mean of the available ex-dollar returns
float sumB = 0.0
int nB = 0
if not na(rEUR)
sumB += rEUR
nB += 1
if not na(rGBP)
sumB += rGBP
nB += 1
if not na(rJPY)
sumB += rJPY
nB += 1
if not na(rCHF)
sumB += rCHF
nB += 1
if not na(rAUD)
sumB += rAUD
nB += 1
float rBasket = nB > 0 ? sumB / nB : na
// Dollar effect: what XAU/USD did beyond gold's move in other currencies
float dollarFx = (not na(rUSD) and not na(rBasket)) ? rUSD - rBasket : na
// =============================
// PLOTS
// =============================
plot(rUSD, "Gold in USD (%)", color = colUSD, linewidth = 2)
plot(rBasket, "Ex-dollar basket (%)", color = colBask, linewidth = 2)
plot(dollarFx, "Dollar effect (%)", color = na(dollarFx) ? color.gray : dollarFx >= 0 ? color.new(colDlrUp, 40) : color.new(colDlrDn, 40),
style = plot.style_columns)
hline(0, "Zero", color = color.new(color.gray, 40), linestyle = hline.style_dotted)
// =============================
// DASHBOARD
// =============================
f_pos(string p) =>
p == "top_left" ? position.top_left :
p == "top_right" ? position.top_right :
p == "bottom_left" ? position.bottom_left :
p == "bottom_right" ? position.bottom_right : position.middle_right
f_size(string s) =>
s == "tiny" ? size.tiny :
s == "normal" ? size.normal : size.small
f_pct(float v) => na(v) ? "—" : (v >= 0 ? "+" : "") + str.tostring(v, "#.00") + "%"
// Strongest / weakest currency pair for gold (highest / lowest return)
f_extremes() =>
string hiN = "—"
string loN = "—"
float hiV = na
float loV = na
if not na(rEUR)
hiN := "EUR"
loN := "EUR"
hiV := rEUR
loV := rEUR
if not na(rGBP) and (na(hiV) or rGBP > hiV)
hiN := "GBP"
hiV := rGBP
if not na(rGBP) and (na(loV) or rGBP < loV)
loN := "GBP"
loV := rGBP
if not na(rJPY) and (na(hiV) or rJPY > hiV)
hiN := "JPY"
hiV := rJPY
if not na(rJPY) and (na(loV) or rJPY < loV)
loN := "JPY"
loV := rJPY
if not na(rCHF) and (na(hiV) or rCHF > hiV)
hiN := "CHF"
hiV := rCHF
if not na(rCHF) and (na(loV) or rCHF < loV)
loN := "CHF"
loV := rCHF
if not na(rAUD) and (na(hiV) or rAUD > hiV)
hiN := "AUD"
hiV := rAUD
if not na(rAUD) and (na(loV) or rAUD < loV)
loN := "AUD"
loV := rAUD
[hiN, hiV, loN, loV]
var table dash = table.new(position = f_pos(dashPos), columns = 2, rows = 7,
bgcolor = color.new(color.black, 80), border_color = color.new(color.gray, 60), border_width = 1)
if showDash and barstate.islast
[hiN, hiV, loN, loV] = f_extremes()
string readTxt = "loading"
color readCol = color.gray
if not na(rUSD) and not na(rBasket)
bool sameSign = (rUSD >= 0 and rBasket >= 0) or (rUSD < 0 and rBasket < 0)
if math.abs(dollarFx) <= dollarThr and sameSign
readTxt := rUSD >= 0 ? "BROAD GOLD STRENGTH" : "BROAD GOLD WEAKNESS"
readCol := rUSD >= 0 ? colDlrUp : colDlrDn
else if not sameSign or math.abs(rBasket) < math.abs(dollarFx)
readTxt := dollarFx >= 0 ? "MOSTLY DOLLAR WEAKNESS" : "MOSTLY DOLLAR STRENGTH"
readCol := colUSD
else
readTxt := "GOLD MOVE + DOLLAR TILT"
readCol := color.silver
string winTxt = winMode == "Since daily open" ? "since daily open" : str.tostring(winBars) + " bars"
table.clear(dash, 0, 0, 1, 6)
table.cell(dash, 0, 0, "Gold Basket", text_color = color.white, text_size = f_size(dashSize), bgcolor = color.new(color.black, 40))
table.cell(dash, 1, 0, winTxt, text_color = color.silver, text_size = f_size(dashSize), bgcolor = color.new(color.black, 40))
table.cell(dash, 0, 1, "Gold in USD", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 1, f_pct(rUSD), text_color = colUSD, text_size = f_size(dashSize))
table.cell(dash, 0, 2, "Ex-dollar basket (" + str.tostring(nB) + ")", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 2, f_pct(rBasket), text_color = colBask, text_size = f_size(dashSize))
table.cell(dash, 0, 3, "Dollar effect", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 3, f_pct(dollarFx), text_color = na(dollarFx) ? color.silver : dollarFx >= 0 ? colDlrUp : colDlrDn, text_size = f_size(dashSize))
table.cell(dash, 0, 4, "Strongest vs", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 4, hiN + " " + f_pct(hiV), text_color = color.white, text_size = f_size(dashSize))
table.cell(dash, 0, 5, "Weakest vs", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 5, loN + " " + f_pct(loV), text_color = color.white, text_size = f_size(dashSize))
table.cell(dash, 0, 6, "Read", text_color = color.silver, text_size = f_size(dashSize))
table.cell(dash, 1, 6, readTxt, text_color = readCol, text_size = f_size(dashSize))
// =============================
// ALERTS
// =============================
alertcondition(ta.crossover(dollarFx, dollarThr), "Dollar weakness takes over", "The gap between gold in USD and the ex-dollar basket crossed above the threshold: the move is now mostly dollar weakness.")
alertcondition(ta.crossunder(dollarFx, -dollarThr), "Dollar strength takes over", "The gap between gold in USD and the ex-dollar basket crossed below the threshold: the move is now mostly dollar strength.")
alertcondition(ta.crossover(rBasket, 0.0), "Basket turns positive", "Gold in EUR, GBP, JPY, CHF and AUD is now up on average over the window.")
alertcondition(ta.crossunder(rBasket, 0.0), "Basket turns negative", "Gold in EUR, GBP, JPY, CHF and AUD is now down on average over the window.")
// No indicator is infallible. A decomposition describes the move; it does not predict the next one.