RSI Price Ladder [Quantum Algo]

Quantum-Algo · study · 185 行 · 点赞 84 · TradingView 原页

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

Pine Script

//@version=6
indicator("RSI Price Ladder [Quantum Algo]", overlay = true, max_lines_count = 100, max_labels_count = 100, max_boxes_count = 50)
 
// ════════════════════════════════════════════════════════════════
//  RSI PRICE LADDER
//  The Relative Strength Index, solved for price. Instead of
//  reading RSI in a separate pane, this script answers the
//  question a trader actually has while looking at the candles:
//  "at what price does RSI reach 70, 50 or 30?" The RSI formula
//  is inverted on every bar and the answers are drawn on the
//  price chart — as a corridor through history and as a ladder
//  beside the current bar, a price-mapped RSI scale with a
//  pointer at the live reading. A behaviour record then measures
//  what this symbol has actually done after entering the
//  overbought and oversold zones.
//
//  Concept credits: the Relative Strength Index and the Average
//  True Range by J. Welles Wilder Jr. (New Concepts in Technical
//  Trading Systems, 1978) · inverting the RSI formula for price
//  by Giorgos E. Siligardos ("Reverse Engineering RSI", Technical
//  Analysis of Stocks & Commodities, 2003) · plotting RSI levels
//  as price bands by Francois Bertrand ("Visualizing RSI",
//  Technical Analysis of Stocks & Commodities, 2008) · additive
//  (Laplace) smoothing from classical statistics. The ladder
//  scale, gradient corridor, distance readouts, zone-behaviour
//  record and all code are original work. No third-party or
//  open-source script code was reused.
// ════════════════════════════════════════════════════════════════
 
// ─── INPUTS ─────────────────────────────────────────────────────
grpR = "RSI"
rsiLen = input.int(14, "Length", minval = 2, maxval = 200, group = grpR)
src    = input.source(close, "Source", group = grpR)
obL    = input.int(70, "Overbought Level", minval = 55, maxval = 90, group = grpR)
osL    = input.int(30, "Oversold Level", minval = 10, maxval = 45, group = grpR)
 
grpL = "Ladder"
showLadder = input.bool(true, "Show Ladder", group = grpL, tooltip = "A price-mapped RSI scale beside the current bar. Each rung is the price at which this bar must close for RSI to read that level. The pointer shows the live RSI at the current price.")
showMidR   = input.bool(true, "Show 40 And 60 Rungs", group = grpL)
ladOff     = input.int(8, "Ladder Offset (Bars)", minval = 3, maxval = 60, group = grpL)
ladW       = input.int(2, "Ladder Width (Bars)", minval = 1, maxval = 6, group = grpL)
 
grpC = "Corridor"
showBands = input.bool(true, "Show RSI Corridor", group = grpC, tooltip = "The overbought and oversold levels drawn as price lines through history, with the RSI 50 price between them. A close above the upper line is a close with RSI above the overbought level, by construction.")
showMid   = input.bool(true, "Show RSI 50 Line", group = grpC)
 
grpS = "Zone Behaviour Record"
horizon   = input.int(10, "Look Ahead (Bars)", minval = 3, maxval = 50, group = grpS, tooltip = "After RSI enters a zone, the record checks where price is this many bars later.")
capN      = input.int(300, "Samples To Keep Per Zone", minval = 30, maxval = 1000, group = grpS)
showMarks = input.bool(true, "Mark Zone Entries", group = grpS)
 
