本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("CTZ Fib Cycle Lows - DCL / WCL / YCL", shorttitle = "CTZ Fib Cycle Lows", overlay = true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 100)
// ═══════════════════════════════════════════════════════════════════
// CTZ FIB CYCLE LOWS
// Daily, Weekly and Yearly cycle lows timed with FIBONACCI instead of
// average days. For each cycle the next low is projected from:
// 1. Fibonacci bar counts from the cycle low (21 / 34 / 55 ...)
// 2. Fib ratios of the previous cycle length (0.618 / 1 / 1.618)
// 3. Fib ratios of the advance (decline time = 1 / 1.618 / 2.618 x advance)
// Where these land together = NEXT LOW WINDOW.
// Price target = Fib retracement of the cycle range, depth set by
// translation (golden-ratio thresholds). Failed cycle = 1.272–1.618 ext.
// ═══════════════════════════════════════════════════════════════════
// ═════════ Inputs ═════════
grp0 = "📉 Daily Cycle Low (DCL)"
show_dcl = input.bool(true, "Show DCL", group = grp0)
dcl_lb = input.int(5, "Pivot lookback", minval = 2, maxval = 30, group = grp0)
dcl_sp = input.int(13, "Min bars between lows", minval = 3, maxval = 100, group = grp0)
dcl_fs = input.string("21,34,55", "Fib counts for next low", group = grp0, tooltip = "Fibonacci bar counts from the cycle low, set for a Daily chart. Comma separated.")
dcl_col = input.color(color.new(#00e5a0, 0), "Colour", group = grp0)
grpW = "📉 Weekly Cycle Low (WCL)"
show_wcl = input.bool(true, "Show WCL", group = grpW)
wcl_lb = input.int(13, "Pivot lookback", minval = 3, maxval = 60, group = grpW)
wcl_sp = input.int(55, "Min bars between lows", minval = 10, maxval = 300, group = grpW)
wcl_fs = input.string("89,144,233", "Fib counts for next low", group = grpW)
wcl_col = input.color(color.new(#38bdf8, 0), "Colour", group = grpW)
grpY = "📉 Yearly Cycle Low (YCL)"
show_ycl = input.bool(true, "Show YCL", group = grpY)
ycl_lb = input.int(21, "Pivot lookback", minval = 5, maxval = 90, group = grpY)
ycl_sp = input.int(233, "Min bars between lows", minval = 50, maxval = 800, group = grpY)
ycl_fs = input.string("233,377,610", "Fib counts for next low", group = grpY)
ycl_col = input.color(color.new(#f59e0b, 0), "Colour", group = grpY)
grpF = "🌀 Fibonacci Timing"
useCnt = input.bool(true, "Fib counts from the cycle low", group = grpF)
useLen = input.bool(true, "Fib ratios of previous cycle length (0.618 / 1 / 1.618)", group = grpF)
useAdv = input.bool(true, "Fib ratios of the advance (1 / 1.618 / 2.618)", group = grpF)
tolPct = input.float(5.0, "Time tolerance (% of cycle length)", minval = 0.5, maxval = 25.0, step = 0.5, group = grpF)
minHits = input.int(2, "Min projections for a window", minval = 1, maxval = 10, group = grpF)
grpTF = "⏱ Timeframe Scaling"
auto_scale = input.bool(true, "Auto-scale lengths to chart timeframe", group = grpTF, tooltip = "All lengths are set for a Daily chart. When on, they keep the same real-time length on any timeframe.")
grpD = "🗂 Display"
show_lbl = input.bool(true, "Cycle low labels", group = grpD)
show_fail = input.bool(true, "Fail level lines", group = grpD)
show_proj = input.bool(true, "Next low projection boxes", group = grpD)
show_dash = input.bool(true, "Dashboard", group = grpD)
dash_pos = input.string("Top Right", "Dashboard position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpD)
lbl_size = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = grpD)
fail_col = input.color(color.new(#ff3333, 0), "Failure colour", group = grpD)
// ═════════ Timeframe scaling (same as Cycle Lows v4) ═════════
float dpw = syminfo.type == "crypto" ? 7.0 : 5.0
float daySecs = syminfo.type == "crypto" or syminfo.type == "forex" ? 86400.0 : 23400.0
float tfFactor = not auto_scale ? 1.0 : timeframe.isdaily ? 1.0 / timeframe.multiplier : timeframe.isweekly ? 1.0 / (timeframe.multiplier * dpw) : timeframe.ismonthly ? 1.0 / (timeframe.multiplier * dpw * 4.345) : daySecs / timeframe.in_seconds()
sc(int v, int mn) => math.max(mn, int(math.round(v * tfFactor)))
f_parse(string s) =>
array<int> out = array.new<int>()
array<string> parts = str.split(s, ",")
for p in parts
float v = str.tonumber(str.replace_all(p, " ", ""))
if not na(v) and v > 0
out.push(math.max(1, int(math.round(v * tfFactor))))
out
int dLb = sc(dcl_lb, 2)
int dSp = sc(dcl_sp, 3)
int wLb = sc(wcl_lb, 2)
int wSp = sc(wcl_sp, 4)
int yLb = sc(ycl_lb, 2)
int ySp = sc(ycl_sp, 6)
// ═════════ Fibonacci constants ═════════
var array<int> FIBALL = array.from(3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597)
var array<float> LR = array.from(0.618, 1.0, 1.618)
var array<float> AR = array.from(1.0, 1.618, 2.618)
// ═════════ Helpers ═════════
szL = lbl_size == "Tiny" ? size.tiny : lbl_size == "Normal" ? size.normal : size.small
f_px(float v) => str.tostring(v, format.mintick)
f_date(int barsAhead) => str.format_time(time + barsAhead * timeframe.in_seconds() * 1000, "dd MMM yy", syminfo.timezone)
f_pos(string p) =>
switch p
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
nearFib(int v) =>
int best = FIBALL.get(0)
for f in FIBALL
if math.abs(f - v) < math.abs(best - v)
best := f
best
// golden-ratio translation thresholds
transOf(float rel) => rel < 0.382 ? "LTR" : rel < 0.618 ? "MTR" : "RTR"
// ═════════ Core series ═════════
float adr = ta.sma(high - low, 14)
float dPiv = ta.pivotlow(low, dLb, dLb)
float wPiv = ta.pivotlow(low, wLb, wLb)
float yPiv = ta.pivotlow(low, yLb, yLb)
float dHH = ta.highest(high, dLb + 1)
int dHHb = bar_index + ta.highestbars(high, dLb + 1)
float wHH = ta.highest(high, wLb + 1)
int wHHb = bar_index + ta.highestbars(high, wLb + 1)
float yHH = ta.highest(high, yLb + 1)
int yHHb = bar_index + ta.highestbars(high, yLb + 1)
// ═════════ Cycle state ═════════
type Fc
string name
color col
bool show
int lb
int sp
array<int> fibs
array<label> lbls
int lastBar = na
float lastLow = na
int prevBar = na
int prevLen = na
int prevLen2 = na
float hiSince = na
int hiSinceBar = na
string trans = "N/A"
bool broke = false
bool failNow = false
bool newLow = false
bool relocated = false
int wS = na
int wE = na
int wPk = na
int hits = 0
float zLo = na
float zHi = na
string liveT = "N/A"
bool overdue = false
method step(Fc c, float piv, float hh, int hhBar) =>
c.newLow := false
c.relocated := false
c.failNow := false
if not na(piv)
int pb = bar_index - c.lb
if na(c.lastBar) or pb - c.lastBar >= c.sp
// new cycle low – close out the completed cycle
if not na(c.lastBar)
int len = pb - c.lastBar
if not na(c.hiSinceBar) and len > 0
c.trans := transOf(float(c.hiSinceBar - c.lastBar) / len)
c.prevLen2 := c.prevLen
c.prevLen := len
c.prevBar := c.lastBar
c.lastBar := pb
c.lastLow := piv
c.broke := false
c.hiSince := hh
c.hiSinceBar := hhBar
c.newLow := true
else if piv < c.lastLow
// deeper low inside min spacing = same trough, move the cycle low
c.lastBar := pb
c.lastLow := piv
if not na(c.prevBar)
c.prevLen := pb - c.prevBar
c.hiSince := hh
c.hiSinceBar := hhBar
c.relocated := true
if not c.newLow and not c.relocated and not na(c.lastBar)
if na(c.hiSince) or high > c.hiSince
c.hiSince := high
c.hiSinceBar := bar_index
// failure: close below the cycle low once relocation is no longer possible
if not na(c.lastBar) and not c.broke and not c.newLow and not c.relocated
if bar_index - c.lastBar >= c.sp + c.lb and close < c.lastLow
c.broke := true
c.failNow := true
c
// Fibonacci projection of the next low (time window + price zone)
method project(Fc c) =>
c.wS := na
c.wE := na
c.wPk := na
c.hits := 0
c.zLo := na
c.zHi := na
c.overdue := false
c.liveT := c.trans
if not na(c.lastBar)
int refLen = not na(c.prevLen) ? c.prevLen : c.fibs.size() > 0 ? c.fibs.get(math.min(1, c.fibs.size() - 1)) : c.sp * 2
int tol = math.max(1, int(math.round(refLen * tolPct / 100)))
array<int> proj = array.new<int>()
if useCnt
for f in c.fibs
proj.push(c.lastBar + f)
if useLen and not na(c.prevLen)
for r in LR
proj.push(c.lastBar + int(math.round(c.prevLen * r)))
if not na(c.prevLen2)
proj.push(c.lastBar + c.prevLen2)
// decline time as a Fib multiple of the advance (only once the high is a few bars old)
if useAdv and not na(c.hiSinceBar) and c.hiSinceBar > c.lastBar and bar_index - c.hiSinceBar >= c.lb
int up = c.hiSinceBar - c.lastBar
for r in AR
proj.push(c.hiSinceBar + int(math.round(up * r)))
int maxT = c.lastBar
for t in proj
maxT := math.max(maxT, t)
int span = math.min(3000, maxT + tol - bar_index)
int minIdx = c.lastBar + int(math.round(c.sp * 0.5)) - bar_index
if span >= 0
array<int> hist = array.new<int>(span + 1, 0)
for t in proj
for x = t - tol to t + tol
int idx = x - bar_index
if idx >= 0 and idx <= span and idx >= minIdx
hist.set(idx, hist.get(idx) + 1)
// strongest window (ties -> earliest)
int runS = na
int pk = 0
int pkI = na
int bestS = na
int bestE = na
int bestP = 0
int bestM = na
for idx = 0 to span
int h = hist.get(idx)
if h >= minHits
if na(runS)
runS := idx
pk := h
pkI := idx
else if h > pk
pk := h
pkI := idx
if not na(runS) and (h < minHits or idx == span)
int e = h < minHits ? idx - 1 : idx
if pk > bestP
bestS := runS
bestE := e
bestP := pk
bestM := pkI
runS := na
if bestP > 0
c.wS := bar_index + bestS
c.wE := bar_index + bestE
c.wPk := bar_index + bestM
c.hits := bestP
else
c.overdue := true
if na(c.wS) and proj.size() > 0 and maxT < bar_index
c.overdue := true
// price zone: Fib retracement of the cycle range, depth by translation
if not na(c.hiSince) and c.hiSince > c.lastLow
float rng = c.hiSince - c.lastLow
string t = c.trans
if not na(c.wPk) and c.wPk > c.lastBar
float expLen = c.wPk - c.lastBar
if bar_index - c.lastBar >= expLen * 0.382
t := transOf((c.hiSinceBar - c.lastBar) / expLen)
c.liveT := t
if c.broke
c.zHi := c.hiSince - rng * 1.272
c.zLo := c.hiSince - rng * 1.618
else
float dS = t == "RTR" ? 0.382 : t == "LTR" ? 0.786 : 0.5
float dD = t == "RTR" ? 0.618 : t == "LTR" ? 1.0 : 0.786
c.zHi := c.hiSince - rng * dS
c.zLo := c.hiSince - rng * dD
c
handleLabels(Fc c, float mult) =>
if show_lbl and c.show and (c.newLow or c.relocated)
if c.relocated and c.lbls.size() > 0
label.delete(c.lbls.pop())
string lenTxt = na(c.prevLen) ? "FIRST" : str.tostring(c.prevLen) + " bars ≈ Fib " + str.tostring(nearFib(c.prevLen))
string tTxt = c.trans != "N/A" ? "\n" + c.trans : ""
color bg = c.trans == "LTR" ? color.new(#f87171, 10) : c.trans == "RTR" ? color.new(#a78bfa, 10) : color.new(c.col, 10)
c.lbls.push(label.new(c.lastBar, c.lastLow - nz(adr) * mult, c.name + "\n" + lenTxt + tTxt, style = label.style_label_up, color = bg, textcolor = color.white, size = szL))
if c.lbls.size() > 3
label.delete(c.lbls.shift())
if c.failNow and c.show
label.new(bar_index, low - nz(adr) * (mult + 1.0), "⛔ " + c.name + " FAIL\nBroke " + f_px(c.lastLow), style = label.style_label_up, color = color.new(fail_col, 10), textcolor = color.white, size = szL)
true
// ═════════ Run cycles ═════════
var Fc dcl = Fc.new(name = "DCL", col = dcl_col, show = show_dcl, lb = dLb, sp = dSp, fibs = f_parse(dcl_fs), lbls = array.new<label>())
var Fc wcl = Fc.new(name = "WCL", col = wcl_col, show = show_wcl, lb = wLb, sp = wSp, fibs = f_parse(wcl_fs), lbls = array.new<label>())
var Fc ycl = Fc.new(name = "YCL", col = ycl_col, show = show_ycl, lb = yLb, sp = ySp, fibs = f_parse(ycl_fs), lbls = array.new<label>())
dcl.step(dPiv, dHH, dHHb)
wcl.step(wPiv, wHH, wHHb)
ycl.step(yPiv, yHH, yHHb)
handleLabels(dcl, 1.5)
handleLabels(wcl, 2.5)
handleLabels(ycl, 3.5)
// ═════════ Projections + drawings (last bar) ═════════
var array<box> gB = array.new<box>()
var array<line> gL = array.new<line>()
var array<label> gLb = array.new<label>()
drawCycle(Fc c, array<box> bxs, array<line> lns, array<label> lbs) =>
if c.show and not na(c.lastBar)
if show_fail
lns.push(line.new(c.lastBar, c.lastLow, bar_index + 20, c.lastLow, color = color.new(c.col, 40), style = line.style_dotted, width = 1))
lbs.push(label.new(bar_index + 20, c.lastLow, c.name + " · FAIL LVL " + f_px(c.lastLow), style = label.style_label_left,
color = c.broke ? color.new(fail_col, 10) : color.new(c.col, 30), textcolor = color.white, size = size.tiny))
if show_proj and not na(c.wS) and not na(c.zHi) and c.wE - bar_index <= 490
float mid = (c.zLo + c.zHi) / 2
int ahead = c.wPk - bar_index
bxs.push(box.new(c.wS, c.zHi, c.wE + 1, c.zLo, border_color = color.new(c.col, 20), border_width = 1, bgcolor = color.new(c.col, 82)))
lns.push(line.new(c.wPk, c.zHi, c.wPk, c.zLo, color = color.new(c.col, 10), style = line.style_dashed, width = 1))
lns.push(line.new(c.wS, mid, c.wE + 1, mid, color = c.col, width = 2))
lbs.push(label.new(c.wPk, c.zHi, "NEXT " + c.name + " (FIB)\nx" + str.tostring(c.hits) + " · " + (ahead > 0 ? "~" + str.tostring(ahead) + " bars" : "IN WINDOW") +
"\n" + f_date(math.max(ahead, 0)) + "\nTarget " + f_px(mid) + "\n" + f_px(c.zLo) + " - " + f_px(c.zHi),
style = label.style_label_down, color = color.new(c.col, 15), textcolor = color.white, size = szL))
true
if barstate.islast
for b in gB
box.delete(b)
for l in gL
line.delete(l)
for lb in gLb
label.delete(lb)
gB.clear()
gL.clear()
gLb.clear()
dcl.project()
wcl.project()
ycl.project()
drawCycle(dcl, gB, gL, gLb)
drawCycle(wcl, gB, gL, gLb)
drawCycle(ycl, gB, gL, gLb)
// ═════════ Windows / confluence ═════════
inWin(Fc c) => c.show and not na(c.wS) and bar_index >= c.wS and bar_index <= c.wE
inBox(Fc c) => inWin(c) and not na(c.zHi) and low <= c.zHi and high >= c.zLo
overlap(Fc a, Fc b) => a.show and b.show and not na(a.wS) and not na(b.wS) and a.wS <= b.wE and b.wS <= a.wE
bool dwConf = overlap(dcl, wcl)
bool wyConf = overlap(wcl, ycl)
// ═════════ Dashboard ═════════
var table dash = table.new(f_pos(dash_pos), 2, 24, bgcolor = color.new(color.black, 15), border_width = 1, border_color = color.new(color.white, 75))
dashSection(table d, int r, Fc c, string title) =>
color lc = color.new(color.white, 25)
bool act = c.show and not na(c.lastBar)
int bs = act ? bar_index - c.lastBar : na
int nextF = na
if act
for f in FIBALL
if na(nextF) and f > bs
nextF := f
bool inW = inWin(c)
string chip = not act ? "waiting…" : c.broke ? "⛔ FAILED" : inW ? "⚡ IN WINDOW" : c.overdue ? "⚠ OVERDUE" : "tracking"
color chC = c.broke ? fail_col : inW ? color.yellow : c.overdue ? color.orange : color.new(color.white, 40)
color tc = c.liveT == "LTR" ? color.new(#f87171, 0) : c.liveT == "RTR" ? color.new(#a78bfa, 0) : c.liveT == "MTR" ? color.new(#fbbf24, 0) : color.new(color.white, 40)
table.cell(d, 0, r, title, text_color = c.col, bgcolor = color.new(c.col, 88), text_size = size.small, text_halign = text.align_left)
table.cell(d, 1, r, chip, text_color = chC, bgcolor = color.new(c.col, 88), text_size = size.small)
table.cell(d, 0, r + 1, "Cycle day", text_color = lc, text_size = size.small)
table.cell(d, 1, r + 1, act ? str.tostring(bs) + (na(nextF) ? "" : " → Fib " + str.tostring(nextF) + " in " + str.tostring(nextF - bs)) : "–", text_color = color.white, text_size = size.small)
table.cell(d, 0, r + 2, "Last cycle", text_color = lc, text_size = size.small)
table.cell(d, 1, r + 2, act and not na(c.prevLen) ? str.tostring(c.prevLen) + " bars ≈ Fib " + str.tostring(nearFib(c.prevLen)) + " · " + c.trans : "–", text_color = color.white, text_size = size.small)
table.cell(d, 0, r + 3, "Translation", text_color = lc, text_size = size.small)
table.cell(d, 1, r + 3, act ? c.liveT + " (now)" : "–", text_color = tc, text_size = size.small)
table.cell(d, 0, r + 4, "Fail level", text_color = lc, text_size = size.small)
table.cell(d, 1, r + 4, act ? f_px(c.lastLow) : "–", text_color = c.broke ? fail_col : color.new(color.white, 30), text_size = size.small)
table.cell(d, 0, r + 5, "Next low (Fib)", text_color = lc, text_size = size.small)
table.cell(d, 1, r + 5, not na(c.wPk) ? (c.wPk > bar_index ? "~" + str.tostring(c.wPk - bar_index) + " bars · " : "now · ") + f_date(math.max(c.wPk - bar_index, 0)) + " · x" + str.tostring(c.hits) : act ? (c.overdue ? "overdue" : "no window") : "–", text_color = c.col, text_size = size.small)
table.cell(d, 0, r + 6, "Target zone", text_color = lc, text_size = size.small)
table.cell(d, 1, r + 6, not na(c.zHi) ? f_px(c.zLo) + " - " + f_px(c.zHi) + (c.broke ? " (ext)" : "") : "–", text_color = c.col, text_size = size.small)
true
if show_dash and barstate.islast
table.cell(dash, 0, 0, "CTZ FIB CYCLE LOWS", text_color = color.white, bgcolor = color.new(#1e293b, 0), text_size = size.normal)
table.cell(dash, 1, 0, syminfo.ticker + " · " + timeframe.period, text_color = color.new(color.white, 30), bgcolor = color.new(#1e293b, 0), text_size = size.small)
dashSection(dash, 1, dcl, "DAILY DCL")
dashSection(dash, 8, wcl, "WEEKLY WCL")
dashSection(dash, 15, ycl, "YEARLY YCL")
string confTxt = dwConf and wyConf ? "DCL + WCL + YCL" : dwConf ? "DCL + WCL" : wyConf ? "WCL + YCL" : "none"
table.cell(dash, 0, 22, "Fib window confluence", text_color = color.new(color.white, 25), text_size = size.small)
table.cell(dash, 1, 22, confTxt, text_color = dwConf or wyConf ? color.new(#f59e0b, 0) : color.new(color.white, 50), text_size = size.small)
table.cell(dash, 0, 23, "TF scale", text_color = color.new(color.white, 25), text_size = size.small)
table.cell(dash, 1, 23, auto_scale ? "x" + str.tostring(tfFactor, "#.###") : "OFF", text_color = color.new(color.white, 40), text_size = size.small)
// ═════════ Alerts ═════════
bool dW = inWin(dcl)
bool wW = inWin(wcl)
bool yW = inWin(ycl)
bool dB = inBox(dcl)
bool wB = inBox(wcl)
bool yB = inBox(ycl)
alertcondition(dcl.newLow, "New DCL", "New Daily Cycle Low — {{ticker}} @ {{close}}")
alertcondition(wcl.newLow, "New WCL", "New Weekly Cycle Low — {{ticker}} @ {{close}}")
alertcondition(ycl.newLow, "New YCL", "New Yearly Cycle Low — {{ticker}} @ {{close}}")
alertcondition(dcl.failNow, "DCL failure", "⛔ DCL FAILED — {{ticker}} @ {{close}}")
alertcondition(wcl.failNow, "WCL failure", "⛔ WCL FAILED — {{ticker}} @ {{close}}")
alertcondition(ycl.failNow, "YCL failure", "⛔ YCL FAILED — {{ticker}} @ {{close}}")
alertcondition(dW and not dW[1], "Entered DCL Fib window", "Entered DCL Fibonacci time window — {{ticker}}")
alertcondition(wW and not wW[1], "Entered WCL Fib window", "Entered WCL Fibonacci time window — {{ticker}}")
alertcondition(yW and not yW[1], "Entered YCL Fib window", "Entered YCL Fibonacci time window — {{ticker}}")
alertcondition(dB and not dB[1], "DCL Fib target hit", "Time + price in DCL Fib target — {{ticker}} @ {{close}}")
alertcondition(wB and not wB[1], "WCL Fib target hit", "Time + price in WCL Fib target — {{ticker}} @ {{close}}")
alertcondition(yB and not yB[1], "YCL Fib target hit", "Time + price in YCL Fib target — {{ticker}} @ {{close}}")
alertcondition(dwConf and not dwConf[1], "DCL + WCL Fib confluence", "DCL and WCL Fibonacci windows overlap — {{ticker}}")
alertcondition(wyConf and not wyConf[1], "WCL + YCL Fib confluence", "WCL and YCL Fibonacci windows overlap — {{ticker}}")