CTZ Daily Fib Time Zones

CryptoTradingZone · study · 206 行 · 点赞 20 · TradingView 原页

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

Pine Script

//@version=6
indicator("CTZ Daily Fib Time Zones", "CTZ Fib Time Zones", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ═══════════════════════════════════════════════════════════════════
// CTZ DAILY FIB TIME ZONES
// Like TradingView's Fib Time Zone, but anchored automatically on the
// daily cycle.  0 and 1 are the two anchor points; zones are drawn at
// 2, 3, 5, 8, 13 ... times that span.
//   Unit "Low → next low"  : 0 = previous cycle low, 1 = last cycle low,
//                             2 = next cycle low if the cycle repeats.
//   Unit "Low → high"      : 0 = last cycle low, 1 = cycle high since.
// Anchors move forward automatically when a new cycle low is confirmed.
// ═══════════════════════════════════════════════════════════════════
 
// ═════════ Inputs ═════════
grpC = "Cycle"
deg        = input.string("Daily (DCL)", "Cycle", options = ["Daily (DCL)", "Weekly (WCL)", "Custom"], group = grpC)
cLb        = input.int(5, "Custom pivot lookback", minval = 2, maxval = 60, group = grpC)
cSp        = input.int(13, "Custom min bars between lows", minval = 3, maxval = 400, group = grpC)
auto_scale = input.bool(true, "Auto-scale to chart timeframe", group = grpC, tooltip = "Lengths are set for a Daily chart and scaled to keep the same real time on other timeframes.")
 
grpU = "Unit (0 → 1)"
unitMode = input.string("Low → next low (last cycle)", "Unit", options = ["Low → next low (last cycle)", "Low → high (current advance)"], group = grpU)
 
grpZ = "Zones"
zs        = input.string("0,1,2,3,5,8,13", "Fib multiples", group = grpZ)
showRatio = input.bool(true, "Add ratio lines", group = grpZ)
rs        = input.string("1.382,1.618,2.618", "Ratios", group = grpZ)
 
grpD = "Display"
cMain    = input.color(color.new(#38bdf8, 0), "Zone lines", group = grpD)
cAnchor  = input.color(color.new(#94a3b8, 0), "Anchor lines (0 and 1)", group = grpD)
cNext    = input.color(color.new(#f59e0b, 0), "Next zone", group = grpD)
cRatio   = input.color(color.new(#a855f7, 0), "Ratio lines", group = grpD)
showLows = input.bool(true, "Mark cycle lows", group = grpD)
showDash = input.bool(true, "Dashboard", group = grpD)
dashPos  = input.string("Bottom Right", "Dashboard position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpD)
 
// ═════════ Scaling (same as CTZ 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()
 
int baseLb = deg == "Daily (DCL)" ? 5 : deg == "Weekly (WCL)" ? 13 : cLb
int baseSp = deg == "Daily (DCL)" ? 13 : deg == "Weekly (WCL)" ? 55 : cSp
int lbS    = math.max(2, int(math.round(baseLb * tfFactor)))
int spS    = math.max(3, int(math.round(baseSp * tfFactor)))
 
// ═════════ Helpers ═════════
int DAY = 86400000
 
f_nums(string s) =>
    array<float> out = array.new<float>()
    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(v)
    out
 
f_date(int t) => str.format_time(t, "dd MMM yyyy", "UTC")
f_num(float v) => v == math.round(v) ? str.tostring(int(v)) : str.tostring(v)
f_pos(string p) =>
    switch p
        "Top Right"   => position.top_right
        "Top Left"    => position.top_left
        "Bottom Left" => position.bottom_left
        => position.bottom_right
 
// ═════════ Cycle low detection ═════════
float pl   = ta.pivotlow(low, lbS, lbS)
float hh   = ta.highest(high, lbS + 1)
int   hhOf = ta.highestbars(high, lbS + 1)
 
var array<int>   lT = array.new<int>()     // cycle low time
var array<int>   lB = array.new<int>()     // cycle low bar
var array<float> lP = array.new<float>()   // cycle low price
var float hiP = na
var int   hiT = na
bool newLow = false
 
if not na(pl)
    int pb = bar_index - lbS
    int pt = time[lbS]
    int n0 = lB.size()
    if n0 == 0 or pb - lB.get(n0 - 1) >= spS
        lT.push(pt)
        lB.push(pb)
        lP.push(pl)
        if lB.size() > 50
            lT.shift()
            lB.shift()
            lP.shift()
        newLow := true
    else if pl < lP.get(n0 - 1)
        // deeper low inside min spacing = same trough, move it
        lT.set(n0 - 1, pt)
        lB.set(n0 - 1, pb)
        lP.set(n0 - 1, pl)
        newLow := true
    if newLow
        hiP := hh
        hiT := time[math.abs(hhOf)]
 
if not newLow and lT.size() > 0
    if na(hiP) or high > hiP
        hiP := high
        hiT := time
 
if showLows and newLow
    label.new(lB.get(lB.size() - 1), lP.get(lP.size() - 1), "▲", style = label.style_none, textcolor = cAnchor, size = size.small, yloc = yloc.belowbar)
 
// ═════════ Zones (last bar) ═════════
float yTop = ta.highest(high, 200)
float yBot = ta.lowest(low, 200)
 
var int nxtT = na
var array<line>  gL  = array.new<line>()
var array<label> gLb = array.new<label>()
var table dash = table.new(f_pos(dashPos), 2, 9, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.white, 80))
 
if barstate.islast
    for x in gL
        line.delete(x)
    for x in gLb
        label.delete(x)
    gL.clear()
    gLb.clear()
    nxtT := na
 
    bool lowLow = unitMode == "Low → next low (last cycle)"
    int  n      = lT.size()
    int  aT     = na
    int  bT     = na
    if lowLow and n >= 2
        aT := lT.get(n - 2)
        bT := lT.get(n - 1)
    else if not lowLow and n >= 1 and not na(hiT) and hiT > lT.get(n - 1)
        aT := lT.get(n - 1)
        bT := hiT
 
    if not na(aT) and bT > aT
        int   unit  = bT - aT
        float rng   = yTop - yBot
        int   nextK = na
        int   next2 = na
 
        array<float> zArr = f_nums(zs)
        for k in zArr
            int   t     = aT + int(math.round(unit * k))
            bool  isAnc = k == 0 or k == 1
            bool  isNxt = lowLow and k == 2
            color cc    = isAnc ? cAnchor : isNxt ? cNext : cMain
            gL.push(line.new(t, yBot, t, yTop, xloc = xloc.bar_time, extend = extend.both, color = color.new(cc, isAnc ? 50 : 10), width = isNxt ? 2 : 1))
            gLb.push(label.new(t, yBot - rng * 0.02, f_num(k) + (t > time ? "\n" + f_date(t) : ""), xloc = xloc.bar_time,
                 style = label.style_label_up, color = color.new(cc, isAnc ? 60 : 20), textcolor = color.white, size = size.small))
            if t > time
                if na(nxtT) or t < nxtT
                    next2 := nxtT
                    nxtT  := t
                    nextK := int(k)
                else if na(next2) or t < next2
                    next2 := t
 
        if showRatio
            array<float> rArr = f_nums(rs)
            for r in rArr
                int t = aT + int(math.round(unit * r))
                gL.push(line.new(t, yBot, t, yTop, xloc = xloc.bar_time, extend = extend.both, color = color.new(cRatio, 30), style = line.style_dashed, width = 1))
                gLb.push(label.new(t, yTop, f_num(r) + "\n" + f_date(t), xloc = xloc.bar_time, style = label.style_label_down, color = color.new(cRatio, 25), textcolor = color.white, size = size.tiny))
                if t > time and (na(next2) or t < next2) and (na(nxtT) or t > nxtT)
                    next2 := t
 
        // dashed span line from 0 to 1, like the TradingView tool
        float yA = lowLow ? lP.get(n - 2) : lP.get(n - 1)
        float yB = lowLow ? lP.get(n - 1) : hiP
        gL.push(line.new(aT, yA, bT, yB, xloc = xloc.bar_time, color = color.new(cAnchor, 30), style = line.style_dashed, width = 1))
 
        if showDash
            color lc = color.new(color.white, 25)
            table.cell(dash, 0, 0, "CTZ FIB TIME ZONES", text_color = color.white, bgcolor = color.new(#1e293b, 0), text_size = size.small)
            table.cell(dash, 1, 0, deg + " · " + timeframe.period, text_color = color.new(color.white, 30), bgcolor = color.new(#1e293b, 0), text_size = size.small)
            table.cell(dash, 0, 1, "Unit", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 1, lowLow ? "Low → next low" : "Low → high", text_color = color.white, text_size = size.small)
            table.cell(dash, 0, 2, "0", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 2, f_date(aT) + " · " + str.tostring(yA, format.mintick), text_color = cAnchor, text_size = size.small)
            table.cell(dash, 0, 3, "1", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 3, f_date(bT) + " · " + str.tostring(yB, format.mintick), text_color = cAnchor, text_size = size.small)
            table.cell(dash, 0, 4, "Unit length", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 4, str.tostring(math.round(unit / DAY)) + " days", text_color = color.white, text_size = size.small)
            table.cell(dash, 0, 5, "Days since 1", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 5, str.tostring(math.round((time - bT) / DAY)), text_color = color.white, text_size = size.small)
            table.cell(dash, 0, 6, "Next zone", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 6, na(nxtT) ? "–" : (na(nextK) ? "" : "Zone " + str.tostring(nextK) + " · ") + f_date(nxtT) + " · " + str.tostring(math.round((nxtT - time) / DAY)) + "d", text_color = cNext, text_size = size.small)
            table.cell(dash, 0, 7, "Following", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 7, na(next2) ? "–" : f_date(next2) + " · " + str.tostring(math.round((next2 - time) / DAY)) + "d", text_color = cMain, text_size = size.small)
            table.cell(dash, 0, 8, "Last cycle low", text_color = lc, text_size = size.small)
            table.cell(dash, 1, 8, f_date(lT.get(n - 1)) + " · " + str.tostring(lP.get(n - 1), format.mintick), text_color = color.white, text_size = size.small)
    else if showDash
        table.cell(dash, 0, 0, "CTZ FIB TIME ZONES", text_color = color.white, bgcolor = color.new(#1e293b, 0), text_size = size.small)
        table.cell(dash, 1, 0, "waiting for cycle lows", text_color = color.orange, text_size = size.small)
 
// ═════════ Alerts ═════════
bool zoneHit = not na(nxtT) and time >= nxtT and time[1] < nxtT
alertcondition(zoneHit, "Fib time zone reached", "A Fibonacci time zone has been reached — {{ticker}}")
alertcondition(newLow, "New cycle low (zones re-anchored)", "New cycle low confirmed, Fib time zones re-anchored — {{ticker}}")

← 返回列表