Unhyped RSI

BitcoinEasyIO · study · 204 行 · 点赞 30 · TradingView 原页

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

Pine Script

//@version=6
// Unhyped RSI v6.0
// RSI(14) with regime-aware range bands (bull support 40-50, bear resistance 50-60),
// zone-gated signal-MA crosses, 30/70 exhaustion reclaims and pivot divergences.
// Divergences confirm `pivLookback` bars after the pivot by construction; they do not repaint after printing.
indicator("Unhyped RSI", shorttitle = "Unhyped RSI", overlay = false, format = format.price, precision = 1, max_lines_count = 200)
 
// ---------------------------------------------------------------- 1. Inputs
grpCore = "1. RSI and signal line"
rsiLen  = input.int(14, "RSI length", minval = 2, group = grpCore)
maType  = input.string("SMA", "Signal line type", options = ["SMA", "EMA", "WMA", "RMA"], group = grpCore)
maLen   = input.int(14, "Signal line length", minval = 2, group = grpCore)
 
grpReg   = "2. Regime"
trendLen = input.int(200, "Regime SMA length", minval = 10, group = grpReg)
regimeTf = input.timeframe("", "Regime timeframe (blank = chart, 'D' recommended on intraday)", group = grpReg)
 
grpSig        = "3. Signal filters"
reqCandle     = input.bool(true, "Require candle confirmation (close in direction or 40% wick)", group = grpSig)
zoneLookback  = input.int(5, "Bars to look back for the band test", minval = 2, maxval = 20, group = grpSig)
minSlope      = input.float(1.2, "Min RSI change on the signal bar", minval = 0.1, maxval = 5.0, step = 0.1, group = grpSig)
cooldownBars  = input.int(6, "Cooldown between same-direction signals (bars)", minval = 1, maxval = 50, group = grpSig)
pivLookback   = input.int(5, "Divergence pivot lookback (bars each side)", minval = 2, maxval = 15, group = grpSig)
divCooldown   = input.int(12, "Cooldown between same-direction divergences (bars)", minval = 5, maxval = 60, group = grpSig)
minDivPts     = input.float(1.5, "Min RSI difference between pivots", minval = 0.5, step = 0.5, group = grpSig)
 
grpVis       = "4. Display"
showBands    = input.bool(true, "Show regime band (40-50 bull / 50-60 bear)", group = grpVis)
showRibbon   = input.bool(true, "Shade RSI vs signal line", group = grpVis)
showCrosses  = input.bool(true, "Show band-test crosses (triangles)", group = grpVis)
showExtremes = input.bool(true, "Show 30/70 reclaim diamonds", group = grpVis)
showDiv      = input.bool(true, "Show classic divergences", group = grpVis)
showHidden   = input.bool(true, "Show hidden divergences", group = grpVis)
showHUD      = input.bool(true, "Show status bar", group = grpVis)
hudPosInput  = input.string("Top Center", "Status bar position", options = ["Top Center", "Bottom Center", "Top Right", "Bottom Right"], group = grpVis)
 
// Unhyped palette
cBull  = #00E676
cBear  = #FF1744
cAmber = #FFAB00
cCyan  = #00E5FF
cOrng  = #FF9100
cSlate = #161B22
cGrey  = #9CA3AF
 
// ---------------------------------------------------------------- 2. Core series (all ta.* calls at global scope, every bar)
rsiVal = ta.rsi(close, rsiLen)
rsiMA = switch maType
    "SMA" => ta.sma(rsiVal, maLen)
    "EMA" => ta.ema(rsiVal, maLen)
    "WMA" => ta.wma(rsiVal, maLen)
    => ta.rma(rsiVal, maLen)
 
useHtf   = regimeTf != ""
reqTf    = useHtf ? regimeTf : timeframe.period
// Non-repainting higher-timeframe read: last CONFIRMED bar of the regime timeframe.
[htfClose, htfSma] = request.security(syminfo.tickerid, reqTf, [close[1], ta.sma(close, trendLen)[1]], lookahead = barmerge.lookahead_on)
chartSma = ta.sma(close, trendLen)
isBull   = useHtf ? htfClose >= htfSma : close >= chartSma
isMomUp  = rsiVal >= rsiMA
 
lowestR  = ta.lowest(rsiVal, zoneLookback)
highestR = ta.highest(rsiVal, zoneLookback)
crossUp  = ta.crossover(rsiVal, rsiMA)
crossDn  = ta.crossunder(rsiVal, rsiMA)
cross30  = ta.crossover(rsiVal, 30)
cross70  = ta.crossunder(rsiVal, 70)
rsiChg   = rsiVal - rsiVal[1]
 
pl = ta.pivotlow(rsiVal, pivLookback, pivLookback)
ph = ta.pivothigh(rsiVal, pivLookback, pivLookback)
bullAtPivot = isBull[pivLookback]
 