grpV = "Visuals"
lowCol  = input.color(#00897b, "Oversold Side", group = grpV)
highCol = input.color(#e4002b, "Overbought Side", group = grpV)
neutCol = input.color(#37474f, "Neutral", group = grpV)
 
grpT = "Readout Strip"
showStrip = input.bool(true, "Show Readout Strip", group = grpT)
stripPosS = input.string("Top Center", "Position", options = ["Top Center", "Bottom Center", "Top Right", "Bottom Right"], group = grpT)
stripSzS  = input.string("Small", "Text Size", options = ["Tiny", "Small", "Normal"], group = grpT)
 
// ─── RSI, COMPUTED FROM ITS COMPONENTS ─────────────────────────
chg = ta.change(src)
up  = ta.rma(math.max(chg, 0.0), rsiLen)
dn  = ta.rma(math.max(-chg, 0.0), rsiLen)
rsi = dn == 0 ? 100.0 : up == 0 ? 0.0 : 100.0 - 100.0 / (1.0 + up / dn)
atrV = ta.atr(14)
 
// ─── THE INVERSION · price at which this bar closes with RSI = L ─
upPrev = up[1]
dnPrev = dn[1]
base   = src[1]
f_lvl(float L) =>
    rs = L / (100.0 - L)
    x = (rsiLen - 1) * (rs * dnPrev - upPrev)
    x >= 0 ? base + x : base - (rsiLen - 1) * (upPrev / rs - dnPrev)
 
obX = math.min(obL + 10, 95)
osX = math.max(osL - 10, 5)
pxOB  = f_lvl(obL)
pxOS  = f_lvl(osL)
pxMid = f_lvl(50.0)
 
// ─── CORRIDOR ──────────────────────────────────────────────────
pU = plot(showBands ? pxOB : na, "Overbought Price", color = color.new(highCol, 15), linewidth = 2)
pM = plot(showBands ? pxMid : na, "RSI 50 Price", color = showMid ? color.new(neutCol, 35) : color.new(neutCol, 100))
pL = plot(showBands ? pxOS : na, "Oversold Price", color = color.new(lowCol, 15), linewidth = 2)
fill(pU, pM, pxOB, pxMid, color.new(highCol, 72), color.new(highCol, 100), title = "Upper Corridor Glow")
fill(pM, pL, pxMid, pxOS, color.new(lowCol, 100), color.new(lowCol, 72), title = "Lower Corridor Glow")
 
// ─── ZONE BEHAVIOUR RECORD ─────────────────────────────────────
enterOB = barstate.isconfirmed and rsi >= obL and rsi[1] < obL
enterOS = barstate.isconfirmed and rsi <= osL and rsi[1] > osL
var array<float> obLower  = array.new<float>()   // 1 when price was lower after the look-ahead
var array<float> osHigher = array.new<float>()   // 1 when price was higher after the look-ahead
f_push(array<float> a, float v) =>
    a.push(v)
    if a.size() > capN
        a.shift()
    true
if barstate.isconfirmed and bar_index > horizon
    if enterOB[horizon]
        f_push(obLower, close < close[horizon] ? 1.0 : 0.0)
    if enterOS[horizon]
        f_push(osHigher, close > close[horizon] ? 1.0 : 0.0)
f_rate(array<float> a) =>
    n = a.size()
    n > 0 ? (a.sum() + 2.0) / (n + 4.0) : na
 
plotshape(showMarks and enterOB ? pxOB : na, "Entered Overbought", shape.diamond, location.absolute, color.new(highCol, 0), size = size.tiny)
plotshape(showMarks and enterOS ? pxOS : na, "Entered Oversold", shape.diamond, location.absolute, color.new(lowCol, 0), size = size.tiny)
 
// ─── LADDER · price-mapped RSI scale beside the current bar ────
f_grad(float L) => color.from_gradient(L, osX, obX, lowCol, highCol)
var array<box>   ladB = array.new<box>()
var array<line>  ladL = array.new<line>()
var array<label> ladT = array.new<label>()
while ladB.size() > 0
    box.delete(ladB.shift())
while ladL.size() > 0
    line.delete(ladL.shift())
while ladT.size() > 0
    label.delete(ladT.shift())
if barstate.islast and showLadder and not na(pxMid)
    lv = array.new<float>()
    lv.push(osX * 1.0)
    lv.push(osL * 1.0)
    if showMidR
        lv.push(40.0)
    lv.push(50.0)
    if showMidR
        lv.push(60.0)
    lv.push(obL * 1.0)
    lv.push(obX * 1.0)
    px = array.new<float>()
    for i = 0 to lv.size() - 1
        px.push(f_lvl(lv.get(i)))
    x0 = bar_index + ladOff
    x1 = x0 + ladW
    for i = 0 to lv.size() - 2
        pA = px.get(i)
        pB = px.get(i + 1)
        ladB.push(box.new(x0, math.max(pA, pB), x1, math.min(pA, pB), border_color = na, bgcolor = color.new(f_grad((lv.get(i) + lv.get(i + 1)) / 2), 20)))
    for i = 0 to lv.size() - 1
        L = lv.get(i)
        p = px.get(i)
        major = L == obL or L == osL or L == 50
        c = f_grad(L)
        ladL.push(line.new(x0 - 1, p, x1 + 1, p, color = color.new(c, 0), width = major ? 3 : 1))
        dPct = (p / close - 1) * 100
        dAtr = atrV > 0 ? (p - close) / atrV : na
        ladT.push(label.new(x1 + 2, p, "RSI " + str.tostring(L, "#") + "   " + str.tostring(p, format.mintick) + "   " + (dPct >= 0 ? "+" : "") + str.tostring(dPct, "#.##") + "%", style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.new(c, 0), size = major ? size.normal : size.small, tooltip = "This bar must close at " + str.tostring(p, format.mintick) + " for RSI(" + str.tostring(rsiLen) + ") to read " + str.tostring(L, "#") + ".\nDistance from the current price: " + (dPct >= 0 ? "+" : "") + str.tostring(dPct, "#.##") + "% · " + str.tostring(dAtr, "#.#") + " ATR"))
    ladT.push(label.new(x0 - 1, close, "RSI " + str.tostring(rsi, "#.#"), style = label.style_label_right, color = color.new(f_grad(rsi), 0), textcolor = color.white, size = size.normal, tooltip = "The live RSI reading, sitting at the current price on the price-mapped scale."))
 
// ─── READOUT STRIP ─────────────────────────────────────────────
stripPos = stripPosS == "Bottom Center" ? position.bottom_center : stripPosS == "Top Right" ? position.top_right : stripPosS == "Bottom Right" ? position.bottom_right : position.top_center
stripSz = stripSzS == "Tiny" ? size.tiny : stripSzS == "Normal" ? size.normal : size.small
var table st = table.new(stripPos, 5, 2, bgcolor = color.new(#1c2526, 0), frame_color = color.new(#90a4ae, 0), frame_width = 1, border_color = color.new(#90a4ae, 75), border_width = 1)
f_cell(int c, string head, string body, color bodyCol, string tip) =>
    table.cell(st, c, 0, head, text_color = color.new(#90a4ae, 0), text_size = size.tiny, text_halign = text.align_center)
    table.cell(st, c, 1, body, text_color = bodyCol, text_size = stripSz, text_halign = text.align_center, tooltip = tip)
    true
f_dist(float p) =>
    d = (p / close - 1) * 100
    (d >= 0 ? "+" : "") + str.tostring(d, "#.##") + "%  ·  " + str.tostring(atrV > 0 ? math.abs(p - close) / atrV : na, "#.#") + " ATR"
if showStrip and barstate.islast
    rOB = f_rate(obLower)
    rOS = f_rate(osHigher)
    nOB = obLower.size()
    nOS = osHigher.size()
    txtOB = nOB == 0 ? "no entries yet" : (rOB >= 0.58 ? "Fades" : rOB <= 0.42 ? "Runs" : "Mixed") + "  ·  lower " + str.tostring(rOB * 100, "#") + "% of " + str.tostring(nOB)
    txtOS = nOS == 0 ? "no entries yet" : (rOS >= 0.58 ? "Bounces" : rOS <= 0.42 ? "Slides" : "Mixed") + "  ·  higher " + str.tostring(rOS * 100, "#") + "% of " + str.tostring(nOS)
    f_cell(0, "RSI " + str.tostring(rsiLen), str.tostring(rsi, "#.#"), f_grad(rsi), "Current RSI reading.")
    f_cell(1, "CLOSE NEEDED FOR " + str.tostring(obL), str.tostring(pxOB, format.mintick) + "   " + f_dist(pxOB), color.new(#ef9a9a, 0), "The price at which this bar must close for RSI to read the overbought level, and how far that is from the current price.")
    f_cell(2, "CLOSE NEEDED FOR " + str.tostring(osL), str.tostring(pxOS, format.mintick) + "   " + f_dist(pxOS), color.new(#80cbc4, 0), "The price at which this bar must close for RSI to read the oversold level, and how far that is from the current price.")
    f_cell(3, "AFTER OVERBOUGHT, " + str.tostring(horizon) + " BARS LATER", txtOB, color.new(#eceff1, 0), "Of the past times RSI entered the overbought zone on this chart, the smoothed share where price was LOWER after the look-ahead. Fades = usually lower, Runs = usually higher, Mixed = no clear tendency. History of this chart only, not a forecast.")
    f_cell(4, "AFTER OVERSOLD, " + str.tostring(horizon) + " BARS LATER", txtOS, color.new(#eceff1, 0), "Of the past times RSI entered the oversold zone on this chart, the smoothed share where price was HIGHER after the look-ahead. Bounces = usually higher, Slides = usually lower, Mixed = no clear tendency. History of this chart only, not a forecast.")
 
// ─── ALERTS ────────────────────────────────────────────────────
alertcondition(enterOB, "RSI Entered Overbought", "RSI closed at or above the overbought level.")
alertcondition(enterOS, "RSI Entered Oversold", "RSI closed at or below the oversold level.")
xUp50 = ta.crossover(rsi, 50)
xDn50 = ta.crossunder(rsi, 50)
alertcondition(barstate.isconfirmed and xUp50, "RSI Crossed Above 50", "RSI closed above 50.")
alertcondition(barstate.isconfirmed and xDn50, "RSI Crossed Below 50", "RSI closed below 50.")

← 返回列表