本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kripto Okulu
//@version=6
indicator("US Recessions (NBER) + Early Warning", shorttitle = "US Recessions", overlay = true, max_labels_count = 100)
// ─────────────────────────────────────────────────────────────
// INPUTS
// ─────────────────────────────────────────────────────────────
grpR = "NBER Recessions"
showRec = input.bool(true, "Shade recession periods", group = grpR)
recCol = input.color(color.new(color.red, 82), "Recession color", group = grpR)
showLbl = input.bool(true, "Start labels", group = grpR)
lblCol = input.color(color.new(color.red, 20), "Label color", group = grpR)
grpS = "Sahm Rule (real-time unemployment signal)"
showSahm = input.bool(false, "Shade when Sahm Rule triggers", group = grpS)
urSym = input.symbol("FRED:UNRATE", "Unemployment rate symbol", group = grpS)
sahmThr = input.float(0.5, "Threshold (pp)", step = 0.05, group = grpS)
sahmCol = input.color(color.new(color.orange, 85), "Sahm color", group = grpS)
grpY = "Yield Curve (10Y - 2Y)"
showYC = input.bool(false, "Shade inverted yield curve", group = grpY)
y10Sym = input.symbol("TVC:US10Y", "10-year yield", group = grpY)
y2Sym = input.symbol("TVC:US02Y", "2-year yield", group = grpY)
ycCol = input.color(color.new(color.purple, 88), "Inversion color", group = grpY)
grpT = "Status Table"
showTbl = input.bool(true, "Show table", group = grpT)
tblPos = input.string("Top right", "Position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = grpT)
// ─────────────────────────────────────────────────────────────
// NBER RECESSION DATES (YYYYMM)
// FRED USREC convention: month after the peak → trough month (inclusive)
// Last reviewed: November 2026 — no NBER recession declared after 2020
// ─────────────────────────────────────────────────────────────
var array<int> recStart = array.from(192002, 192306, 192611, 192909, 193706, 194503, 194812, 195308, 195709, 196005, 197001, 197312, 198002, 198108, 199008, 200104, 200801, 202003)
var array<int> recEnd = array.from(192107, 192407, 192711, 193303, 193806, 194510, 194910, 195405, 195804, 196102, 197011, 197503, 198007, 198211, 199103, 200111, 200906, 202004)
var array<string> recName = array.from(
"Depression of 1920-21", "1923-24 Recession", "1926-27 Recession", "Great Depression",
"Recession of 1937-38", "Post-WWII Contraction", "Recession of 1949", "Post-Korean War Recession",
"Eisenhower Recession", "Recession of 1960-61", "Recession of 1969-70", "Oil Crisis Recession",
"Volcker I", "Volcker II", "Gulf War / S&L Recession", "Dot-com Bust",
"Great Recession", "COVID-19 Recession")
f_ymStr(int ym) =>
int y = int(math.floor(ym / 100))
int m = ym % 100
(m < 10 ? "0" : "") + str.tostring(m) + "/" + str.tostring(y)
f_months(int s, int e) =>
int sy = int(math.floor(s / 100))
int ey = int(math.floor(e / 100))
(ey * 12 + e % 100) - (sy * 12 + s % 100) + 1
f_inRec(int ym) =>
bool r = false
int idx = -1
for i = 0 to recStart.size() - 1
if ym >= recStart.get(i) and ym <= recEnd.get(i)
r := true
idx := i
break
[r, idx]
int ymNow = year(time) * 100 + month(time)
[inRec, recIdx] = f_inRec(ymNow)
// Most recent recession (for the table)
int lastIdx = recStart.size() - 1
// ─────────────────────────────────────────────────────────────
// SAHM RULE: 3-month avg unemployment − lowest 3-month avg of the prior 12 months
// ─────────────────────────────────────────────────────────────
f_sahm() =>
a = ta.sma(close, 3)
a - ta.lowest(a[1], 12)
float sahm = request.security(urSym, "M", f_sahm(), ignore_invalid_symbol = true)
float urNow = request.security(urSym, "M", close, ignore_invalid_symbol = true)
bool sahmOn = not na(sahm) and sahm >= sahmThr
// ─────────────────────────────────────────────────────────────
// YIELD CURVE
// ─────────────────────────────────────────────────────────────
float y10 = request.security(y10Sym, timeframe.period, close, ignore_invalid_symbol = true)
float y2 = request.security(y2Sym, timeframe.period, close, ignore_invalid_symbol = true)
float yc = y10 - y2
bool ycInv = not na(yc) and yc < 0
// ─────────────────────────────────────────────────────────────
// PLOTTING
// ─────────────────────────────────────────────────────────────
bgcolor(showYC and ycInv ? ycCol : na, title = "Inverted yield curve")
bgcolor(showSahm and sahmOn ? sahmCol : na, title = "Sahm Rule triggered")
bgcolor(showRec and inRec ? recCol : na, title = "NBER recession")
if showLbl and inRec and not inRec[1] and recIdx >= 0
int s = recStart.get(recIdx)
int e = recEnd.get(recIdx)
string txt = recName.get(recIdx) + "\n" + f_ymStr(s) + " – " + f_ymStr(e) + " (" + str.tostring(f_months(s, e)) + " mo)"
label.new(bar_index, high, txt, yloc = yloc.abovebar, style = label.style_label_down, color = lblCol, textcolor = color.white, size = size.small)
// ─────────────────────────────────────────────────────────────
// STATUS TABLE
// ─────────────────────────────────────────────────────────────
pos = tblPos == "Top right" ? position.top_right : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Top left" ? position.top_left : position.bottom_left
var table tb = table.new(pos, 2, 5, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.gray, 60))
if showTbl and barstate.islast
color txtC = color.white
table.cell(tb, 0, 0, "US Recession Monitor", text_color = txtC, text_size = size.small)
table.merge_cells(tb, 0, 0, 1, 0)
table.cell(tb, 0, 1, "NBER status", text_color = txtC, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, 1, inRec ? "RECESSION" : "Expansion", text_color = inRec ? color.red : color.lime, text_size = size.small)
table.cell(tb, 0, 2, "Last recession", text_color = txtC, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, 2, recName.get(lastIdx) + " (" + f_ymStr(recStart.get(lastIdx)) + "–" + f_ymStr(recEnd.get(lastIdx)) + ")", text_color = txtC, text_size = size.small)
table.cell(tb, 0, 3, "Sahm / Unemployment", text_color = txtC, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, 3, na(sahm) ? "no data" : str.tostring(sahm, "0.00") + " pp | " + str.tostring(urNow, "0.0") + "%", text_color = sahmOn ? color.orange : color.lime, text_size = size.small)
table.cell(tb, 0, 4, "10Y − 2Y", text_color = txtC, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, 4, na(yc) ? "no data" : str.tostring(yc, "0.00") + (ycInv ? " (INVERTED)" : " (normal)"), text_color = ycInv ? color.purple : color.lime, text_size = size.small)
// ─────────────────────────────────────────────────────────────
// ALERTS
// ─────────────────────────────────────────────────────────────
alertcondition(sahmOn and not sahmOn[1], "Sahm Rule triggered", "US: Sahm Rule crossed the recession threshold")
alertcondition(ycInv and not ycInv[1], "Yield curve inverted", "US: 10Y-2Y spread turned negative")
alertcondition(not ycInv and ycInv[1], "Yield curve un-inverted", "US: 10Y-2Y spread turned positive again (historically a late-cycle warning)")