Institutional RSI Momentum & Signal Statistics

PremiumTrader57 · study · 196 行 · 点赞 7 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PremiumTrader57
//@version=6
indicator("Institutional RSI Momentum & Signal Statistics", shorttitle = "RSI Stats Pro", overlay = false, max_labels_count = 500, max_lines_count = 500)
 
// ======== 1. OSCILLATOR & TREND SETTINGS ========
GRP_OSC = "1. RSI & HTF Trend Confluence"
rsiLen  = input.int(14, "RSI Length", minval = 2, group = GRP_OSC)
maLen   = input.int(14, "RSI Signal MA Length", minval = 2, group = GRP_OSC)
src     = input.source(close, "Source", group = GRP_OSC)
obLvl   = input.int(70, "Overbought Level", minval = 55, maxval = 95, group = GRP_OSC)
osLvl   = input.int(30, "Oversold Level", minval = 5, maxval = 45, group = GRP_OSC)
emaLen  = input.int(200, "HTF Trend EMA Length", minval = 10, group = GRP_OSC, tooltip = "Separates trend-aligned pullbacks from counter-trend reversals.")
 
// ======== 2. SIGNAL GRADING ENGINE ========
GRP_ENG = "2. Signal Grading & Risk Engine"
atrLen  = input.int(14, "ATR Length", minval = 2, group = GRP_ENG)
tgtAtr  = input.float(1.5, "Target Distance (x ATR)", minval = 0.2, step = 0.1, group = GRP_ENG)
stpAtr  = input.float(1.0, "Stop Distance (x ATR)", minval = 0.2, step = 0.1, group = GRP_ENG)
horizon = input.int(24, "Max Bars to Resolve", minval = 3, maxval = 200, group = GRP_ENG)
minN    = input.int(8, "Min Samples to Display Win %", minval = 3, group = GRP_ENG)
 