// ---------------------------------------------------------------- 3. Candle confirmation
candleRange = math.max(high - low, syminfo.mintick)
lowerWick   = math.min(open, close) - low
upperWick   = high - math.max(open, close)
bullCandle  = not reqCandle or close > open or lowerWick / candleRange >= 0.40
bearCandle  = not reqCandle or close < open or upperWick / candleRange >= 0.40
 
// ---------------------------------------------------------------- 4. Signals
// Bull regime: RSI tested the 40-50 support band (allowing a 5-point overshoot to 35) and crossed back above its signal line.
// Bear regime: RSI tested the 50-60 resistance band (overshoot to 65) and crossed back below its signal line.
rawBull = isBull and crossUp and lowestR <= 52 and lowestR >= 35 and rsiChg >= minSlope and bullCandle
rawBear = not isBull and crossDn and highestR >= 48 and highestR <= 65 and -rsiChg >= minSlope and bearCandle
 
var int lastBull   = -1000
var int lastBear   = -1000
var int lastBullEx = -1000
var int lastBearEx = -1000
var int lastBullDv = -1000
var int lastBearDv = -1000
 
bullSig = rawBull and bar_index - lastBull >= cooldownBars
if bullSig
    lastBull := bar_index
bearSig = rawBear and bar_index - lastBear >= cooldownBars
if bearSig
    lastBear := bar_index
 
bullEx = cross30 and bullCandle and bar_index - lastBullEx >= cooldownBars
if bullEx
    lastBullEx := bar_index
bearEx = cross70 and bearCandle and bar_index - lastBearEx >= cooldownBars
if bearEx
    lastBearEx := bar_index
 
// ---------------------------------------------------------------- 5. Divergences (regime sampled at the pivot bar)
var float prevRsiLow   = na
var float prevPriceLow = na
var int   prevLowBar   = na
var float prevRsiHigh  = na
var float prevPriceHigh = na
var int   prevHighBar  = na
 
bool bullDiv  = false
bool hBullDiv = false
bool bearDiv  = false
bool hBearDiv = false
 
if not na(pl) and pl <= 50
    curBar   = bar_index - pivLookback
    curPrice = low[pivLookback]
    if not na(prevRsiLow)
        dist = curBar - prevLowBar
        if dist >= 6 and dist <= 60 and curBar - lastBullDv >= divCooldown
            if showDiv and curPrice < prevPriceLow and pl - prevRsiLow >= minDivPts
                bullDiv := true
                lastBullDv := curBar
                line.new(prevLowBar, prevRsiLow, curBar, pl, color = cBull, width = 2)
            else if showHidden and bullAtPivot and curPrice > prevPriceLow and prevRsiLow - pl >= minDivPts
                hBullDiv := true
                lastBullDv := curBar
                line.new(prevLowBar, prevRsiLow, curBar, pl, color = cCyan, width = 2, style = line.style_dashed)
    prevRsiLow   := pl
    prevPriceLow := curPrice
    prevLowBar   := curBar
 
if not na(ph) and ph >= 50
    curBar   = bar_index - pivLookback
    curPrice = high[pivLookback]
    if not na(prevRsiHigh)
        dist = curBar - prevHighBar
        if dist >= 6 and dist <= 60 and curBar - lastBearDv >= divCooldown
            if showDiv and curPrice > prevPriceHigh and prevRsiHigh - ph >= minDivPts
                bearDiv := true
                lastBearDv := curBar
                line.new(prevHighBar, prevRsiHigh, curBar, ph, color = cBear, width = 2)
            else if showHidden and not bullAtPivot and curPrice < prevPriceHigh and ph - prevRsiHigh >= minDivPts
                hBearDiv := true
                lastBearDv := curBar
                line.new(prevHighBar, prevRsiHigh, curBar, ph, color = cOrng, width = 2, style = line.style_dashed)
    prevRsiHigh   := ph
    prevPriceHigh := curPrice
    prevHighBar   := curBar
 
// ---------------------------------------------------------------- 6. Plots
hline(70, "70", color = color.new(cBear, 50), linestyle = hline.style_dashed)
hline(50, "50", color = color.new(cGrey, 55), linestyle = hline.style_solid)
hline(30, "30", color = color.new(cBull, 50), linestyle = hline.style_dashed)
 
p60 = plot(60, "Band 60", display = display.none, editable = false)
p50 = plot(50, "Band 50", display = display.none, editable = false)
p40 = plot(40, "Band 40", display = display.none, editable = false)
fill(p50, p40, color = showBands and isBull ? color.new(cBull, 85) : na, title = "Bull regime support band 40-50")
fill(p60, p50, color = showBands and not isBull ? color.new(cBear, 85) : na, title = "Bear regime resistance band 50-60")
 
