RCI 8 Lines [Saikix]

Saikix · study · 497 行 · 点赞 16 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// RCI 8 Lines [Saikix]
// © Saikix
 
//@version=6
indicator("RCI 8 Lines [Saikix]", shorttitle="Saikix-RCI8", overlay=false, format=format.price, precision=0, max_labels_count=500, behind_chart=false)
 
G_BASIC = "Basic settings"
linesBy = input.string("Lengths", "Lines", options=["Lengths", "Timeframes"], group=G_BASIC, display=display.none,
     tooltip="Lengths: RCI lines with different lengths on one timeframe.\nTimeframes: one RCI length on different timeframes.\nShort lines are blue and long lines are orange.")
IS_LEN  = linesBy == "Lengths"
nS = input.int(4, "Number of lines", minval=1, maxval=6, inline="cnt", group=G_BASIC, display=display.none)
nL = input.int(4, "", minval=1, maxval=6, inline="cnt", group=G_BASIC, display=display.none,
     tooltip="Short lines (left) and long lines (right), 1 to 6 each.")
 
s1 = input.int(9,  "Short lengths 1–2", minval=3, maxval=200, inline="s12", group=G_BASIC, display=display.none, active=IS_LEN)
s2 = input.int(14, "", minval=3, maxval=200, inline="s12", group=G_BASIC, display=display.none, active=IS_LEN and nS >= 2)
s3 = input.int(18, "Short lengths 3–4", minval=3, maxval=200, inline="s34", group=G_BASIC, display=display.none, active=IS_LEN and nS >= 3)
s4 = input.int(26, "", minval=3, maxval=200, inline="s34", group=G_BASIC, display=display.none, active=IS_LEN and nS >= 4)
s5 = input.int(11, "Short lengths 5–6", minval=3, maxval=200, inline="s56", group=G_BASIC, display=display.none, active=IS_LEN and nS >= 5)
s6 = input.int(22, "", minval=3, maxval=200, inline="s56", group=G_BASIC, display=display.none, active=IS_LEN and nS >= 6)
l1 = input.int(33, "Long lengths 1–2", minval=3, maxval=200, inline="l12", group=G_BASIC, display=display.none, active=IS_LEN)
l2 = input.int(42, "", minval=3, maxval=200, inline="l12", group=G_BASIC, display=display.none, active=IS_LEN and nL >= 2)
l3 = input.int(52, "Long lengths 3–4", minval=3, maxval=200, inline="l34", group=G_BASIC, display=display.none, active=IS_LEN and nL >= 3)
l4 = input.int(75, "", minval=3, maxval=200, inline="l34", group=G_BASIC, display=display.none, active=IS_LEN and nL >= 4)
l5 = input.int(62, "Long lengths 5–6", minval=3, maxval=200, inline="l56", group=G_BASIC, display=display.none, active=IS_LEN and nL >= 5)
l6 = input.int(90, "", minval=3, maxval=200, inline="l56", group=G_BASIC, display=display.none, active=IS_LEN and nL >= 6,
     tooltip="Any order is fine: the lines are sorted by length for the colors and the signals.")
tfIn = input.timeframe("", "Timeframe", group=G_BASIC, display=display.none, active=IS_LEN,
     tooltip="Timeframe of all lengths. Empty = chart timeframe. A higher timeframe RCI is built from its completed bars and the current price, so past bars show what it was at that time. A timeframe lower than the chart uses the chart timeframe.")
lenT = input.int(9, "Length", minval=3, maxval=200, group=G_BASIC, display=display.none, active=not IS_LEN,
     tooltip="RCI length used on all timeframes.")
