Eye RSI Thermometer [EyeAlgo]

eyealgo · study · 291 行 · 点赞 303 · 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/
// © EyeAlgo
//@version=6
indicator("Eye RSI Thermometer [EyeAlgo]", "Eye RSI Thermometer", overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500)
 
// ───────────────────────── Inputs ─────────────────────────
grpR = "RSI Cycle"
src    = input.source(close, "Source", group=grpR)
len    = input.int(14, "RSI Length", minval=2, group=grpR)
upLvl  = input.float(70, "Bull cycle starts when RSI touches", minval=50, maxval=99, group=grpR)
dnLvl  = input.float(30, "Bear cycle starts when RSI touches", minval=1, maxval=50, group=grpR)
 
grpC = "Candles & Colours"
colorBars = input.bool(true, "Colour price candles by cycle", group=grpC)
shadeWeak = input.bool(true, "Lighter shade when RSI crosses 50 against the cycle", group=grpC)
bullCol   = input.color(#00C853, "Bull cycle", group=grpC)
bearCol   = input.color(#FF1744, "Bear cycle", group=grpC)
waitCol   = input.color(#9E9E9E, "Waiting (before first touch)", group=grpC)
 
grpP = "Price Chart"
showMarks = input.bool(true, "Cycle start labels", group=grpP)
keepMarks = input.int(60, "Labels kept on chart", minval=5, maxval=200, group=grpP)
showStart = input.bool(true, "Cycle start price line", group=grpP)
showFlip  = input.bool(true, "Flip price line (RSI touches the other level next bar)", group=grpP)
farLimit  = input.float(4.0, "Hide price lines farther than (x ATR)", minval=0.5, step=0.5, group=grpP)
 
grpD = "Dashboard"
dashStyle = input.string("Cycle HUD (on chart)", "Dashboard style", options=["Cycle HUD (on chart)", "Classic table", "Off"], group=grpD)
hudGap    = input.int(6, "HUD distance from last candle (bars)", minval=2, maxval=60, group=grpD)
dashPos   = input.string("Top Right", "Table position", options=["Top Right", "Top Left", "Middle Right", "Bottom Right", "Bottom Left"], group=grpD)
dashSize  = input.string("Small", "Table text size", options=["Tiny", "Small", "Normal"], group=grpD)
bool showDash = dashStyle == "Classic table"
bool showHud  = dashStyle == "Cycle HUD (on chart)"
 
// ───────────────────────── RSI (own RMA build) ─────────────────────────
float ch  = ta.change(src)
float upR = ta.rma(math.max(ch, 0), len)
float dnR = ta.rma(math.max(-ch, 0), len)
float rsi = dnR == 0 ? 100.0 : upR == 0 ? 0.0 : 100.0 - 100.0 / (1.0 + upR / dnR)
float atrv = ta.atr(14)
 
// ───────────────────────── Cycle state (latched) ─────────────────────────
var int cyc = 0
if rsi >= upLvl
    cyc := 1
else if rsi <= dnLvl
    cyc := -1
int prevCyc = nz(cyc[1])
bool flipUp = cyc == 1 and prevCyc != 1
bool flipDn = cyc == -1 and prevCyc != -1
 
// ───────────────────────── Cycle tracking + history stats ─────────────────────────
var float stPx  = na
var int   stBar = na
var float stAtr = na
var float mfe   = 0.0
 
var int   bN = 0
var int   bW = 0
var float bLen = 0.0
var float bMove = 0.0
var int   sN = 0
var int   sW = 0
var float sLen = 0.0
var float sMove = 0.0
var array<float> hMove = array.new<float>()
var array<int>   hDir  = array.new<int>()
 
if (flipUp or flipDn) and prevCyc != 0 and not na(stPx) and not na(stAtr)
    float mv = (close - stPx) / stAtr * prevCyc
    int   ln = bar_index - stBar
    hMove.push(mv)
    hDir.push(prevCyc)
    if hMove.size() > 12
        hMove.shift()
        hDir.shift()
    if prevCyc == 1
        bN += 1
        bLen += ln
        bMove += mv
        if mv > 0
            bW += 1
    else
        sN += 1
        sLen += ln
        sMove += mv
        if mv > 0
            sW += 1
 
if flipUp or flipDn
    stPx  := close
    stBar := bar_index
    stAtr := (na(atrv) or atrv <= 0) ? na : atrv
    mfe   := 0.0
else if cyc != 0 and not na(stPx) and not na(stAtr)
    float exc = cyc == 1 ? high - stPx : stPx - low
    mfe := math.max(mfe, exc / stAtr)
 
// ───────────────────────── Flip price (inverse RSI, next bar) ─────────────────────────
float flipPx = na
if cyc == 1 and not na(upR) and not na(dnR)
    float T = dnLvl / 100.0
    float upN = upR * (len - 1) / len
    float dnNeed = upN * (1.0 - T) / T
    float drop = len * dnNeed - dnR * (len - 1)
    flipPx := drop > 0 and src - drop > 0 ? src - drop : na
else if cyc == -1 and not na(upR) and not na(dnR)
    float T = upLvl / 100.0
    float dnN = dnR * (len - 1) / len
    float upNeed = dnN * T / (1.0 - T)
    float rise = len * upNeed - upR * (len - 1)
    flipPx := rise > 0 ? src + rise : na
 
// ───────────────────────── Colours ─────────────────────────
color baseCol = cyc == 1 ? bullCol : cyc == -1 ? bearCol : waitCol
color oppCol  = cyc == 1 ? bearCol : bullCol
bool  weak    = shadeWeak and ((cyc == 1 and rsi < 50) or (cyc == -1 and rsi > 50))
color candCol = weak ? color.new(baseCol, 35) : baseCol
 
// Candle body, wick and border in cycle colour
bool  paint   = colorBars and cyc != 0
barcolor(paint ? candCol : na, title="Cycle candles")
 
// ───────────────────────── Price chart: labels + lines ─────────────────────────
var label[] marks = array.new<label>()
if showMarks and (flipUp or flipDn) and barstate.isconfirmed
    label lb = label.new(bar_index, flipUp ? low : high, flipUp ? "Bull cycle" : "Bear cycle", style=flipUp ? label.style_label_up : label.style_label_down, color=flipUp ? bullCol : bearCol, textcolor=color.white, size=size.small)
    marks.push(lb)
    if marks.size() > keepMarks
        label.delete(marks.shift())
 
var line  lnStart = na
var label lbStart = na
var line  lnFlip  = na
var label lbFlip  = na
if barstate.islast
    line.delete(lnStart)
    label.delete(lbStart)
    line.delete(lnFlip)
    label.delete(lbFlip)
    float lim = na(atrv) ? na : farLimit * atrv
    if showStart and cyc != 0 and not na(stPx) and not na(lim) and math.abs(close - stPx) <= lim
        int x1 = math.max(stBar, bar_index - 4900)
        lnStart := line.new(x1, stPx, bar_index + 8, stPx, color=color.new(baseCol, 20), style=line.style_dotted, width=1)
        lbStart := label.new(bar_index + 8, stPx, "Cycle start " + str.tostring(stPx, format.mintick), style=label.style_label_left, color=#00000000, textcolor=baseCol, size=size.small)
    if showFlip and not na(flipPx) and not na(lim) and math.abs(flipPx - src) <= lim
        lnFlip := line.new(bar_index - 5, flipPx, bar_index + 8, flipPx, color=oppCol, style=line.style_dashed, width=1)
        lbFlip := label.new(bar_index + 8, flipPx, (cyc == 1 ? "Bear flip ≈ " : "Bull flip ≈ ") + str.tostring(flipPx, format.mintick), style=label.style_label_left, color=#00000000, textcolor=oppCol, size=size.small)
 
// ───────────────────────── Dashboard ─────────────────────────
sz = dashSize == "Tiny" ? size.tiny : dashSize == "Normal" ? size.normal : size.small
pos = switch dashPos
    "Top Left"     => position.top_left
    "Middle Right" => position.middle_right
    "Bottom Right" => position.bottom_right
    "Bottom Left"  => position.bottom_left
    => position.top_right
 
var table tb = table.new(pos, 2, 10, border_color=color.new(chart.fg_color, 88), border_width=1, frame_color=color.new(chart.fg_color, 75), frame_width=1)
color bgC = color.new(chart.bg_color, 0)
color fgC = chart.fg_color
color dimC = color.new(chart.fg_color, 35)
 
fSigned(float x) =>
    na(x) ? "—" : (x >= 0 ? "+" : "") + str.tostring(x, "0.0")
 
fSide(int n, int w, float mv) =>
    n == 0 ? "—" : str.tostring(n) + " cycles · " + str.tostring(w * 100.0 / n, "0") + "% in favour · avg " + fSigned(mv / n) + " ATR"
 
fRow(int r, string k, string v, color vc) =>
    table.cell(tb, 0, r, k, text_color=dimC, text_size=sz, text_halign=text.align_left, bgcolor=bgC)
    table.cell(tb, 1, r, v, text_color=vc, text_size=sz, text_halign=text.align_right, bgcolor=bgC)
 
if showDash and barstate.isfirst
    table.merge_cells(tb, 0, 0, 1, 0)
    table.merge_cells(tb, 0, 9, 1, 9)
 
if showDash and barstate.islast
    string cycTxt = cyc == 1 ? "BULL ▲" : cyc == -1 ? "BEAR ▼" : "WAITING"
    int    barsIn = cyc != 0 and not na(stBar) ? bar_index - stBar : na
    float  avgLen = cyc == 1 ? (bN > 0 ? bLen / bN : na) : cyc == -1 ? (sN > 0 ? sLen / sN : na) : na
    float  mvAtr  = cyc != 0 and not na(stPx) and not na(stAtr) ? (close - stPx) / stAtr * cyc : na
    float  mvPct  = cyc != 0 and not na(stPx) and stPx != 0 ? (close - stPx) / stPx * 100.0 * cyc : na
    string barsTxt = na(barsIn) ? "—" : str.tostring(barsIn) + (na(avgLen) ? "" : "  ·  avg " + str.tostring(avgLen, "0") + " (" + str.tostring(barsIn * 100.0 / avgLen, "0") + "%)")
    string flipTxt = na(flipPx) ? "—" : str.tostring(flipPx, format.mintick) + (na(atrv) or atrv == 0 ? "" : "  (" + str.tostring(math.abs(flipPx - src) / atrv, "0.0") + " ATR away)")
 
    table.cell(tb, 0, 0, "Eye RSI Thermometer", text_color=color.white, text_size=sz, bgcolor=baseCol)
    table.cell(tb, 1, 0, "", bgcolor=baseCol)
    fRow(1, "Cycle", cycTxt, baseCol)
    fRow(2, "RSI", str.tostring(rsi, "0.0") + (weak ? "  · weakening" : ""), weak ? dimC : fgC)
    fRow(3, "Bars in cycle", barsTxt, fgC)
    fRow(4, "Move with cycle", na(mvPct) ? "—" : fSigned(mvPct) + "%  ·  " + fSigned(mvAtr) + " ATR", na(mvAtr) ? fgC : mvAtr >= 0 ? bullCol : bearCol)
    fRow(5, "Best run this cycle", cyc != 0 ? fSigned(mfe) + " ATR" : "—", fgC)
    fRow(6, cyc == -1 ? "Bull flip price" : "Bear flip price", flipTxt, oppCol)
    fRow(7, "Past bull cycles", fSide(bN, bW, bMove), bullCol)
    fRow(8, "Past bear cycles", fSide(sN, sW, sMove), bearCol)
    table.cell(tb, 0, 9, "In-sample stats on this chart · before spread/commission", text_color=dimC, text_size=size.tiny, bgcolor=bgC)
    table.cell(tb, 1, 9, "", bgcolor=bgC)
 
// ───────────────────────── Cycle HUD (drawn on the chart, right of the last candle) ─────────────────────────
var box[]   hudB  = array.new<box>()
var label[] hudL  = array.new<label>()
var line[]  hudLn = array.new<line>()
 
fY(float r, float b, float t) =>
    b + (t - b) * r / 100.0
 
fBox(int l, float y1, int r, float y2, color bg, color bd) =>
    hudB.push(box.new(l, math.max(y1, y2), r, math.min(y1, y2), border_color=bd, bgcolor=bg))
 
fLab(int x, float y, string t, string st, color bg, color tc, string s) =>
    hudL.push(label.new(x, y, t, style=st, color=bg, textcolor=tc, size=s))
 
if barstate.islast
    for b in hudB
        box.delete(b)
    hudB.clear()
    for l in hudL
        label.delete(l)
    hudL.clear()
    for l in hudLn
        line.delete(l)
    hudLn.clear()
 
    if showHud and not na(atrv) and atrv > 0 and not na(rsi)
        float bot  = close - 3.0 * atrv
        float top  = close + 3.0 * atrv
        color fg   = chart.fg_color
        color dim  = color.new(chart.fg_color, 40)
        color none = #00000000
        int   x0   = bar_index + hudGap
        int   x1   = x0 + 6
 
        // header
        string head = "EYE RSI THERMOMETER  ·  " + (cyc == 1 ? "BULL ▲" : cyc == -1 ? "BEAR ▼" : "WAITING")
        fLab(x0, fY(100, bot, top) + 0.25 * atrv, head, label.style_label_lower_left, baseCol, color.white, size.normal)
 
        // RSI thermometer
        fBox(x0, fY(100, bot, top), x0 + 2, fY(0, bot, top), color.new(fg, 95), color.new(fg, 70))
        fBox(x0, fY(100, bot, top), x0 + 2, fY(upLvl, bot, top), color.new(bullCol, 85), none)
        fBox(x0, fY(dnLvl, bot, top), x0 + 2, fY(0, bot, top), color.new(bearCol, 85), none)
        fBox(x0, fY(rsi, bot, top), x0 + 2, fY(50, bot, top), color.new(baseCol, weak ? 55 : 15), none)
        fLab(x0, fY(upLvl, bot, top), str.tostring(upLvl, "0"), label.style_label_right, none, dim, size.tiny)
        fLab(x0, fY(50, bot, top), "50", label.style_label_right, none, dim, size.tiny)
        fLab(x0, fY(dnLvl, bot, top), str.tostring(dnLvl, "0"), label.style_label_right, none, dim, size.tiny)
        fLab(x0 + 2, fY(rsi, bot, top), "RSI " + str.tostring(rsi, "0.0") + (weak ? " · weak" : ""), label.style_label_left, baseCol, color.white, size.small)
 
        // cycle age battery (10 cells = average cycle length)
        int   barsIn = cyc != 0 and not na(stBar) ? bar_index - stBar : na
        float avgLen = cyc == 1 ? (bN > 0 ? bLen / bN : na) : cyc == -1 ? (sN > 0 ? sLen / sN : na) : na
        float age    = na(barsIn) or na(avgLen) or avgLen == 0 ? na : barsIn / avgLen
        int   cells  = na(age) ? 0 : math.min(10, int(math.round(age * 10)))
        color cellC  = not na(age) and age > 1 ? #FF9800 : baseCol
        fLab(x1 + 8, fY(92, bot, top), "CYCLE AGE  " + (na(barsIn) ? "—" : str.tostring(barsIn) + (na(avgLen) ? " bars" : " / " + str.tostring(avgLen, "0") + " bars" + (age > 1 ? "  · overdue" : ""))), label.style_label_center, none, dim, size.small)
        for i = 0 to 9
            fBox(x1 + i * 2, fY(86, bot, top), x1 + i * 2 + 1, fY(79, bot, top), i < cells ? cellC : color.new(fg, 90), none)
 
        // move + flip
        float mvAtr = cyc != 0 and not na(stPx) and not na(stAtr) ? (close - stPx) / stAtr * cyc : na
        float mvPct = cyc != 0 and not na(stPx) and stPx != 0 ? (close - stPx) / stPx * 100.0 * cyc : na
        string mvTxt = na(mvAtr) ? "MOVE  —" : "MOVE  " + fSigned(mvPct) + "%  ·  " + fSigned(mvAtr) + " ATR   BEST " + fSigned(mfe) + " ATR"
        fLab(x1, fY(68, bot, top), mvTxt, label.style_label_left, none, na(mvAtr) ? fg : mvAtr >= 0 ? bullCol : bearCol, size.small)
        string flTxt = na(flipPx) ? "FLIP  —" : (cyc == 1 ? "BEAR FLIP ≈ " : "BULL FLIP ≈ ") + str.tostring(flipPx, format.mintick) + "  (" + str.tostring(math.abs(flipPx - src) / atrv, "0.0") + " ATR)"
        fLab(x1, fY(56, bot, top), flTxt, label.style_label_left, none, oppCol, size.small)
 
        // last 12 cycles barcode: up = ended in favour, down = ended against
        float base = fY(26, bot, top)
        float hgt  = fY(42, bot, top) - base
        fLab(x1 + 11, fY(46, bot, top), "LAST " + str.tostring(hMove.size()) + " CYCLES", label.style_label_center, none, dim, size.small)
        hudLn.push(line.new(x1 - 1, base, x1 + 24, base, color=color.new(fg, 60), width=1))
        if hMove.size() > 0
            for i = 0 to hMove.size() - 1
                float m  = hMove.get(i)
                int   d  = hDir.get(i)
                float h  = math.min(math.abs(m), 3.0) / 3.0 * hgt
                color bc = d == 1 ? bullCol : bearCol
                fBox(x1 + i * 2, base, x1 + i * 2 + 1, m >= 0 ? base + math.max(h, hgt * 0.05) : base - math.max(h, hgt * 0.05), m >= 0 ? bc : color.new(bc, 60), none)
 
        // history summary
        string bS = bN == 0 ? "Bull —" : "Bull " + str.tostring(bW * 100.0 / bN, "0") + "% in favour · " + fSigned(bMove / bN) + " ATR"
        string sS = sN == 0 ? "Bear —" : "Bear " + str.tostring(sW * 100.0 / sN, "0") + "% in favour · " + fSigned(sMove / sN) + " ATR"
        fLab(x1, fY(8, bot, top), bS + "    " + sS, label.style_label_left, none, fg, size.small)
        fLab(x1, fY(0, bot, top), "in-sample on this chart · before costs", label.style_label_left, none, dim, size.tiny)
 
// ───────────────────────── Alerts ─────────────────────────
alertcondition(barstate.isconfirmed and flipUp, "Bull cycle start", "Eye RSI Thermometer: RSI touched the upper level, bull cycle started")
alertcondition(barstate.isconfirmed and flipDn, "Bear cycle start", "Eye RSI Thermometer: RSI touched the lower level, bear cycle started")
alertcondition(barstate.isconfirmed and (flipUp or flipDn), "Any cycle change", "Eye RSI Thermometer: RSI cycle changed")
 
if barstate.isconfirmed and (flipUp or flipDn)
    alert("Eye RSI Thermometer | " + syminfo.ticker + " " + timeframe.period + " | " + (flipUp ? "BULL" : "BEAR") + " cycle started | RSI " + str.tostring(rsi, "0.0") + " | close " + str.tostring(close, format.mintick), alert.freq_once_per_bar_close)

← 返回列表