pRsi = plot(rsiVal, "RSI", color = color.white, linewidth = 2)
pMa  = plot(rsiMA, "Signal line", color = cAmber, linewidth = 2)
fill(pRsi, pMa, color = showRibbon ? (isMomUp ? color.new(cBull, 78) : color.new(cBear, 78)) : na, title = "RSI vs signal line")
 
plotshape(showCrosses and bullSig ? math.min(rsiVal, rsiMA) - 4 : na, "Band test up", shape.triangleup, location.absolute, cBull, size = size.small, display = display.pane)
plotshape(showCrosses and bearSig ? math.max(rsiVal, rsiMA) + 4 : na, "Band test down", shape.triangledown, location.absolute, cBear, size = size.small, display = display.pane)
plotshape(showExtremes and bullEx ? 22 : na, "30 reclaim", shape.diamond, location.absolute, cCyan, size = size.tiny, display = display.pane)
plotshape(showExtremes and bearEx ? 78 : na, "70 breakdown", shape.diamond, location.absolute, cOrng, size = size.tiny, display = display.pane)
 
plotshape(bullDiv ? rsiVal[pivLookback] - 4 : na, "Bullish divergence", shape.labelup, location.absolute, cBull, offset = -pivLookback, text = "DIV", textcolor = color.black, size = size.tiny, display = display.pane)
plotshape(hBullDiv ? rsiVal[pivLookback] - 4 : na, "Hidden bullish divergence", shape.labelup, location.absolute, cCyan, offset = -pivLookback, text = "H-DIV", textcolor = color.black, size = size.tiny, display = display.pane)
plotshape(bearDiv ? rsiVal[pivLookback] + 4 : na, "Bearish divergence", shape.labeldown, location.absolute, cBear, offset = -pivLookback, text = "DIV", textcolor = color.white, size = size.tiny, display = display.pane)
plotshape(hBearDiv ? rsiVal[pivLookback] + 4 : na, "Hidden bearish divergence", shape.labeldown, location.absolute, cOrng, offset = -pivLookback, text = "H-DIV", textcolor = color.black, size = size.tiny, display = display.pane)
 
// ---------------------------------------------------------------- 7. Status bar
hudPos = switch hudPosInput
    "Top Center"    => position.top_center
    "Bottom Center" => position.bottom_center
    "Top Right"     => position.top_right
    => position.bottom_right
 
var table hud = table.new(hudPos, 3, 1, bgcolor = color.new(cSlate, 8), border_color = color.new(#2D3748, 25), border_width = 1)
if barstate.islast and showHUD
    inBand    = isBull ? rsiVal >= 40 and rsiVal <= 50 : rsiVal >= 50 and rsiVal <= 60
    zoneText  = rsiVal > 70 ? "STRETCHED (>70)" : rsiVal < 30 ? "STRETCHED (<30)" : inBand ? (isBull ? "IN BULL SUPPORT BAND" : "IN BEAR RESISTANCE BAND") : "BETWEEN BANDS"
    zoneColor = inBand ? (isBull ? cBull : cBear) : color.new(#E5E7EB, 0)
    table.cell(hud, 0, 0, "RSI " + str.tostring(rsiVal, "0.0") + " | " + zoneText, text_color = zoneColor, text_size = size.small)
    table.cell(hud, 1, 0, isMomUp ? "MOMENTUM UP" : "MOMENTUM DOWN", text_color = isMomUp ? cBull : cBear, text_size = size.small)
    table.cell(hud, 2, 0, (isBull ? "REGIME BULL" : "REGIME BEAR") + (useHtf ? " (" + regimeTf + ")" : ""), text_color = isBull ? cBull : cBear, text_size = size.small)
 
// ---------------------------------------------------------------- 8. Alerts
alertcondition(bullSig, "Unhyped RSI: bull band test", "Unhyped RSI: bull-regime band test on {{ticker}} ({{interval}}) at {{close}}")
alertcondition(bearSig, "Unhyped RSI: bear band test", "Unhyped RSI: bear-regime band test on {{ticker}} ({{interval}}) at {{close}}")
alertcondition(bullEx, "Unhyped RSI: 30 reclaim", "Unhyped RSI: RSI reclaimed 30 on {{ticker}} ({{interval}})")
alertcondition(bearEx, "Unhyped RSI: 70 breakdown", "Unhyped RSI: RSI lost 70 on {{ticker}} ({{interval}})")
alertcondition(bullDiv or hBullDiv, "Unhyped RSI: bullish divergence", "Unhyped RSI: bullish divergence confirmed on {{ticker}} ({{interval}})")
alertcondition(bearDiv or hBearDiv, "Unhyped RSI: bearish divergence", "Unhyped RSI: bearish divergence confirmed on {{ticker}} ({{interval}})")

← 返回列表