The Staircase

TheSimplifier7 · study · 352 行 · 点赞 129 · TradingView 原页

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

Pine Script

//@version=6
// THE STAIRCASE 1.0, draft 1, 5 Nov 2026. THE SIMPLIFIER.
// The big swings of the day, months apart, drawn as a staircase: each multi month high held as a step until the next
// one, each low the same, the space between shaded by which way the stairs run, the path through the swings in gold.
// A picture of the structure so far. It calls no side, prints no odds and draws nothing ahead of price.
//
// How it reads the day: on the chart symbol's own daily bars, closed days only. A swing high is the highest high of
// the days each side (30 by default, about six weeks each way), a swing low the lowest low. Two highs in a row take
// the lowest low between them as the low they skipped, two lows the highest high between them, so highs and lows
// alternate. A swing is known only once its days to the right have closed, and from that day on it never moves.
// The steps are drawn from the day the swing was made, so on history a step appears to start before it was known;
// in Bar Replay you see each step arrive when it was known.
//
// Built by the lab, not compiled. Checked statically, and its engine checked in Python against the plate's pivots on
// his broker's gold, silver, platinum and palladium day bars (TWIN_THE_STAIRCASE_05NOV.py): every day identical.
indicator("The Staircase", shorttitle = "The Staircase", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 20, max_bars_back = 150)
 