// ======== 3. VISUALS & OVERLAYS ========
GRP_VIS = "3. Visuals & Chart Overlay"
colRsi  = input.color(#2962FF, "RSI Line", group = GRP_VIS)
colMa   = input.color(#FF9800, "Signal MA Line", group = GRP_VIS)
wLine   = input.int(2, "Line Width", minval = 1, maxval = 5, group = GRP_VIS)
colBull = input.color(#089981, "Bullish Accent", group = GRP_VIS)
colBear = input.color(#F23645, "Bearish Accent", group = GRP_VIS)
lblMode = input.string("Trend Pullbacks", "Price Chart Labels", options = ["Trend Pullbacks", "Zone Exits", "50-Line Crosses", "All", "Off"], group = GRP_VIS)
showLvl = input.bool(true, "Draw Active TP / SL Levels on Price Chart", group = GRP_VIS)
lvlLen  = input.int(15, "TP/SL Line Length (Bars)", minval = 5, maxval = 100, group = GRP_VIS)
 
// ======== 4. DASHBOARD ========
GRP_HUD = "4. Institutional HUD"
showHud = input.bool(true, "Show Statistics Table on Price Chart", group = GRP_HUD)
hudPos  = input.string("Bottom Right", "Dashboard Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = GRP_HUD)
 
// ======== CALCULATIONS ========
float rsi    = ta.rsi(src, rsiLen)
float rsiMa  = ta.ema(rsi, maLen)
float htfEma = ta.ema(close, emaLen)
float atr    = ta.atr(atrLen)
 
bool bullTrend = close > htfEma
bool bearTrend = close < htfEma
 
bool sig0 = bullTrend and rsi < 50 and ta.crossover(rsi, rsiMa)
bool sig1 = bearTrend and rsi > 50 and ta.crossunder(rsi, rsiMa)
bool sig2 = ta.crossover(rsi, osLvl)
bool sig3 = ta.crossunder(rsi, obLvl)
bool sig4 = ta.crossover(rsi, 50)
bool sig5 = ta.crossunder(rsi, 50)
 
// ======== SIGNAL GRADING & TRACKING ENGINE ========
var array<int>   pBar = array.new<int>(6, -1)
var array<float> pTgt = array.new<float>(6, na)
var array<float> pStp = array.new<float>(6, na)
var array<float> stN  = array.new<float>(6, 0.0)
var array<float> stW  = array.new<float>(6, 0.0)
 
var array<line>  actLines  = array.new<line>()
var array<label> actLabels = array.new<label>()
 
var array<string> sigNames = array.from("BULL Trend Pullback", "BEAR Trend Pullback", "BULL Oversold Exit", "BEAR Overbought Exit", "BULL 50-Line Break", "BEAR 50-Line Break")
 
f_isBull(int idx) =>
    idx == 0 or idx == 2 or idx == 4
 
f_winRate(int idx) =>
    float n = array.get(stN, idx)
    n >= minN ? (array.get(stW, idx) / n) * 100.0 : na
 
f_drawSetup(float entryP, float tpP, float slP) =>
    if showLvl
        while array.size(actLines) > 0
            line.delete(array.shift(actLines))
        while array.size(actLabels) > 0
            label.delete(array.shift(actLabels))
        array.push(actLines, line.new(bar_index, entryP, bar_index + lvlLen, entryP, color = color.new(#2962FF, 20), width = 1, style = line.style_solid, force_overlay = true))
        array.push(actLines, line.new(bar_index, tpP, bar_index + lvlLen, tpP, color = colBull, width = 1, style = line.style_dotted, force_overlay = true))
        array.push(actLines, line.new(bar_index, slP, bar_index + lvlLen, slP, color = colBear, width = 1, style = line.style_dashed, force_overlay = true))
        array.push(actLabels, label.new(bar_index + lvlLen, tpP, "TP " + str.tostring(tpP, format.mintick), style = label.style_label_left, color = color.new(colBull, 20), textcolor = color.white, size = size.tiny, force_overlay = true))
        array.push(actLabels, label.new(bar_index + lvlLen, slP, "SL " + str.tostring(slP, format.mintick), style = label.style_label_left, color = color.new(colBear, 20), textcolor = color.white, size = size.tiny, force_overlay = true))
 
f_register(int idx, bool drawChart) =>
    bool bull = f_isBull(idx)
    float tp  = bull ? close + tgtAtr * atr : close - tgtAtr * atr
    float sl  = bull ? close - stpAtr * atr : close + stpAtr * atr
    if array.get(pBar, idx) < 0
        array.set(pBar, idx, bar_index)
        array.set(pTgt, idx, tp)
        array.set(pStp, idx, sl)
    if drawChart
        float wr     = f_winRate(idx)
        string wrTxt = na(wr) ? "" : " (" + str.tostring(math.round(wr)) + "%)"
        string tag   = idx <= 1 ? (bull ? "PB BUY" : "PB SELL") : idx <= 3 ? (bull ? "OS BUY" : "OB SELL") : (bull ? "50+ BUY" : "50- SELL")
        label.new(bar_index, bull ? low : high, tag + wrTxt, style = bull ? label.style_label_up : label.style_label_down, color = color.new(bull ? colBull : colBear, 15), textcolor = color.white, size = size.tiny, force_overlay = true)
        f_drawSetup(close, tp, sl)
 
if barstate.isconfirmed and not na(atr)
    for i = 0 to 5
        int pb = array.get(pBar, i)
        if pb >= 0 and bar_index > pb
            bool  bull = f_isBull(i)
            float tg   = array.get(pTgt, i)
            float sp   = array.get(pStp, i)
            bool  hitT = bull ? high >= tg : low <= tg
            bool  hitS = bull ? low <= sp  : high >= sp
            bool  exp  = bar_index - pb >= horizon
            if hitT or hitS
                array.set(stN, i, array.get(stN, i) + 1.0)
                if hitT and not hitS
                    array.set(stW, i, array.get(stW, i) + 1.0)
                array.set(pBar, i, -1)
            else if exp
                array.set(pBar, i, -1)
 
    bool showPb = lblMode == "Trend Pullbacks" or lblMode == "All"
    bool showZn = lblMode == "Zone Exits" or lblMode == "All"
    bool show50 = lblMode == "50-Line Crosses" or lblMode == "All"
 
    if sig0
        f_register(0, showPb)
    if sig1
        f_register(1, showPb)
    if sig2
        f_register(2, showZn)
    if sig3
        f_register(3, showZn)
    if sig4
        f_register(4, show50)
    if sig5
        f_register(5, show50)
 
// ======== OSCILLATOR PANE PLOTS ========
pRsi = plot(rsi, "RSI", color = colRsi, linewidth = wLine)
pMa  = plot(rsiMa, "Signal MA", color = colMa, linewidth = 1)
fill(pRsi, pMa, color = rsi >= rsiMa ? color.new(colBull, 85) : color.new(colBear, 85), title = "Momentum Cloud")
 
hOb  = hline(obLvl, "Overbought", color = color.new(colBear, 45), linestyle = hline.style_dashed)
hMid = hline(50, "Midline 50", color = color.new(#787B86, 55), linestyle = hline.style_dotted)
hOs  = hline(osLvl, "Oversold", color = color.new(colBull, 45), linestyle = hline.style_dashed)
fill(hOb, hOs, color = color.new(#787B86, 94), title = "Neutral Band")
 
// ======== INSTITUTIONAL DASHBOARD (ON PRICE CHART) ========
f_hudPos(string p) =>
    switch p
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Bottom Right" => position.bottom_right
        =>                position.bottom_left
 
var table hud = table.new(f_hudPos(hudPos), 3, 10, bgcolor = color.new(#131722, 8), border_color = color.new(#363A45, 0), border_width = 1, frame_color = color.new(#363A45, 0), frame_width = 2, force_overlay = true)
 
f_renderRow(int row, int idx, color bg) =>
    float n      = array.get(stN, idx)
    float wr     = f_winRate(idx)
    bool bull    = f_isBull(idx)
    string wrStr = na(wr) ? "Collecting..." : str.tostring(math.round(wr)) + "%"
    color wrCol  = na(wr) ? #94A3B8 : wr >= 55 ? colBull : wr <= 42 ? colBear : color.white
 
    table.cell(hud, 0, row, array.get(sigNames, idx), text_color = bull ? colBull : colBear, bgcolor = bg, text_size = size.small, text_halign = text.align_left)
    table.cell(hud, 1, row, str.tostring(int(n)), text_color = color.white, bgcolor = bg, text_size = size.small, text_halign = text.align_center)
    table.cell(hud, 2, row, wrStr, text_color = wrCol, bgcolor = bg, text_size = size.small, text_halign = text.align_center)
 
if showHud and barstate.islast
    color bgHdr = color.new(#1E222D, 0)
    color bgA   = color.new(#1E222D, 35)
    color bgB   = color.new(#131722, 35)
 
    table.cell(hud, 0, 0, "RSI MOMENTUM STATS PRO", text_color = color.white, bgcolor = bgHdr, text_size = size.small, text_halign = text.align_center)
    table.merge_cells(hud, 0, 0, 2, 0)
 
    table.cell(hud, 0, 1, "Setup Type", text_color = #94A3B8, bgcolor = bgB, text_size = size.small, text_halign = text.align_left)
    table.cell(hud, 1, 1, "Graded", text_color = #94A3B8, bgcolor = bgB, text_size = size.small, text_halign = text.align_center)
    table.cell(hud, 2, 1, "Win Rate", text_color = #94A3B8, bgcolor = bgB, text_size = size.small, text_halign = text.align_center)
 
    for i = 0 to 5
        f_renderRow(i + 2, i, i % 2 == 0 ? bgA : bgB)
 
    string biasStr = bullTrend ? "BULLISH (> 200 EMA)" : "BEARISH (< 200 EMA)"
    table.cell(hud, 0, 8, "Macro Bias", text_color = #94A3B8, bgcolor = bgA, text_size = size.small, text_halign = text.align_left)
    table.cell(hud, 1, 8, biasStr, text_color = bullTrend ? colBull : colBear, bgcolor = bgA, text_size = size.small, text_halign = text.align_center)
    table.merge_cells(hud, 1, 8, 2, 8)
 
    string cfgStr = str.tostring(tgtAtr) + " / " + str.tostring(stpAtr) + " ATR | " + str.tostring(horizon) + " bars"
    table.cell(hud, 0, 9, "Grading R:R", text_color = #94A3B8, bgcolor = bgB, text_size = size.small, text_halign = text.align_left)
    table.cell(hud, 1, 9, cfgStr, text_color = color.white, bgcolor = bgB, text_size = size.small, text_halign = text.align_center)
    table.merge_cells(hud, 1, 9, 2, 9)
 
// ======== ALERTS ========
alertcondition(sig0, "Bullish Trend Pullback", "RSI Stats Pro: Bullish Trend Pullback on {{ticker}}")
alertcondition(sig1, "Bearish Trend Pullback", "RSI Stats Pro: Bearish Trend Pullback on {{ticker}}")
alertcondition(sig2, "Oversold Exit Buy",      "RSI Stats Pro: Oversold Exit on {{ticker}}")
alertcondition(sig3, "Overbought Exit Sell",   "RSI Stats Pro: Overbought Rejection on {{ticker}}")

← 返回列表