t1 = input.timeframe("1",   "Short timeframes 1–2", inline="t12", group=G_BASIC, display=display.none, active=not IS_LEN)
t2 = input.timeframe("5",   "", inline="t12", group=G_BASIC, display=display.none, active=not IS_LEN and nS >= 2)
t3 = input.timeframe("15",  "Short timeframes 3–4", inline="t34", group=G_BASIC, display=display.none, active=not IS_LEN and nS >= 3)
t4 = input.timeframe("30",  "", inline="t34", group=G_BASIC, display=display.none, active=not IS_LEN and nS >= 4)
t5 = input.timeframe("3",   "Short timeframes 5–6", inline="t56", group=G_BASIC, display=display.none, active=not IS_LEN and nS >= 5)
t6 = input.timeframe("10",  "", inline="t56", group=G_BASIC, display=display.none, active=not IS_LEN and nS >= 6)
u1 = input.timeframe("60",  "Long timeframes 1–2", inline="u12", group=G_BASIC, display=display.none, active=not IS_LEN)
u2 = input.timeframe("240", "", inline="u12", group=G_BASIC, display=display.none, active=not IS_LEN and nL >= 2)
u3 = input.timeframe("D",   "Long timeframes 3–4", inline="u34", group=G_BASIC, display=display.none, active=not IS_LEN and nL >= 3)
u4 = input.timeframe("W",   "", inline="u34", group=G_BASIC, display=display.none, active=not IS_LEN and nL >= 4)
u5 = input.timeframe("120", "Long timeframes 5–6", inline="u56", group=G_BASIC, display=display.none, active=not IS_LEN and nL >= 5)
u6 = input.timeframe("M",   "", inline="u56", group=G_BASIC, display=display.none, active=not IS_LEN and nL >= 6,
     tooltip="Higher than the chart: built from that timeframe's completed bars and the current price, so past bars show what it was at that time.\nLower than the chart: not drawn as a line (it changes many times within one chart bar). Its current value is in the summary.")
lvl  = input.int(80, "Levels (±)", minval=1, maxval=99, group=G_BASIC, display=display.none,
     tooltip="Upper and lower levels, for example +80 and −80.")
 
G_DISP = "Chart display"
showTable = input.bool(false, "Show summary", group=G_DISP, display=display.none,
     tooltip="Top right of the price chart: how many lines are on the same side of 0, and the current value of each line. With Timeframes, timeframes lower than the chart are not drawn as lines, and their values are shown here.")
 
G_SIG = "Signals"
showPO = input.bool(true, "Show perfect order", group=G_SIG, display=display.none, active=IS_LEN,
     tooltip="PO on the price chart when all lines line up in order of length: the shortest on top and the longest at the bottom (bullish), or the reverse (bearish). Checked at bar close. Lengths only.")
poGap  = input.int(50, "Bars between perfect orders", minval=0, maxval=1000, group=G_SIG, display=display.none, active=IS_LEN,
     tooltip="A perfect order in the same direction is not shown again within this many bars of the last one. 0 = every time.")
showGC = input.bool(true, "Show golden and death crosses", group=G_SIG, display=display.none, active=IS_LEN,
     tooltip="GC when all short lines move above all long lines, DC when they all move below. Only when the direction changes from the last cross. Checked at bar close. Lengths only.")
 