// ---------------------------------------------------------------------------------------------------------- settings
int   L_IN    = input.int(30, "Swing size, days each side", minval = 5, maxval = 60, tooltip = "A swing high is the highest high of this many closed days on each side of it, a swing low the lowest low. 30 days is about six weeks each way: the multi month swings. A swing is known this many days after it is made.")
int   KEEP_IN = input.int(40, "Swings to draw, at most", minval = 6, maxval = 80, tooltip = "The newest swings drawn. 40 is about six years of gold's day at 30 days.")
bool  SHADE_IN = input.bool(true, "Shade between the steps", tooltip = "Blue while both the highs and the lows step up, strongest at the high step. Red while both step down, strongest at the low step. Faint while they disagree.")
bool  PATH_IN  = input.bool(true, "The path through the swings")
bool  FORM_IN  = input.bool(true, "The swing still forming, dashed", tooltip = "From the last known swing to the furthest price reached since, closed days only. It moves until the next swing is known. Drawn, like the path, when the chart shows at least the swing size in days.")
bool  NAMES_IN = input.bool(true, "Name the last two steps", tooltip = "RIDGE on the last known swing high, FLOOR on the last known swing low; ABOVE or BELOW at the edge when the step is off the screen. The price is in the tag's tooltip.")
color UP_IN   = input.color(#8DB0F5, "Higher step")
color DN_IN   = input.color(#E2574C, "Lower step")
color NEU_IN  = input.color(#B9C2D3, "Not yet compared")
color PATH_C  = input.color(#E0A93B, "The path")
 
int LAYERS = 6
 
// ---------------------------------------------------------------------------------------------------------- the day
// Runs once a day in the daily context. Hands back the swing count, the newest swing's kind, the last 8 swings and the
// forming end, each as of the day before ([1]), so a chart never sees a day that has not closed.
f_day(int L) =>
    var array<int>   si = array.new<int>()      // bar_index of each swing, in the day's own count
    var array<int>   st = array.new<int>()      // its time
    var array<float> sp = array.new<float>()    // its price
    var array<int>   sk = array.new<int>()      // 1 a high, -1 a low
    var int   loI = na
    var float loP = na
    var int   loT = na
    var int   hiI = na
    var float hiP = na
    var int   hiT = na
    var int   fT  = na
    var float fP  = na
    float hh = ta.highest(high, 2 * L + 1)
    float ll = ta.lowest(low, 2 * L + 1)
    // the day that joins the running extremes: L + 1 days back, if it came after the last swing
    int n0 = array.size(si)
    if bar_index > L and n0 > 0
        int x = bar_index - L - 1
        if x > array.get(si, n0 - 1)
            if na(loP) or low[L + 1] < loP
                loI := x
                loP := low[L + 1]
                loT := time[L + 1]
            if na(hiP) or high[L + 1] > hiP
                hiI := x
                hiP := high[L + 1]
                hiT := time[L + 1]
    // the candidate L days back: a high first, then a low
    bool changed = false
    if bar_index >= 2 * L
        int i  = bar_index - L
        int ti = time[L]
        for side = 0 to 1
            int   k    = side == 0 ? 1 : -1
            bool  cand = side == 0 ? high[L] == hh : low[L] == ll
            float p    = side == 0 ? high[L] : low[L]
            if cand
                bool push = true
                int  n    = array.size(si)
                if n > 0 and array.get(sk, n - 1) == k
                    int j = array.get(si, n - 1)
                    if i > j + 1
                        // the swing it skipped
                        if k == 1
                            array.push(si, loI)
                            array.push(st, loT)
                            array.push(sp, loP)
                            array.push(sk, -1)
                        else
                            array.push(si, hiI)
                            array.push(st, hiT)
                            array.push(sp, hiP)
                            array.push(sk, 1)
                    else
                        push := false
                        float pj = array.get(sp, n - 1)
                        if (k == 1 and p > pj) or (k == -1 and p < pj)
                            array.set(si, n - 1, i)
                            array.set(st, n - 1, ti)
                            array.set(sp, n - 1, p)
                            changed := true
                if push
                    int n2 = array.size(si)
                    if n2 > 0 and array.get(si, n2 - 1) == i
                        push := false
                if push
                    array.push(si, i)
                    array.push(st, ti)
                    array.push(sp, p)
                    array.push(sk, k)
                    changed := true
    int nS = array.size(si)
    if changed
        // a new last swing: the running extremes start again, the forming end is read over the days since it
        loI := na
        loP := na
        loT := na
        hiI := na
        hiP := na
        hiT := na
        fT  := na
        fP  := na
        int kl = array.get(sk, nS - 1)
        for q = 0 to L - 1
            int b = L - 1 - q
            if kl == 1
                if na(fP) or low[b] < fP
                    fP := low[b]
                    fT := time[b]
            else
                if na(fP) or high[b] > fP
                    fP := high[b]
                    fT := time[b]
    else if nS > 0
        if array.get(sk, nS - 1) == 1
            if na(fP) or low < fP
                fP := low
                fT := time
        else
            if na(fP) or high > fP
                fP := high
                fT := time
    int lk = nS > 0 ? array.get(sk, nS - 1) : 0
    int   t1 = nS >= 1 ? array.get(st, nS - 1) : na
    int   t2 = nS >= 2 ? array.get(st, nS - 2) : na
    int   t3 = nS >= 3 ? array.get(st, nS - 3) : na
    int   t4 = nS >= 4 ? array.get(st, nS - 4) : na
    int   t5 = nS >= 5 ? array.get(st, nS - 5) : na
    int   t6 = nS >= 6 ? array.get(st, nS - 6) : na
    int   t7 = nS >= 7 ? array.get(st, nS - 7) : na
    int   t8 = nS >= 8 ? array.get(st, nS - 8) : na
    float p1 = nS >= 1 ? array.get(sp, nS - 1) : na
    float p2 = nS >= 2 ? array.get(sp, nS - 2) : na
    float p3 = nS >= 3 ? array.get(sp, nS - 3) : na
    float p4 = nS >= 4 ? array.get(sp, nS - 4) : na
    float p5 = nS >= 5 ? array.get(sp, nS - 5) : na
    float p6 = nS >= 6 ? array.get(sp, nS - 6) : na
    float p7 = nS >= 7 ? array.get(sp, nS - 7) : na
    float p8 = nS >= 8 ? array.get(sp, nS - 8) : na
    int   fTo = fT
    float fPo = fP
    [nS[1], lk[1], t1[1], t2[1], t3[1], t4[1], t5[1], t6[1], t7[1], t8[1], p1[1], p2[1], p3[1], p4[1], p5[1], p6[1], p7[1], p8[1], fTo[1], fPo[1]]
 
bool ABOVE_DAY = timeframe.in_seconds() > 86400
string TF = ABOVE_DAY ? timeframe.period : "D"
 
// Closed days only: every value above is offset by one day inside f_day, so lookahead on hands each chart bar the
// state as of the last closed day, on history and live alike. Nothing repaints. Above the day nothing is drawn.
[dN, dK, d1, d2, d3, d4, d5, d6, d7, d8, q1, q2, q3, q4, q5, q6, q7, q8, dFT, dFP] = request.security(syminfo.tickerid, TF, f_day(L_IN), lookahead = barmerge.lookahead_on)
 
// ---------------------------------------------------------------------------------------------------------- the chart's list
var array<int>   cT = array.new<int>()
var array<float> cP = array.new<float>()
var array<int>   cK = array.new<int>()
var int seen = 0
bool stepUp = false
bool stepDn = false
if not ABOVE_DAY and not na(dN) and dN > 0
    if dN > seen
        int nw = math.min(dN - seen, 8)
        array<int>   tt = array.from(d1, d2, d3, d4, d5, d6, d7, d8)
        array<float> pp = array.from(q1, q2, q3, q4, q5, q6, q7, q8)
        bool fresh = seen > 0
        for q = 0 to nw - 1
            int d = nw - 1 - q
            int kd = d % 2 == 0 ? dK : -dK
            array.push(cT, array.get(tt, d))
            array.push(cP, array.get(pp, d))
            array.push(cK, kd)
            int m = array.size(cP)
            if fresh and m >= 3
                float pNew = array.get(cP, m - 1)
                float pOld = array.get(cP, m - 3)
                if pNew > pOld
                    stepUp := true
                if pNew < pOld
                    stepDn := true
        seen := dN
    else if array.size(cT) > 0
        int m = array.size(cT)
        if array.get(cT, m - 1) != d1 or array.get(cP, m - 1) != q1
            array.set(cT, m - 1, d1)
            array.set(cP, m - 1, q1)
 
// the candles on screen set the height: nothing is drawn far past them
var float visHi = na
var float visLo = na
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
    visHi := na(visHi) ? high : math.max(visHi, high)
    visLo := na(visLo) ? low : math.min(visLo, low)
 
// ---------------------------------------------------------------------------------------------------------- drawing
f_clip(int xA, float ya, int xB, float yb, float lo, float hi) =>
    float s0 = 0.0
    float s1 = 1.0
    float dy = yb - ya
    bool ok = true
    if dy != 0
        float sA = (lo - ya) / dy
        float sB = (hi - ya) / dy
        s0 := math.max(s0, math.min(sA, sB))
        s1 := math.min(s1, math.max(sA, sB))
        ok := s0 <= s1
    else
        ok := ya >= lo and ya <= hi
    int xA2 = xA + int(math.round((xB - xA) * s0))
    int xB2 = xA + int(math.round((xB - xA) * s1))
    [xA2, ya + dy * s0, xB2, ya + dy * s1, ok]
 
f_step_col(float now, float before) =>
    na(before) ? NEU_IN : now > before ? UP_IN : now < before ? DN_IN : NEU_IN
 
var array<line>  LN = array.new<line>()
var array<box>   BX = array.new<box>()
var array<label> LB = array.new<label>()
 
if barstate.islast
    for x in LN
        line.delete(x)
    array.clear(LN)
    for x in BX
        box.delete(x)
    array.clear(BX)
    for x in LB
        label.delete(x)
    array.clear(LB)
    int n = array.size(cT)
    if ABOVE_DAY
        array.push(LB, label.new(time, close, "The Staircase reads the day: open it on a daily chart or lower", xloc = xloc.bar_time, style = label.style_label_left, color = color.new(NEU_IN, 100), textcolor = NEU_IN, size = size.small))
    else if n >= 2 and not na(visHi)
        float rng    = math.max(visHi - visLo, syminfo.mintick)
        float bandHi = visHi + rng / 8
        float bandLo = visLo - rng / 8
        int   left   = chart.left_visible_bar_time
        int   nowT   = time
        // closer in than one swing's width, the path and the forming swing would meet the candles at a day's open,
        // not at the bar that made them: they wait for a view of L days or more
        bool  wide   = chart.right_visible_bar_time - left >= L_IN * 86400000
        // the first swing to draw: two before the first one on screen, and no more than the setting allows
        int q0 = n - 1
        while q0 > 0 and array.get(cT, q0) > left
            q0 -= 1
        q0 := math.max(0, q0 - 2)
        q0 := math.max(q0, n - KEEP_IN)
        // the shade: between consecutive swings, the last high step over the last low step
        if SHADE_IN
            for q = math.max(q0, 1) to n - 1
                int   tA = array.get(cT, q)
                int   tB = q + 1 < n ? array.get(cT, q + 1) : nowT
                if tB >= left and tB > tA
                    bool  qHigh = array.get(cK, q) == 1
                    float ridge = qHigh ? array.get(cP, q) : array.get(cP, q - 1)
                    float flr = qHigh ? array.get(cP, q - 1) : array.get(cP, q)
                    float rPrev = na
                    float fPrev = na
                    if qHigh
                        rPrev := q >= 2 ? array.get(cP, q - 2) : na
                        fPrev := q >= 3 ? array.get(cP, q - 3) : na
                    else
                        fPrev := q >= 2 ? array.get(cP, q - 2) : na
                        rPrev := q >= 3 ? array.get(cP, q - 3) : na
                    bool rUp = not na(rPrev) and ridge > rPrev
                    bool rDn = not na(rPrev) and ridge < rPrev
                    bool fUp = not na(fPrev) and flr > fPrev
                    bool fDn = not na(fPrev) and flr < fPrev
                    float top = math.max(ridge, flr)
                    float bot = math.min(ridge, flr)
                    // zoomed in, only a slice of the step is on screen: the shade quietens so the candles stay clear
                    float kS  = math.max(0.25, math.min(1.0, rng / math.max(top - bot, syminfo.mintick)))
                    if (rUp and fUp) or (rDn and fDn)
                        for m = 0 to LAYERS - 1
                            float f  = (m + 0.5) / LAYERS
                            float a  = kS * (rUp ? 0.03 + 0.42 * math.pow(f, 1.6) : 0.03 + 0.42 * math.pow(1 - f, 1.6))
                            float yA = math.max(bot + (top - bot) * m / LAYERS, bandLo)
                            float yB = math.min(bot + (top - bot) * (m + 1) / LAYERS, bandHi)
                            if yB > yA
                                color c = color.new(rUp ? UP_IN : DN_IN, 100 - a * 100)
                                array.push(BX, box.new(left = tA, top = yB, right = tB, bottom = yA, xloc = xloc.bar_time, bgcolor = c, border_color = color.new(c, 100), border_width = 0))
                    else
                        float yA = math.max(bot, bandLo)
                        float yB = math.min(top, bandHi)
                        if yB > yA
                            color c = color.new(NEU_IN, 100 - 7 * kS)
                            array.push(BX, box.new(left = tA, top = yB, right = tB, bottom = yA, xloc = xloc.bar_time, bgcolor = c, border_color = color.new(c, 100), border_width = 0))
        // the steps: each swing held until the next of its kind; the riser where the next one starts
        for q = q0 to n - 1
            int   tA  = array.get(cT, q)
            float v   = array.get(cP, q)
            int   tB  = q + 2 < n ? array.get(cT, q + 2) : nowT
            float vb  = q >= 2 ? array.get(cP, q - 2) : na
            color col = f_step_col(v, vb)
            if tB >= left and v >= bandLo and v <= bandHi
                array.push(LN, line.new(tA, v, tB, v, xloc = xloc.bar_time, color = color.new(col, 85), width = 6))
                array.push(LN, line.new(tA, v, tB, v, xloc = xloc.bar_time, color = col, width = 2))
            if q + 2 < n and tB >= left
                float vn = array.get(cP, q + 2)
                float yA = math.max(math.min(v, vn), bandLo)
                float yB = math.min(math.max(v, vn), bandHi)
                if yB > yA
                    array.push(LN, line.new(tB, yA, tB, yB, xloc = xloc.bar_time, color = color.new(f_step_col(vn, v), 45), width = 1))
        // the path through the swings, in gold
        if PATH_IN and wide
            for q = q0 to n - 2
                int tA = array.get(cT, q)
                int tB = array.get(cT, q + 1)
                if tB >= left
                    [xa, ya, xb, yb, ok] = f_clip(tA, array.get(cP, q), tB, array.get(cP, q + 1), bandLo, bandHi)
                    if ok
                        array.push(LN, line.new(xa, ya, xb, yb, xloc = xloc.bar_time, color = color.new(PATH_C, 82), width = 5))
                        array.push(LN, line.new(xa, ya, xb, yb, xloc = xloc.bar_time, color = PATH_C, width = 2))
        // the swing still forming
        if FORM_IN and wide and not na(dFT) and not na(dFP) and dFT > array.get(cT, n - 1)
            [xa, ya, xb, yb, ok] = f_clip(array.get(cT, n - 1), array.get(cP, n - 1), dFT, dFP, bandLo, bandHi)
            if ok
                array.push(LN, line.new(xa, ya, xb, yb, xloc = xloc.bar_time, color = color.new(PATH_C, 40), style = line.style_dashed, width = 1))
        // the names of the last two steps
        if NAMES_IN
            for q = n - 2 to n - 1
                float v   = array.get(cP, q)
                bool  isH = array.get(cK, q) == 1
                float vb  = q >= 2 ? array.get(cP, q - 2) : na
                color col = f_step_col(v, vb)
                string nm = isH ? "RIDGE" : "FLOOR"
                string tip = nm + " " + str.tostring(v, format.mintick) + ", made " + str.format_time(array.get(cT, q), "d MMM yyyy", syminfo.timezone)
                float y = v
                if v > bandHi
                    nm := nm + " ABOVE"
                    y := bandHi
                else if v < bandLo
                    nm := nm + " BELOW"
                    y := bandLo
                array.push(LB, label.new(nowT, y, nm, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(col, 100), textcolor = col, size = size.small, tooltip = tip))
 
// ---------------------------------------------------------------------------------------------------------- alerts
alertcondition(stepUp, "The Staircase: a higher step", "The Staircase on {{ticker}}: a higher step is known (a swing above the last of its kind).")
alertcondition(stepDn, "The Staircase: a lower step", "The Staircase on {{ticker}}: a lower step is known (a swing below the last of its kind).")
alertcondition(stepUp or stepDn, "The Staircase: a new step", "The Staircase on {{ticker}}: a new step is known.")

← 返回列表