本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("DXY & VIX Mini Charts + NQ Bias", "DXY/VIX Mini", overlay = true)
// ───────────────────────── Inputs ─────────────────────────
grpS = "Symbols and timeframe"
sym1 = input.symbol("TVC:DXY", "Top chart (DXY)", group = grpS)
sym2 = input.symbol("TVC:VIX", "Bottom chart (VIX)", group = grpS)
miniTf = input.timeframe("5", "Mini-chart and rule timeframe", group = grpS,
tooltip = "5m suits 1m-5m NQ entries. 15m for a slower intraday read, 60m for swing. Blank = same as your chart.")
grpR = "Bias rules (each rule = +1 or -1 for NQ)"
emaLen = input.int(20, "EMA length (on the mini timeframe)", minval = 2, group = grpR)
useDxyOpen = input.bool(true, "DXY vs today's open", group = grpR)
useDxyEma = input.bool(true, "DXY vs its EMA", group = grpR)
useVixPrev = input.bool(true, "VIX vs yesterday's close", group = grpR)
useVixEma = input.bool(true, "VIX vs its EMA", group = grpR)
showDiv = input.bool(true, "Warn when NQ and VIX move the same way", group = grpR)
corrLen = input.int(30, "Correlation window (mini bars)", minval = 10, maxval = 100, group = grpR,
tooltip = "Correlation of bar-to-bar % changes vs your chart symbol over the last N mini bars. -1 = always opposite, 0 = unrelated, +1 = always together.")
staleBars = input.int(3, "Mark as closed after N mini bars with no new data", minval = 1, group = grpR)
grpL = "Layout"
posIn = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Middle Right", "Bottom Right", "Bottom Left"], group = grpL)
styleIn = input.string("Candles", "Style", options = ["Candles", "Line"], group = grpL)
nBars = input.int(50, "Bars per chart", minval = 10, maxval = 100, group = grpL)
nRows = input.int(18, "Height of each chart (rows) - more rows = more detail", minval = 6, maxval = 40, group = grpL)
cellW = input.float(0.25, "Candle width (% of screen)", minval = 0.05, maxval = 2.0, step = 0.01, group = grpL)
gapW = input.float(0.08, "Gap between candles (% of screen)", minval = 0.0, maxval = 1.0, step = 0.01, group = grpL)
cellH = input.float(0.32, "Row height (% of screen)", minval = 0.10, maxval = 3.0, step = 0.05, group = grpL)
showRef = input.bool(true, "Show reference level (DXY open / VIX prev close)", group = grpL)
grpC = "Colors"
upCol = input.color(#26a69a, "Up / good for NQ", group = grpC)
dnCol = input.color(#ef5350, "Down / bad for NQ", group = grpC)
bgCol = input.color(color.new(#131722, 10), "Background", group = grpC)
divCol = input.color(#2a2e39, "Divider / frame", group = grpC)
txtCol = input.color(#d1d4dc, "Neutral text", group = grpC)
refCol = input.color(color.new(#787b86, 55), "Reference level", group = grpC)
wrnCol = input.color(#f7a600, "Warning (unusual correlation)", group = grpC)
// ───────────────────────── Data ─────────────────────────
tfUse = miniTf == "" ? timeframe.period : miniTf
tfMs = timeframe.in_seconds(tfUse) * 1000
nCols = 2 * nBars - 1 // candle columns with a thin gap column between them
// Last n values of a source, built on the requested symbol's own bars.
// Live ticks update the current bar instead of adding a new one.
f_hist(float src, int n) =>
var arr = array.new_float()
var tms = array.new_int()
if tms.size() > 0 and tms.last() == time
arr.set(arr.size() - 1, src)
else
arr.push(src)
tms.push(time)
if arr.size() > n
arr.shift()
tms.shift()
arr
f_last(array<float> a) =>
na(a) ? na : a.size() > 0 ? a.last() : na
// Only hand the history to the chart on the last bar: storing a copy on every bar exceeds memory limits
f_histL(float src, int n) =>
a = f_hist(src, n)
barstate.islast ? a : na
a1O = request.security(sym1, tfUse, f_histL(open, nBars))
a1H = request.security(sym1, tfUse, f_histL(high, nBars))
a1L = request.security(sym1, tfUse, f_histL(low, nBars))
a1C = request.security(sym1, tfUse, f_histL(close, nBars))
a2O = request.security(sym2, tfUse, f_histL(open, nBars))
a2H = request.security(sym2, tfUse, f_histL(high, nBars))
a2L = request.security(sym2, tfUse, f_histL(low, nBars))
a2C = request.security(sym2, tfUse, f_histL(close, nBars))
a1T = request.security(sym1, tfUse, f_histL(float(time), nBars))
a2T = request.security(sym2, tfUse, f_histL(float(time), nBars))
aNC = request.security(syminfo.tickerid, tfUse, f_histL(close, nBars))
aNT = request.security(syminfo.tickerid, tfUse, f_histL(float(time), nBars))
[ema1, t1, c1] = request.security(sym1, tfUse, [ta.ema(close, emaLen), time, close])
[ema2, t2, c2] = request.security(sym2, tfUse, [ta.ema(close, emaLen), time, close])
[open1D, prev1D] = request.security(sym1, "D", [open, close[1]])
[open2D, prev2D] = request.security(sym2, "D", [open, close[1]])
nqOpenD = request.security(syminfo.tickerid, "D", open)
// "Closed" = no new mini bar for staleBars bars (e.g. VIX outside Cboe calculation hours)
nowMs = barstate.islast ? timenow : time
stale1 = na(t1) or nowMs - t1 > staleBars * tfMs
stale2 = na(t2) or nowMs - t2 > staleBars * tfMs
// ───────────────────────── Rules ─────────────────────────
// +1 when value is above reference, -1 when below
f_side(float v, float ref) =>
na(v) or na(ref) ? 0 : v > ref ? 1 : v < ref ? -1 : 0
dxyVsOpen = f_side(c1, open1D)
dxyVsEma = f_side(c1, ema1)
vixVsPrev = f_side(c2, prev2D)
vixVsEma = f_side(c2, ema2)
// DXY up and VIX up are both headwinds for NQ, so the sign is flipped
score1 = stale1 ? 0 : (useDxyOpen ? -dxyVsOpen : 0) + (useDxyEma ? -dxyVsEma : 0)
score2 = stale2 ? 0 : (useVixPrev ? -vixVsPrev : 0) + (useVixEma ? -vixVsEma : 0)
score = score1 + score2
nRules = (stale1 ? 0 : (useDxyOpen ? 1 : 0) + (useDxyEma ? 1 : 0)) + (stale2 ? 0 : (useVixPrev ? 1 : 0) + (useVixEma ? 1 : 0))
biasTxt = "NEUTRAL"
biasCol = txtCol
if nRules >= 3 and score == nRules
biasTxt := "STRONG BULLISH"
biasCol := upCol
else if nRules >= 3 and score == -nRules
biasTxt := "STRONG BEARISH"
biasCol := dnCol
else if score >= 2
biasTxt := "LEAN BULLISH"
biasCol := upCol
else if score <= -2
biasTxt := "LEAN BEARISH"
biasCol := dnCol
// NQ and VIX normally move opposite. Same direction = warning.
divTxt = ""
if showDiv and not stale2 and not na(nqOpenD)
if close > nqOpenD and vixVsPrev > 0
divTxt := " ! NQ up + VIX up"
else if close < nqOpenD and vixVsPrev < 0
divTxt := " ! NQ down + VIX down"
plot(score, "NQ bias score", display = display.data_window)
alertcondition(score >= 2 and score[1] < 2, "NQ bias turned bullish", "DXY/VIX bias turned bullish for NQ")
alertcondition(score <= -2 and score[1] > -2, "NQ bias turned bearish", "DXY/VIX bias turned bearish for NQ")
// ───────────────────────── Correlation vs chart symbol ─────────────────────────
// Pearson correlation of bar-to-bar log changes, matched by bar time
f_corr(array<float> xT, array<float> xC, array<float> nT, array<float> nC, int len) =>
m = map.new<float, float>()
for i = 0 to nT.size() - 1
m.put(nT.get(i), nC.get(i))
a = array.new_float()
b = array.new_float()
int start = math.max(1, xC.size() - len)
if xC.size() > 1
for i = start to xC.size() - 1
float tPrev = xT.get(i - 1)
float tNow = xT.get(i)
if m.contains(tPrev) and m.contains(tNow) and xC.get(i - 1) > 0 and m.get(tPrev) > 0
a.push(math.log(xC.get(i) / xC.get(i - 1)))
b.push(math.log(m.get(tNow) / m.get(tPrev)))
float r = na
if a.size() >= 10
float ma = a.avg()
float mb = b.avg()
float sab = 0.0
float saa = 0.0
float sbb = 0.0
for i = 0 to a.size() - 1
sab += (a.get(i) - ma) * (b.get(i) - mb)
saa += math.pow(a.get(i) - ma, 2)
sbb += math.pow(b.get(i) - mb, 2)
r := saa > 0 and sbb > 0 ? sab / math.sqrt(saa * sbb) : na
r
f_corrTxt(float r) =>
na(r) ? "not enough data" : r <= -0.6 ? "strong inverse" : r <= -0.3 ? "inverse" : r < 0.3 ? "not correlated" : r < 0.6 ? "moving WITH NQ" : "strongly WITH NQ"
// DXY and VIX normally move opposite to NQ: inverse = normal, positive = unusual
f_corrCol(float r) =>
na(r) ? txtCol : r <= -0.3 ? upCol : r < 0.3 ? txtCol : wrnCol
// ───────────────────────── Drawing ─────────────────────────
f_pos(string s) =>
switch s
"Top Left" => position.top_left
"Middle Right" => position.middle_right
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
f_name(string sym) =>
str.split(sym, ":").last()
f_arrow(int side) =>
side > 0 ? "▲" : side < 0 ? "▼" : "="
// price -> table row (row 1 = top of chart, row nRows = bottom)
f_row(float v, float lo, float rng) =>
(nRows - 1) - int(math.round((v - lo) / rng * (nRows - 1)))
// width of a column: candle column or thin gap column
f_w(int col) =>
col % 2 == 0 ? cellW : gapW
f_paint(table t, int col, int rA, int rB, color c) =>
for r = math.min(rA, rB) to math.max(rA, rB)
table.cell(t, col, r, "", width = cellW, height = cellH, bgcolor = c)
// One full-width section: header row, nRows chart rows, footer row (correlation). r0 = header row.
f_draw(table t, int r0, string name, array<float> o, array<float> h, array<float> l, array<float> c, float chg, float ref, string status, color statusCol, string corrTxt, color corrCol) =>
int half = int(nCols / 2)
for i = 0 to nCols - 1
for r = 1 to nRows
table.cell(t, i, r0 + r, "", width = f_w(i), height = cellH, bgcolor = bgCol)
// header: name, price, % change (left) and rule status (right)
color hc = na(chg) ? txtCol : chg >= 0 ? upCol : dnCol
string arrow = na(chg) ? "" : chg >= 0 ? "▲ " : "▼ "
string hdr = name + " " + str.tostring(f_last(c), "0.00") + " " + arrow + str.tostring(math.abs(nz(chg)), "0.00") + "%"
table.cell(t, 0, r0, hdr, width = f_w(0), text_color = hc, text_size = size.small, text_halign = text.align_left, bgcolor = bgCol)
table.merge_cells(t, 0, r0, half - 1, r0)
table.cell(t, half, r0, status, width = f_w(half), text_color = statusCol, text_size = size.tiny, text_halign = text.align_right, bgcolor = bgCol)
table.merge_cells(t, half, r0, nCols - 1, r0)
// footer: correlation with the chart symbol
table.cell(t, 0, r0 + nRows + 1, corrTxt, width = f_w(0), text_color = corrCol, text_size = size.tiny, text_halign = text.align_left, bgcolor = bgCol)
table.merge_cells(t, 0, r0 + nRows + 1, nCols - 1, r0 + nRows + 1)
int n = na(c) ? 0 : c.size()
if n > 1
float hi = h.max()
float lo = l.min()
if showRef and not na(ref)
hi := math.max(hi, ref)
lo := math.min(lo, ref)
float rng = math.max(hi - lo, 1e-10)
int base = r0 + 1
if showRef and not na(ref)
int rr = base + f_row(ref, lo, rng)
for i = 0 to nCols - 1
table.cell(t, i, rr, "", width = f_w(i), height = cellH, bgcolor = refCol)
int off = nBars - n
for i = 0 to n - 1
int col = 2 * (off + i)
if styleIn == "Candles"
color bc = c.get(i) >= o.get(i) ? upCol : dnCol
f_paint(t, col, base + f_row(h.get(i), lo, rng), base + f_row(l.get(i), lo, rng), color.new(bc, 60))
f_paint(t, col, base + f_row(math.max(o.get(i), c.get(i)), lo, rng), base + f_row(math.min(o.get(i), c.get(i)), lo, rng), bc)
else
float prev = i > 0 ? c.get(i - 1) : c.get(i)
color lc = c.get(i) >= prev ? upCol : dnCol
f_paint(t, col, base + f_row(prev, lo, rng), base + f_row(c.get(i), lo, rng), lc)
f_corrLine(float r) =>
"vs NQ: " + (na(r) ? "-" : str.tostring(r, "0.00")) + " " + f_corrTxt(r)
// ───────────────────────── Render ─────────────────────────
// Layout (rows): DXY section, divider, VIX section, bias line. Each section = header, chart, correlation.
var table t = na
if barstate.islast
if not na(t)
table.delete(t)
int rDiv = nRows + 2
int rVix = nRows + 3
int rBias = 2 * nRows + 5
t := table.new(f_pos(posIn), nCols, rBias + 1, bgcolor = bgCol, frame_color = divCol, frame_width = 1, border_width = 0)
chg1 = na(prev1D) ? na : (c1 - prev1D) / prev1D * 100
chg2 = na(prev2D) ? na : (c2 - prev2D) / prev2D * 100
st1 = stale1 ? "closed" : "open " + f_arrow(dxyVsOpen) + " EMA " + f_arrow(dxyVsEma)
st2 = stale2 ? "closed" : "prev " + f_arrow(vixVsPrev) + " EMA " + f_arrow(vixVsEma)
sc1 = stale1 ? txtCol : score1 > 0 ? upCol : score1 < 0 ? dnCol : txtCol
sc2 = stale2 ? txtCol : score2 > 0 ? upCol : score2 < 0 ? dnCol : txtCol
float r1 = na(a1C) or na(aNC) ? na : f_corr(a1T, a1C, aNT, aNC, corrLen)
float r2 = na(a2C) or na(aNC) ? na : f_corr(a2T, a2C, aNT, aNC, corrLen)
f_draw(t, 0, f_name(sym1), a1O, a1H, a1L, a1C, chg1, open1D, st1, sc1, f_corrLine(r1), f_corrCol(r1))
for i = 0 to nCols - 1
table.cell(t, i, rDiv, "", width = f_w(i), height = 0.15, bgcolor = divCol)
f_draw(t, rVix, f_name(sym2), a2O, a2H, a2L, a2C, chg2, prev2D, st2, sc2, f_corrLine(r2), f_corrCol(r2))
string scoreTxt = (score > 0 ? "+" : "") + str.tostring(score) + "/" + str.tostring(nRules)
table.cell(t, 0, rBias, "Bias: " + biasTxt + " (" + scoreTxt + ")" + divTxt, width = f_w(0), text_color = biasCol, text_size = size.tiny, text_halign = text.align_left, bgcolor = bgCol)
table.merge_cells(t, 0, rBias, nCols - 1, rBias)