Aquile Reali Macro Compass

giua64 · study · 277 行 · 点赞 146 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
// © giua64 — Aquile Reali
//@version=6
indicator("Aquile Reali Macro Compass", shorttitle = "AR Compass", overlay = false, precision = 3)
 
// ═══════════════════════════════════════════════════════════
// PANEL SELECTION
// Add the indicator once per pane and pick a different panel each time.
// Suggested layout under a gold chart: DXY · Yield Curve · TIP · US10Y + US02Y · WTI
// ═══════════════════════════════════════════════════════════
lang  = input.string("English", "Language / Lingua", options = ["English", "Italiano"])
isIT  = lang == "Italiano"
tr(string en, string itx) => isIT ? itx : en
 
panel = input.string("DXY", "Panel",
     options = ["DXY", "Yield Curve 10Y-2Y", "TIP", "US10Y + US02Y", "WTI Crude Oil", "DFII10 (daily data)", "Custom"])
 
// ─── Line colors ──────────────────────────────────────────
gC      = "Line colors"
cDXY    = input.color(#00897B, "DXY", group = gC)
cCurve  = input.color(#9C27B0, "Yield Curve 10Y-2Y", group = gC)
cTIP    = input.color(#42A5F5, "TIP", group = gC)
c10Y    = input.color(#E53935, "US10Y", group = gC)
c02Y    = input.color(#FB8C00, "US02Y", group = gC)
cWTI    = input.color(#212121, "WTI Crude Oil", group = gC)
cDFII   = input.color(#6D4C41, "DFII10", group = gC)
cCustA  = input.color(#1848CC, "Custom – line A", group = gC)
cCustB  = input.color(#D4A017, "Custom – line B", group = gC)
colZero = input.color(color.new(color.gray, 40), "Zero line", group = gC)
colTxt  = input.color(color.gray, "Label text", group = gC)
 
// ─── Style ────────────────────────────────────────────────
gS       = "Style"
lineW    = input.int(2, "Line width", minval = 1, maxval = 4, group = gS)
showZero = input.bool(true, "Show zero line (Yield Curve)", group = gS)
showScl  = input.bool(false, "Show labels on price scale", group = gS)
showDesc = input.bool(true, "Show description under the title", group = gS)
colDesc  = input.color(color.gray, "Description color", group = gS)
dispPlot = showScl ? display.all : display.pane + display.data_window + display.status_line
 
// ─── Custom panel ─────────────────────────────────────────
gU      = "Custom panel"
uTitle  = input.string("Custom", "Title", group = gU)
uMode   = input.string("Single line", "Mode", options = ["Single line", "Two lines", "Spread A - B"], group = gU)
uSymA   = input.symbol("TVC:GOLD", "Symbol A", group = gU)
uSymB   = input.symbol("TVC:US02Y", "Symbol B", group = gU)
uDesc   = input.string("", "Description", group = gU)
 
// ─── Presets ──────────────────────────────────────────────
title = switch panel
    "DXY"                 => tr("DXY – Dollar Index", "DXY – Indice del dollaro")
    "Yield Curve 10Y-2Y"  => tr("Yield Curve 10Y-2Y", "Curva dei rendimenti 10Y-2Y")
    "TIP"                 => tr("TIP – Real Yield Proxy", "TIP – Proxy rendimenti reali")
    "US10Y + US02Y"       => "US10Y / US02Y"
    "WTI Crude Oil"       => tr("WTI – Crude Oil", "WTI – Petrolio")
    "DFII10 (daily data)" => tr("Real 10Y Yield (DFII10)", "Rendimento reale 10Y (DFII10)")
    => uTitle
 
mode = switch panel
    "Yield Curve 10Y-2Y" => "Spread A - B"
    "US10Y + US02Y"      => "Two lines"
    "Custom"             => uMode
    => "Single line"
 
symA = switch panel
    "DXY"                 => "TVC:DXY"
    "Yield Curve 10Y-2Y"  => "TVC:US10Y"
    "TIP"                 => "AMEX:TIP"
    "US10Y + US02Y"       => "TVC:US10Y"
    "WTI Crude Oil"       => "NYMEX:CL1!"
    "DFII10 (daily data)" => "FRED:DFII10"
    => uSymA
 
symB = switch panel
    "Yield Curve 10Y-2Y" => "TVC:US02Y"
    "US10Y + US02Y"      => "TVC:US02Y"
    => uSymB
 
colA = switch panel
    "DXY"                 => cDXY
    "Yield Curve 10Y-2Y"  => cCurve
    "TIP"                 => cTIP
    "US10Y + US02Y"       => c10Y
    "WTI Crude Oil"       => cWTI
    "DFII10 (daily data)" => cDFII
    => cCustA
 
colB = panel == "US10Y + US02Y" ? c02Y : cCustB
 
desc = switch panel
    "DXY"                 => tr("Dollar up = gold down  ·  Dollar down = gold up", "Dollaro su = oro giù  ·  Dollaro giù = oro su")
    "Yield Curve 10Y-2Y"  => tr("10Y minus 2Y  ·  Falling = market prices Fed hikes  ·  Rising = cuts or long-end stress  ·  Below 0 = recession warning", "Distanza 10Y−2Y  ·  Scende = mercato prezza rialzi Fed  ·  Sale = tagli o paura sul lungo  ·  Sotto 0 = segnale recessione")
    "TIP"                 => tr("TIP up = real yields down = gold up  ·  TIP down = gold down  ·  Moves only during US cash hours", "TIP su = rendimenti reali giù = oro su  ·  TIP giù = oro giù  ·  Si muove solo 15:30–22:00")
    "US10Y + US02Y"       => tr("Rate levels  ·  Rising rates = pressure on gold  ·  2Y rising faster than 10Y = Fed hike expectations", "Livello dei tassi  ·  Tassi su = oro sotto pressione  ·  2Y più veloce del 10Y = attese di rialzi Fed")
    "WTI Crude Oil"       => tr("Oil up = inflation expectations up = yields up = gold down", "Petrolio su = inflazione attesa su = rendimenti su = oro giù")
    "DFII10 (daily data)" => tr("Real yield up = gold down  ·  Daily data, do not use intraday", "Rendimento reale su = oro giù  ·  Dato giornaliero, non usarlo intraday")
    => uDesc
 
// ═══════════════════════════════════════════════════════════
// DATA
// Futures bars (e.g. COMEX gold) close at 17:00 New York, while TVC yield and
// dollar bars run midnight to midnight: the last TVC bar of the period closes
// AFTER the chart bar and would be skipped. A lower timeframe is requested so
// its bars fit inside the chart bar and the real last value is used.
// ═══════════════════════════════════════════════════════════
tfReq = timeframe.ismonthly ? "D" : timeframe.isweekly ? "240" : timeframe.isdaily ? "60" : timeframe.in_seconds() > 3600 ? "60" : timeframe.period
a = request.security(symA, tfReq, close, ignore_invalid_symbol = true)
b = request.security(symB, tfReq, close, ignore_invalid_symbol = true)
 
isSpread = mode == "Spread A - B"
isTwo    = mode == "Two lines"
valA = isSpread ? a - b : a
valB = isTwo ? b : na
 
plot(valA, "Value", color = colA, linewidth = lineW, display = dispPlot)
plot(valB, "Value 2", color = colB, linewidth = lineW, display = dispPlot)
plot(isSpread and showZero ? 0.0 : na, "Zero", color = colZero, linewidth = 1, display = dispPlot)
 
// ═══════════════════════════════════════════════════════════
// MAIN PANEL: COMPASS DASHBOARD + ALERTS
// Enable in ONE pane only, otherwise alerts are duplicated.
// ═══════════════════════════════════════════════════════════
gM       = "Main panel: compass + alerts (enable in ONE pane only)"
isMain   = input.bool(false, "This is the main panel (compass dashboard + alerts)", group = gM)
nConf    = input.int(3, "Lookback bars for direction", minval = 1, maxval = 20, group = gM)
dashPos  = input.string("Bottom right", "Dashboard position", options = ["Bottom right", "Bottom left", "Top left"], group = gM)
dashSize = input.string("Medium", "Dashboard size", options = ["Small", "Medium", "Large"], group = gM)
szDash   = dashSize == "Small" ? size.small : dashSize == "Medium" ? size.normal : size.large
 
gAl       = "Alerts"
aCompass  = input.bool(true, "Compass state change (TIP vs DXY)", group = gAl)
useFilter = input.bool(true, "Compass alerts only in the direction of the higher trend", group = gAl)
tfFilter  = input.timeframe("W", "Higher trend timeframe", group = gAl)
aSpike    = input.bool(true, "Sharp US02Y move", group = gAl)
spikeBp   = input.float(10, "US02Y threshold (basis points per bar)", minval = 1, group = gAl)
aCurve    = input.bool(true, "Yield curve crossing zero", group = gAl)
 
gL      = "Level alerts (0 = disabled)"
dxyUp   = input.float(101.6, "DXY above", group = gL)
dxyDn   = input.float(99.9, "DXY below", group = gL)
y10Up   = input.float(5.23, "US10Y above", group = gL)
y10Dn   = input.float(5.00, "US10Y below", group = gL)
wtiUp   = input.float(97.0, "WTI above", group = gL)
wtiDn   = input.float(88.7, "WTI below", group = gL)
pxUp    = input.float(4389, "Chart price above", group = gL)
pxDn    = input.float(4278, "Chart price below", group = gL)
 
dxy = request.security("TVC:DXY",    tfReq, close, ignore_invalid_symbol = true)
tip = request.security("AMEX:TIP",   tfReq, close, ignore_invalid_symbol = true)
y10 = request.security("TVC:US10Y",  tfReq, close, ignore_invalid_symbol = true)
y02 = request.security("TVC:US02Y",  tfReq, close, ignore_invalid_symbol = true)
wti = request.security("NYMEX:CL1!", tfReq, close, ignore_invalid_symbol = true)
curve = y10 - y02
 
// TIP only moves during US cash hours: on intraday charts the compass is updated only then
tipLive = not timeframe.isintraday or not na(time(timeframe.period, "0930-1600", "America/New_York"))
 
// Compass state: +1 bullish gold, -1 bearish gold, 0 mixed signal (no trade)
cTip = ta.change(tip, nConf)
cDxy = ta.change(dxy, nConf)
newState = cTip > 0 and cDxy < 0 ? 1 : cTip < 0 and cDxy > 0 ? -1 : 0
var int state = 0
if tipLive and not na(newState)
    state := newState
stateChanged = state != state[1]
 
stateTF(simple string tf) =>
    ct = request.security("AMEX:TIP", tf, ta.change(close, nConf), ignore_invalid_symbol = true)
    cd = request.security("TVC:DXY",  tf, ta.change(close, nConf), ignore_invalid_symbol = true)
    ct > 0 and cd < 0 ? 1 : ct < 0 and cd > 0 ? -1 : 0
 
s1H  = stateTF("60")
s4H  = stateTF("240")
s8H  = stateTF("480")
s12H = stateTF("720")
sD   = stateTF("D")
sW   = stateTF("W")
sM   = stateTF("M")
sFilter = stateTF(tfFilter)
filterChanged = sFilter != sFilter[1]
 
stateName(s)  => s == 1 ? tr("BULLISH gold", "LONG oro") : s == -1 ? tr("BEARISH gold", "SHORT oro") : tr("mixed signal", "segnale sporco")
stateShort(s) => s == 1 ? tr("BULL", "LONG") : s == -1 ? tr("BEAR", "SHORT") : tr("mixed", "sporco")
stateEmoji(s) => s == 1 ? "🟢" : s == -1 ? "🔴" : "⚪"
stateColor(s) => s == 1 ? #26A69A : s == -1 ? #EF5350 : #9E9E9E
 
fmt(x)   => str.tostring(x, "#.###")
arrow(x) => x > 0 ? " ↑" : x < 0 ? " ↓" : " →"
 
// Crossings computed on every bar, then enabled only if the level is not 0
cxDxyUp = ta.crossover(dxy, dxyUp)
cxDxyDn = ta.crossunder(dxy, dxyDn)
cxY10Up = ta.crossover(y10, y10Up)
cxY10Dn = ta.crossunder(y10, y10Dn)
cxWtiUp = ta.crossover(wti, wtiUp)
cxWtiDn = ta.crossunder(wti, wtiDn)
cxPxUp  = ta.crossover(close, pxUp)
cxPxDn  = ta.crossunder(close, pxDn)
xDxyUp = dxyUp > 0 and cxDxyUp
xDxyDn = dxyDn > 0 and cxDxyDn
xY10Up = y10Up > 0 and cxY10Up
xY10Dn = y10Dn > 0 and cxY10Dn
xWtiUp = wtiUp > 0 and cxWtiUp
xWtiDn = wtiDn > 0 and cxWtiDn
xPxUp  = pxUp > 0 and cxPxUp
xPxDn  = pxDn > 0 and cxPxDn
xCurDn = ta.crossunder(curve, 0)
xCurUp = ta.crossover(curve, 0)
bp02   = (y02 - y02[1]) * 100
 
msg = ""
if aCompass and useFilter and filterChanged
    msg += "🧭 " + tfFilter + tr(" trend changed: ", " trend cambia: ") + stateEmoji(sFilter) + " " + stateName(sFilter) + "\n"
okFilter = not useFilter or (state != 0 and state == sFilter)
if aCompass and stateChanged and okFilter
    msg += stateEmoji(state) + tr(" Compass ", " Bussola ") + stateName(state) + " (" + timeframe.period + (useFilter ? tr(", with the ", ", a favore del trend ") + tfFilter + tr(" trend", "") : "") + ")"
    msg += " | DXY " + fmt(dxy) + arrow(cDxy) + " / TIP " + fmt(tip) + arrow(cTip) + "\n"
if xDxyUp
    msg += tr("⚠️ DXY above ", "⚠️ DXY sopra ") + fmt(dxyUp) + " (" + fmt(dxy) + ")\n"
if xDxyDn
    msg += tr("⚠️ DXY below ", "⚠️ DXY sotto ") + fmt(dxyDn) + " (" + fmt(dxy) + ")\n"
if xY10Up
    msg += tr("⚠️ US10Y above ", "⚠️ US10Y sopra ") + fmt(y10Up) + "% (" + fmt(y10) + ")\n"
if xY10Dn
    msg += tr("⚠️ US10Y below ", "⚠️ US10Y sotto ") + fmt(y10Dn) + "% (" + fmt(y10) + ")\n"
if xWtiUp
    msg += tr("🛢️ WTI above ", "🛢️ WTI sopra ") + fmt(wtiUp) + " (" + fmt(wti) + ")\n"
if xWtiDn
    msg += tr("🛢️ WTI below ", "🛢️ WTI sotto ") + fmt(wtiDn) + " (" + fmt(wti) + ")\n"
if xPxUp
    msg += "⚠️ " + syminfo.ticker + tr(" above ", " sopra ") + fmt(pxUp) + " (" + fmt(close) + ")\n"
if xPxDn
    msg += "⚠️ " + syminfo.ticker + tr(" below ", " sotto ") + fmt(pxDn) + " (" + fmt(close) + ")\n"
if aSpike and math.abs(bp02) >= spikeBp
    msg += "⚡ US02Y " + (bp02 > 0 ? "+" : "") + str.tostring(bp02, "#.#") + tr(" bp in one bar (", " pb in una barra (") + fmt(y02) + ")\n"
if aCurve and xCurDn
    msg += tr("🔻 Yield curve 10Y-2Y INVERTED (", "🔻 Curva 10Y-2Y INVERTITA (") + fmt(curve) + ")\n"
if aCurve and xCurUp
    msg += tr("🔺 Yield curve 10Y-2Y back above zero (", "🔺 Curva 10Y-2Y tornata positiva (") + fmt(curve) + ")\n"
 
if isMain and msg != ""
    alert(msg, alert.freq_once_per_bar_close)
 
// ═══════════════════════════════════════════════════════════
// LABELS
// ═══════════════════════════════════════════════════════════
nameA = panel == "US10Y + US02Y" ? "US10Y" : title
nameB = panel == "US10Y + US02Y" ? "US02Y" : uTitle + " B"
clear = color.new(color.black, 100)
 
var table tb = table.new(position.top_center, 2, 3)
varip bool merged = false
if barstate.islast
    table.cell(tb, 0, 0, nameA + "  " + fmt(valA), text_color = colA, text_size = size.large, bgcolor = clear, tooltip = desc)
    if isTwo
        table.cell(tb, 1, 0, "   " + nameB + "  " + fmt(valB), text_color = colB, text_size = size.large, bgcolor = clear, tooltip = desc)
    if isMain
        table.cell(tb, 0, 1, stateEmoji(state) + tr(" Compass ", " Bussola ") + timeframe.period + ": " + stateName(state), text_color = colTxt, text_size = szDash, bgcolor = clear)
    if showDesc and desc != ""
        table.cell(tb, 0, 2, desc, text_color = colDesc, text_size = size.small, bgcolor = clear)
        if isTwo
            table.cell(tb, 1, 2, "", bgcolor = clear)
            if not merged
                table.merge_cells(tb, 0, 2, 1, 2)
                merged := true
 
posD = dashPos == "Bottom right" ? position.bottom_right : dashPos == "Bottom left" ? position.bottom_left : position.top_left
var table dash = table.new(posD, 8, 2, border_width = 1, border_color = color.new(color.gray, 70))
if barstate.islast and isMain
    tfs = array.from("1H", "4H", "8H", "12H", "D", "W", "M")
    sts = array.from(s1H, s4H, s8H, s12H, sD, sW, sM)
    table.cell(dash, 0, 0, "🧭", text_color = colTxt, text_size = szDash, bgcolor = clear)
    table.cell(dash, 0, 1, tr("Compass", "Bussola"), text_color = colTxt, text_size = szDash, bgcolor = clear)
    for i = 0 to 6
        s = array.get(sts, i)
        table.cell(dash, i + 1, 0, array.get(tfs, i), text_color = colTxt, text_size = szDash, bgcolor = clear)
        table.cell(dash, i + 1, 1, stateShort(s), text_color = color.white, text_size = szDash, bgcolor = stateColor(s))

← 返回列表