本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("[blackcat] L2 Kondratieff Wave Macro Cycle", shorttitle="L2-K-Wave-Macro", overlay=true, max_lines_count=500, max_labels_count=500)
// ── Inputs ──────────────────────────────────────────────
grpPhase = "Phase Color Settings"
colSpring = input.color(color.new(color.teal, 85), "Spring / Recovery", group=grpPhase)
colSummer = input.color(color.new(color.green, 85), "Summer / Prosperity", group=grpPhase)
colAutumn = input.color(color.new(color.orange, 85), "Autumn / Recession", group=grpPhase)
colWinter = input.color(color.new(color.red, 85), "Winter / Depression", group=grpPhase)
grpViz = "Visualization"
showTransitions = input.bool(true, "Mark Historical Phase Transitions", group=grpViz)
showLastLabel = input.bool(true, "Show Current Phase Label", group=grpViz)
grpTable = "Status Table Settings"
showTable = input.bool(true, "Show Status Table", group=grpTable)
tablePosInput = input.string("top_right", "Table Position", options=["top_right","top_left","bottom_right","bottom_left"], group=grpTable)
// ── K-Wave Phase Definitions (Consensus Academic Timeline) ──
// Each phase entry: [startYear, endYear, phaseLabel, season, phaseColor]
// Phases based on widely-cited Kondratieff Wave chronologies.
// Current wave numbering follows the 4-wave consensus grid; adjust years
// inside the barstate.isfirst block if you follow a different school.
type Phase
int startYr
int endYr
string label
string season
color col
var array<Phase> phases = array.new<Phase>()
if barstate.isfirst
// Wave 1
array.push(phases, Phase.new(1780, 1810, "Spring (W1)", "Spring", colSpring))
array.push(phases, Phase.new(1810, 1840, "Summer (W1)", "Summer", colSummer))
array.push(phases, Phase.new(1840, 1860, "Autumn (W1)", "Autumn", colAutumn))
array.push(phases, Phase.new(1860, 1890, "Winter (W1)", "Winter", colWinter))
// Wave 2
array.push(phases, Phase.new(1890, 1910, "Spring (W2)", "Spring", colSpring))
array.push(phases, Phase.new(1910, 1930, "Summer (W2)", "Summer", colSummer))
array.push(phases, Phase.new(1930, 1950, "Autumn (W2)", "Autumn", colAutumn))
array.push(phases, Phase.new(1950, 1960, "Winter (W2)", "Winter", colWinter))
// Wave 3
array.push(phases, Phase.new(1960, 1970, "Spring (W3)", "Spring", colSpring))
array.push(phases, Phase.new(1970, 1980, "Summer (W3)", "Summer", colSummer))
array.push(phases, Phase.new(1980, 1990, "Autumn (W3)", "Autumn", colAutumn))
array.push(phases, Phase.new(1990, 2000, "Winter (W3)", "Winter", colWinter))
// Wave 4
array.push(phases, Phase.new(2000, 2010, "Spring (W4)", "Spring", colSpring))
array.push(phases, Phase.new(2010, 2020, "Summer (W4)", "Summer", colSummer))
array.push(phases, Phase.new(2020, 2030, "Autumn (W4)", "Autumn", colAutumn))
array.push(phases, Phase.new(2030, 2040, "Winter (W4)", "Winter", colWinter))
// Wave 5 (approximate, projected)
array.push(phases, Phase.new(2040, 2050, "Spring (W5)", "Spring", colSpring))
array.push(phases, Phase.new(2050, 2060, "Summer (W5)", "Summer", colSummer))
array.push(phases, Phase.new(2060, 2070, "Autumn (W5)", "Autumn", colAutumn))
array.push(phases, Phase.new(2070, 2080, "Winter (W5)", "Winter", colWinter))
// ── Determine current phase ─────────────────────────────
int currentYear = year(time)
var Phase currentPhase = na
int phaseIdx = -1
int lastIdx = array.size(phases) - 1
for i = 0 to lastIdx
Phase p = array.get(phases, i)
if currentYear >= p.startYr and currentYear < p.endYr
currentPhase := p
phaseIdx := i
break
// ── Phase change detection (edge, for markers + alert) ──
var int prevIdx = -2
bool phaseChanged = phaseIdx != prevIdx and phaseIdx >= 0
// ── Background coloring ─────────────────────────────────
bgcolor(not na(currentPhase) ? currentPhase.col : na, title="K-Wave Phase Background")
// ── Historical phase transition markers ─────────────────
if showTransitions and phaseChanged
bool isWaveStart = currentPhase.season == "Spring"
label.new(bar_index, low, (isWaveStart ? "◈ " : "· ") + currentPhase.label, style=label.style_label_up, color=color.new(currentPhase.col, isWaveStart ? 0 : 40), textcolor=color.white, size=isWaveStart ? size.small : size.tiny, yloc=yloc.belowbar)
// ── Current phase label (var-managed, redrawn on last bar) ──
var label phLbl = na
if showLastLabel and barstate.islast and not na(currentPhase)
if na(phLbl)
phLbl := label.new(bar_index, high, currentPhase.label, style=label.style_label_down, color=currentPhase.col, textcolor=color.white, size=size.small)
else
label.set_xy(phLbl, bar_index, high)
label.set_text(phLbl, currentPhase.label)
label.set_color(phLbl, currentPhase.col)
// ── Alert: phase regime change ──────────────────────────
alertcondition(phaseChanged, "K-Wave Phase Change", "Kondratieff phase changed to a new regime")
// ── Status Table ────────────────────────────────────────
var table statusTable = table.new(position.top_right, 2, 8, border_width=1, border_color=color.new(chart.fg_color, 55))
if showTable and barstate.islast
tablePosSel = switch tablePosInput
"top_left" => position.top_left
"bottom_right" => position.bottom_right
"bottom_left" => position.bottom_left
=> position.top_right
table.set_position(statusTable, tablePosSel)
if showTable and barstate.islast and not na(currentPhase)
int waveNum = math.floor(phaseIdx / 4) + 1
int phaseInWave = phaseIdx % 4 + 1
int phaseLen = currentPhase.endYr - currentPhase.startYr
int phaseProgress = math.round((currentYear - currentPhase.startYr) / phaseLen * 100)
int yrsToNext = math.max(currentPhase.endYr - currentYear, 0)
Phase nextPhase = phaseIdx + 1 < array.size(phases) ? array.get(phases, phaseIdx + 1) : Phase.new(na, na, "—", "—", color.gray)
int barBlocks = math.min(math.floor(phaseProgress / 10), 10)
string progBar = str.repeat("█", barBlocks) + str.repeat("▒", 10 - barBlocks)
string bias = switch currentPhase.season
"Spring" => "Growth / Early-cycle equities"
"Summer" => "Trend-following / Equities"
"Autumn" => "Defensive / Real assets, commodities"
"Winter" => "Preservation / Cash & fixed income"
=> "N/A"
bool tfOk = timeframe.period == "1Y" or timeframe.period == "12M" or timeframe.period == "3M"
table.cell(statusTable, 0, 0, "K-Wave Macro", bgcolor=chart.fg_color, text_color=chart.bg_color, text_size=size.normal)
table.cell(statusTable, 1, 0, "Regime", bgcolor=chart.fg_color, text_color=chart.bg_color, text_size=size.normal)
table.cell(statusTable, 0, 1, "Wave", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 1, "Wave " + str.tostring(waveNum) + " · Phase " + str.tostring(phaseInWave) + "/4", bgcolor=color.new(chart.fg_color, 93), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 0, 2, "Current Phase", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 2, currentPhase.label, bgcolor=color.new(currentPhase.col, 25), text_color=color.white, text_size=size.normal)
table.cell(statusTable, 0, 3, "Progress", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 3, progBar + " " + str.tostring(phaseProgress) + "%", bgcolor=color.new(chart.fg_color, 93), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 0, 4, "Phase Window", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 4, str.tostring(currentPhase.startYr) + " – " + str.tostring(currentPhase.endYr), bgcolor=color.new(chart.fg_color, 93), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 0, 5, "Next Phase", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 5, nextPhase.label + " in " + str.tostring(yrsToNext) + "y", bgcolor=color.new(chart.fg_color, 93), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 0, 6, "Macro Bias", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 6, bias, bgcolor=color.new(chart.fg_color, 93), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 0, 7, "Chart TF", bgcolor=color.new(chart.fg_color, 88), text_color=chart.fg_color, text_size=size.normal)
table.cell(statusTable, 1, 7, timeframe.period + (tfOk ? " ✓" : " → use 1Y / 3M"), bgcolor=color.new(chart.fg_color, 93), text_color=chart.fg_color, text_size=size.normal)
if showTable and barstate.islast and na(currentPhase)
table.clear(statusTable, 0, 0, 1, 7)
table.cell(statusTable, 0, 0, "K-Wave Macro", bgcolor=chart.fg_color, text_color=chart.bg_color, text_size=size.normal)
table.cell(statusTable, 1, 0, "Year " + str.tostring(currentYear) + " outside 1780–2080", bgcolor=color.new(color.red, 25), text_color=color.white, text_size=size.normal)
// ── Update edge memory (must stay last) ─────────────────
prevIdx := phaseIdx