G_STYLE = "Colors and text"
shortDark  = input.color(color.new(#0064FF, 30), "Short colors", inline="sc", group=G_STYLE, display=display.none)
shortLight = input.color(color.new(#00DCFF, 30), "", inline="sc", group=G_STYLE, display=display.none,
     tooltip="Shortest and longest of the short lines. The lines in between get the colors in between.")
longDark   = input.color(color.new(#FF5000, 30), "Long colors", inline="lc", group=G_STYLE, display=display.none)
longLight  = input.color(color.new(#FFC800, 30), "", inline="lc", group=G_STYLE, display=display.none,
     tooltip="Shortest and longest of the long lines. The lines in between get the colors in between.")
upCol   = input.color(#089981, "Signal colors", inline="sm", group=G_STYLE, display=display.none)
dnCol   = input.color(#F23645, "", inline="sm", group=G_STYLE, display=display.none)
normCol = input.color(#787B86, "", inline="sm", group=G_STYLE, display=display.none,
     tooltip="Up, down and mixed. Used for the signals and the summary.")
lineW   = input.int(2, "Line width", minval=1, maxval=5, group=G_STYLE, display=display.none)
txtSize = input.int(13, "Text size", minval=8, maxval=30, group=G_STYLE, display=display.none)
tblPos  = input.string("Top right", "Summary position", options=["Top right", "Top left", "Bottom right", "Bottom left"], group=G_STYLE, display=display.none)
 
G_ALERT = "Alerts"
alSide  = input.bool(true, "All lines on one side of 0", group=G_ALERT, display=display.none,
     tooltip="Create the alert with \"Any alert() function call\". Sent at bar close.\nAll lines move above 0, or all move below 0.")
alLevel = input.bool(true, "All lines beyond the levels", group=G_ALERT, display=display.none,
     tooltip="All lines move above the upper level, or all move below the lower level.")
alPO    = input.bool(true, "Perfect order", group=G_ALERT, display=display.none, active=IS_LEN,
     tooltip="Bullish or bearish perfect order, as shown on the chart.")
alGC    = input.bool(true, "Golden and death crosses", group=G_ALERT, display=display.none, active=IS_LEN,
     tooltip="Golden cross or death cross, as shown on the chart.")
 
N_ALL = nS + nL
 
CHART_SEC = timeframe.in_seconds(timeframe.period)
tfSec(simple string tf) => tf == "" ? CHART_SEC : timeframe.in_seconds(tf)
tfLabel(simple string tf) =>
    int sec = tfSec(tf)
    sec < 3600 ? str.tostring(math.round(sec / 60.0), "#") + "m" : sec < 86400 ? str.tostring(math.round(sec / 3600.0), "#") + "h" : sec < 604800 ? "D" : sec < 2419200 ? "W" : "M"
 
packOf(array<float> buf, int n) =>
    int m = n - 1
    int sz = buf.size()
    array<float> pk = na
    if m >= 1 and sz >= m
        array<float> vals = buf.slice(sz - m, sz)
        array<int> idx = vals.sort_indices(order.ascending)
        array<float> q = array.new<float>(m, 0.0)
        int i = 0
        while i < m
            float vi = vals.get(idx.get(i))
            int j = i
            while j + 1 < m and vals.get(idx.get(j + 1)) == vi
                j += 1
            float avg = (i + j) / 2.0
            for k = i to j
                q.set(idx.get(k), avg)
            i := j + 1
        pk := array.new<float>(3 * m + 4, 0.0)
        int bPT = 2 + m
        int bPQ = 3 + 2 * m
        float stq = 0.0
        float sqq = 0.0
        for s = 0 to m - 1
            int o = idx.get(s)
            float qo = q.get(o)
            pk.set(2 + s, vals.get(o))
            pk.set(bPT + s + 1, pk.get(bPT + s) + o)
            pk.set(bPQ + s + 1, pk.get(bPQ + s) + qo)
            stq += o * qo
            sqq += qo * qo
        pk.set(0, stq)
        pk.set(1, sqq)
    pk
 
rciAt(array<float> pk, int n, float x) =>
    float r = na
    if not na(pk) and not na(x)
        int m = n - 1
        int bPT = 2 + m
        int bPQ = 3 + 2 * m
 
        int a = 0
        int b = m
        while a < b
            int c = int((a + b) / 2)
            if pk.get(2 + c) < x
                a := c + 1
            else
                b := c
        int lo = a
        b := m
        while a < b
            int c = int((a + b) / 2)
            if pk.get(2 + c) <= x
                a := c + 1
            else
                b := c
        int hi = a
        float sTgt = pk.get(bPT + m) - pk.get(bPT + hi)
        float sTeq = pk.get(bPT + hi) - pk.get(bPT + lo)
        float sQgt = pk.get(bPQ + m) - pk.get(bPQ + hi)
        float sQeq = pk.get(bPQ + hi) - pk.get(bPQ + lo)
        int   cGt  = m - hi
        int   cEq  = hi - lo
        float rx   = lo + cEq / 2.0
 
        float sTR  = pk.get(0) + sTgt + 0.5 * sTeq + m * rx
        float sRR  = pk.get(1) + 2 * sQgt + cGt + sQeq + 0.25 * cEq + rx * rx
        float mean = m / 2.0
        float cov  = sTR / n - mean * mean
        float varR = sRR / n - mean * mean
        float varT = (n * n - 1) / 12.0
        r := varR <= 1e-12 ? 0.0 : 100 * cov / math.sqrt(varT * varR)
    r
 
htfPack(simple int n) =>
    var array<float> buf = array.new<float>()
    array<float> pk = packOf(buf, n)
    buf.push(close)
    if buf.size() > n - 1
        buf.shift()
    pk
 
htfPacks(simple int a1, simple int a2, simple int a3, simple int a4, simple int a5, simple int a6, simple int b1, simple int b2, simple int b3, simple int b4, simple int b5, simple int b6) =>
    var array<float> buf = array.new<float>()
    int mx = math.max(a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, b5, b6) - 1
    array<float> p1 = packOf(buf, a1)
    array<float> p2 = packOf(buf, a2)
    array<float> p3 = packOf(buf, a3)
    array<float> p4 = packOf(buf, a4)
    array<float> p5 = packOf(buf, a5)
    array<float> p6 = packOf(buf, a6)
    array<float> q1 = packOf(buf, b1)
    array<float> q2 = packOf(buf, b2)
    array<float> q3 = packOf(buf, b3)
    array<float> q4 = packOf(buf, b4)
    array<float> q5 = packOf(buf, b5)
    array<float> q6 = packOf(buf, b6)
    buf.push(close)
    if buf.size() > mx
        buf.shift()
    [p1, p2, p3, p4, p5, p6, q1, q2, q3, q4, q5, q6]
 
float cT = ta.rci(close, lenT)
 
rciOnTf(simple string tf, simple int n) =>
    int s = tfSec(tf)
    float r = na
    if s == CHART_SEC
        r := cT
    else if s > CHART_SEC
        array<float> pk = request.security(syminfo.tickerid, tf, htfPack(n), lookahead=barmerge.lookahead_on)
        r := rciAt(pk, n, close)
    else
        r := request.security(syminfo.tickerid, tf, ta.rci(close, n))
    r
 
string tfL   = tfIn == "" or timeframe.in_seconds(tfIn) <= CHART_SEC ? timeframe.period : tfIn
bool   isHtf = tfL != timeframe.period
 
bool aS1 = true
bool aS2 = nS >= 2
bool aS3 = nS >= 3
bool aS4 = nS >= 4
bool aS5 = nS >= 5
bool aS6 = nS >= 6
bool aL1 = true
bool aL2 = nL >= 2
bool aL3 = nL >= 3
bool aL4 = nL >= 4
bool aL5 = nL >= 5
bool aL6 = nL >= 6
 
float rS1 = na
float rS2 = na
float rS3 = na
float rS4 = na
float rS5 = na
float rS6 = na
float rL1 = na
float rL2 = na
float rL3 = na
float rL4 = na
float rL5 = na
float rL6 = na
if IS_LEN
    if isHtf
        [p1, p2, p3, p4, p5, p6, q1, q2, q3, q4, q5, q6] = request.security(syminfo.tickerid, tfL, htfPacks(s1, aS2 ? s2 : 0, aS3 ? s3 : 0, aS4 ? s4 : 0, aS5 ? s5 : 0, aS6 ? s6 : 0, l1, aL2 ? l2 : 0, aL3 ? l3 : 0, aL4 ? l4 : 0, aL5 ? l5 : 0, aL6 ? l6 : 0), lookahead=barmerge.lookahead_on)
        rS1 := rciAt(p1, s1, close)
        rS2 := aS2 ? rciAt(p2, s2, close) : na
        rS3 := aS3 ? rciAt(p3, s3, close) : na
        rS4 := aS4 ? rciAt(p4, s4, close) : na
        rS5 := aS5 ? rciAt(p5, s5, close) : na
        rS6 := aS6 ? rciAt(p6, s6, close) : na
        rL1 := rciAt(q1, l1, close)
        rL2 := aL2 ? rciAt(q2, l2, close) : na
        rL3 := aL3 ? rciAt(q3, l3, close) : na
        rL4 := aL4 ? rciAt(q4, l4, close) : na
        rL5 := aL5 ? rciAt(q5, l5, close) : na
        rL6 := aL6 ? rciAt(q6, l6, close) : na
else
    rS1 := rciOnTf(t1, lenT)
    if aS2
        rS2 := rciOnTf(t2, lenT)
    if aS3
        rS3 := rciOnTf(t3, lenT)
    if aS4
        rS4 := rciOnTf(t4, lenT)
    if aS5
        rS5 := rciOnTf(t5, lenT)
    if aS6
        rS6 := rciOnTf(t6, lenT)
    rL1 := rciOnTf(u1, lenT)
    if aL2
        rL2 := rciOnTf(u2, lenT)
    if aL3
        rL3 := rciOnTf(u3, lenT)
    if aL4
        rL4 := rciOnTf(u4, lenT)
    if aL5
        rL5 := rciOnTf(u5, lenT)
    if aL6
        rL6 := rciOnTf(u6, lenT)
 
float cS1 = ta.rci(close, s1)
float cS2 = ta.rci(close, s2)
float cS3 = ta.rci(close, s3)
float cS4 = ta.rci(close, s4)
float cS5 = ta.rci(close, s5)
float cS6 = ta.rci(close, s6)
float cL1 = ta.rci(close, l1)
float cL2 = ta.rci(close, l2)
float cL3 = ta.rci(close, l3)
float cL4 = ta.rci(close, l4)
float cL5 = ta.rci(close, l5)
float cL6 = ta.rci(close, l6)
if IS_LEN and not isHtf
    rS1 := cS1
    rS2 := aS2 ? cS2 : na
    rS3 := aS3 ? cS3 : na
    rS4 := aS4 ? cS4 : na
    rS5 := aS5 ? cS5 : na
    rS6 := aS6 ? cS6 : na
    rL1 := cL1
    rL2 := aL2 ? cL2 : na
    rL3 := aL3 ? cL3 : na
    rL4 := aL4 ? cL4 : na
    rL5 := aL5 ? cL5 : na
    rL6 := aL6 ? cL6 : na
 
array<int>    keyS  = IS_LEN ? array.from(s1, s2, s3, s4, s5, s6) : array.from(tfSec(t1), tfSec(t2), tfSec(t3), tfSec(t4), tfSec(t5), tfSec(t6))
array<int>    keyL  = IS_LEN ? array.from(l1, l2, l3, l4, l5, l6) : array.from(tfSec(u1), tfSec(u2), tfSec(u3), tfSec(u4), tfSec(u5), tfSec(u6))
array<string> nameS = IS_LEN ? array.from(str.tostring(s1), str.tostring(s2), str.tostring(s3), str.tostring(s4), str.tostring(s5), str.tostring(s6)) : array.from(tfLabel(t1), tfLabel(t2), tfLabel(t3), tfLabel(t4), tfLabel(t5), tfLabel(t6))
array<string> nameL = IS_LEN ? array.from(str.tostring(l1), str.tostring(l2), str.tostring(l3), str.tostring(l4), str.tostring(l5), str.tostring(l6)) : array.from(tfLabel(u1), tfLabel(u2), tfLabel(u3), tfLabel(u4), tfLabel(u5), tfLabel(u6))
 
drawTf(simple string tf) => IS_LEN or tfSec(tf) >= CHART_SEC
array<bool>   drawS = array.from(drawTf(t1), drawTf(t2), drawTf(t3), drawTf(t4), drawTf(t5), drawTf(t6))
array<bool>   drawL = array.from(drawTf(u1), drawTf(u2), drawTf(u3), drawTf(u4), drawTf(u5), drawTf(u6))
 
rankIn(array<int> keys, int n, int i) =>
    int r = 0
    for j = 0 to n - 1
        if j != i and (keys.get(j) < keys.get(i) or (keys.get(j) == keys.get(i) and j < i))
            r += 1
    r
 
mixCol(color a, color b, int rank, int n) =>
    float t = n <= 1 ? 0.0 : rank / (n - 1.0)
    color.rgb(color.r(a) + (color.r(b) - color.r(a)) * t, color.g(a) + (color.g(b) - color.g(a)) * t, color.b(a) + (color.b(b) - color.b(a)) * t, color.t(a) + (color.t(b) - color.t(a)) * t)
colS(int i) => mixCol(shortDark, shortLight, rankIn(keyS, nS, i), nS)
colL(int i) => mixCol(longDark, longLight, rankIn(keyL, nL, i), nL)
 
hline(lvl,  "Upper level", color=color.new(#787B86, 30), linestyle=hline.style_dotted)
hline(0,    "Zero line",   color=color.new(#787B86, 60), linestyle=hline.style_dotted)
hline(-lvl, "Lower level", color=color.new(#787B86, 30), linestyle=hline.style_dotted)
lineOf(float r, simple string tf) => drawTf(tf) ? r : na
 
plot(lineOf(rS1, t1), "Short 1", color=colS(0), linewidth=lineW)
plot(lineOf(rS2, t2), "Short 2", color=colS(1), linewidth=lineW)
plot(lineOf(rS3, t3), "Short 3", color=colS(2), linewidth=lineW)
plot(lineOf(rS4, t4), "Short 4", color=colS(3), linewidth=lineW)
plot(lineOf(rS5, t5), "Short 5", color=colS(4), linewidth=lineW)
plot(lineOf(rS6, t6), "Short 6", color=colS(5), linewidth=lineW)
plot(lineOf(rL1, u1), "Long 1",  color=colL(0), linewidth=lineW)
plot(lineOf(rL2, u2), "Long 2",  color=colL(1), linewidth=lineW)
plot(lineOf(rL3, u3), "Long 3",  color=colL(2), linewidth=lineW)
plot(lineOf(rL4, u4), "Long 4",  color=colL(3), linewidth=lineW)
plot(lineOf(rL5, u5), "Long 5",  color=colL(4), linewidth=lineW)
plot(lineOf(rL6, u6), "Long 6",  color=colL(5), linewidth=lineW)
 
array<float> valS = array.from(rS1, rS2, rS3, rS4, rS5, rS6)
array<float> valL = array.from(rL1, rL2, rL3, rL4, rL5, rL6)
sortedIdx(array<int> keys, int n) =>
    array<int> k = keys.slice(0, n).copy()
    k.sort_indices(order.ascending)
array<int> ordS = sortedIdx(keyS, nS)
array<int> ordL = sortedIdx(keyL, nL)
 
array<float>  allV = array.new<float>()
array<string> allN = array.new<string>()
array<color>  allC = array.new<color>()
array<bool>   allD = array.new<bool>()
for i = 0 to nS - 1
    int o = ordS.get(i)
    allV.push(valS.get(o))
    allN.push(nameS.get(o))
    allC.push(colS(o))
    allD.push(drawS.get(o))
for i = 0 to nL - 1
    int o = ordL.get(i)
    allV.push(valL.get(o))
    allN.push(nameL.get(o))
    allC.push(colL(o))
    allD.push(drawL.get(o))
 
int nOk = 0
int nUp = 0
int nDn = 0
int nHi = 0
int nLo = 0
for v in allV
    if not na(v)
        nOk += 1
        nUp += v > 0 ? 1 : 0
        nDn += v < 0 ? 1 : 0
        nHi += v >= lvl ? 1 : 0
        nLo += v <= -lvl ? 1 : 0
bool okAll = nOk == N_ALL
bool allUp = okAll and nUp == N_ALL
bool allDn = okAll and nDn == N_ALL
bool allHi = okAll and nHi == N_ALL
bool allLo = okAll and nLo == N_ALL
 
var int lastPoUp = na
var int lastPoDn = na
var bool prevPoUp = false
var bool prevPoDn = false
var int lastCross = 0
bool evPoUp = false
bool evPoDn = false
bool evGC = false
bool evDC = false
if IS_LEN and barstate.isconfirmed
 
    bool poUp = okAll and N_ALL >= 2
    bool poDn = okAll and N_ALL >= 2
    if okAll
        array<int> keyAll = array.new<int>()
        for i = 0 to nS - 1
            keyAll.push(keyS.get(ordS.get(i)))
        for i = 0 to nL - 1
            keyAll.push(keyL.get(ordL.get(i)))
        array<int> ordAll = keyAll.sort_indices(order.ascending)
        for i = 0 to N_ALL - 2
            float va = allV.get(ordAll.get(i))
            float vb = allV.get(ordAll.get(i + 1))
            poUp := poUp and va > vb
            poDn := poDn and va < vb
    evPoUp := poUp and not prevPoUp and (na(lastPoUp) or bar_index - lastPoUp >= poGap)
    evPoDn := poDn and not prevPoDn and (na(lastPoDn) or bar_index - lastPoDn >= poGap)
    if evPoUp
        lastPoUp := bar_index
    if evPoDn
        lastPoDn := bar_index
    prevPoUp := poUp
    prevPoDn := poDn
 
    int grp = 0
    if okAll
        float minS = allV.slice(0, nS).min()
        float maxS = allV.slice(0, nS).max()
        float minL = allV.slice(nS, N_ALL).min()
        float maxL = allV.slice(nS, N_ALL).max()
        grp := minS > maxL ? 1 : maxS < minL ? -1 : 0
    evGC := grp == 1 and lastCross != 1
    evDC := grp == -1 and lastCross != -1
    if grp != 0
        lastCross := grp
 
string upTxt = (showPO and evPoUp ? "PO" : "") + (showPO and evPoUp and showGC and evGC ? " · " : "") + (showGC and evGC ? "GC" : "")
string dnTxt = (showPO and evPoDn ? "PO" : "") + (showPO and evPoDn and showGC and evDC ? " · " : "") + (showGC and evDC ? "DC" : "")
if upTxt != ""
    label.new(bar_index, low, "▲\n" + upTxt, yloc=yloc.belowbar, style=label.style_label_up, color=color.new(upCol, 100), textcolor=upCol, size=txtSize - 1, force_overlay=true,
         tooltip=(evPoUp ? "Bullish perfect order: all lines are in order of length, the shortest on top." : "") + (evPoUp and evGC ? "\n" : "") + (evGC ? "Golden cross: all short lines moved above all long lines." : ""))
if dnTxt != ""
    label.new(bar_index, high, dnTxt + "\n▼", yloc=yloc.abovebar, style=label.style_label_down, color=color.new(dnCol, 100), textcolor=dnCol, size=txtSize - 1, force_overlay=true,
         tooltip=(evPoDn ? "Bearish perfect order: all lines are in order of length, the longest on top." : "") + (evPoDn and evDC ? "\n" : "") + (evDC ? "Death cross: all short lines moved below all long lines." : ""))
 
signed(float v) => na(v) ? "–" : (v >= 0 ? "+" : "−") + str.tostring(math.abs(v), "#")
listText() =>
    string s = ""
    for i = 0 to N_ALL - 1
        s += (i == 0 ? "" : " · ") + allN.get(i) + " " + signed(allV.get(i))
    s
allWord() => "All " + str.tostring(N_ALL)
headText() =>
    string pre = IS_LEN and isHtf ? tfLabel(tfL) + " · " : ""
    string lvTxt = str.tostring(lvl)
    string tot = str.tostring(N_ALL)
    string s = ""
    if nOk < N_ALL
        s := pre + "Not enough bars yet for " + str.tostring(N_ALL - nOk) + " of " + tot + " lines"
    else if nUp >= nDn
        s := pre + (nUp == N_ALL ? allWord() + " above 0" : str.tostring(nUp) + " of " + tot + " above 0") + (nHi > 0 ? " · " + str.tostring(nHi) + " above +" + lvTxt : "")
    else
        s := pre + (nDn == N_ALL ? allWord() + " below 0" : str.tostring(nDn) + " of " + tot + " below 0") + (nLo > 0 ? " · " + str.tostring(nLo) + " below −" + lvTxt : "")
    s
 
var table tbl = table.new(tblPos == "Top left" ? position.top_left : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Bottom left" ? position.bottom_left : position.top_right, 2, 13, bgcolor=chart.bg_color, frame_color=color.new(chart.fg_color, 80), frame_width=1, border_color=chart.bg_color, border_width=2, force_overlay=true)
 
if barstate.islast and showTable
    table.clear(tbl, 0, 0, 1, 12)
    color hc = allUp ? upCol : allDn ? dnCol : normCol
    table.cell(tbl, 0, 0, headText(), text_color=color.white, bgcolor=hc, text_size=txtSize, text_halign=text.align_left)
    table.merge_cells(tbl, 0, 0, 1, 0)
    for i = 0 to N_ALL - 1
        float v = allV.get(i)
        bool dr = allD.get(i)
        color tc = na(v) ? chart.fg_color : v >= lvl ? upCol : v <= -lvl ? dnCol : chart.fg_color
        string tt = dr ? na : "Lower than the chart: not drawn as a line."
        table.cell(tbl, 0, i + 1, (dr ? "━ " : "    ") + allN.get(i), text_color=dr ? color.new(allC.get(i), 0) : chart.fg_color, bgcolor=chart.bg_color, text_size=txtSize, text_halign=text.align_left, tooltip=tt)
        table.cell(tbl, 1, i + 1, signed(v), text_color=tc, bgcolor=chart.bg_color, text_size=txtSize, text_halign=text.align_right, text_font_family=font.family_monospace, tooltip=tt)
 
var int pSide  = 0
var int pLevel = 0
if barstate.isconfirmed
    int side  = allUp ? 1 : allDn ? -1 : 0
    int level = allHi ? 1 : allLo ? -1 : 0
    string head = "[Saikix-RCI8] " + syminfo.ticker + " " + tfLabel(timeframe.period) + " "
    if alLevel and level != 0 and level != pLevel
        alert(head + allWord() + (level > 0 ? " above +" : " below −") + str.tostring(lvl) + ": " + listText(), alert.freq_once_per_bar_close)
    else if alSide and side != 0 and side != pSide
        alert(head + allWord() + (side > 0 ? " above 0" : " below 0") + ": " + listText(), alert.freq_once_per_bar_close)
    pSide  := side
    pLevel := level
    if alPO and evPoUp
        alert(head + "Bullish perfect order: " + listText(), alert.freq_once_per_bar_close)
    if alPO and evPoDn
        alert(head + "Bearish perfect order: " + listText(), alert.freq_once_per_bar_close)
    if alGC and evGC
        alert(head + "Golden cross: " + listText(), alert.freq_once_per_bar_close)
    if alGC and evDC
        alert(head + "Death cross: " + listText(), alert.freq_once_per_bar_close)

← 返回列表