Range Enjoyer

CryptoCurrentlyYT · study · 141 行 · 点赞 146 · 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/
// © CryptoCurrentlyYT
//
// Range Enjoyer
// Tracks the latest swing high and swing low and tags what price does at them:
//   BO  = close above a swing high          FBO = wick above a swing high, close back below
//   BD  = close below a swing low           FBD = wick below a swing low, close back above
// After every event, the level is watched for the review window. A close back through the
// level undoes the event (BO ↔ FBO, BD ↔ FBD): the old tag is removed and the new one goes
// on the reversal candle. All signals are evaluated on bar close.
 
//@version=6
indicator("Range Enjoyer", shorttitle = "Range Enjoyer", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ─────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────
grpL = "Swings"
swingLen  = input.int(4, "Swing lookback (left bars)", minval = 1, group = grpL, tooltip = "A swing high must be the highest wick of this many bars before it (lows work the same way). Higher = more significant levels.")
swingConf = input.int(2, "Swing confirmation (right bars)", minval = 1, group = grpL, tooltip = "Bars after the swing that must stay below it (highs) or above it (lows) before it becomes a level. Lower = levels appear sooner and quick sweeps get caught, with more levels overall.")
 
grpS = "Signals"
bufPct     = input.float(0.0, "Close buffer %", minval = 0, step = 0.25, group = grpS, tooltip = "Every change of state needs a close beyond the level by this %: breakouts, breakdowns, and undoing them. Closes inside the buffer change nothing.")
reviewBars = input.int(4, "Review window (bars)", minval = 1, group = grpS, tooltip = "After every event, the level is watched for this many bars. A decisive close back through the level undoes the event (BO ↔ FBO, BD ↔ FBD): the old tag is removed, the new one goes on the reversal candle, and the window restarts. If a full window passes with no change, the tag is final.")
 
grpV = "Display"
showLevels = input.bool(true, "Show swing levels", group = grpV)
fullText   = input.bool(false, "Full tag text (off = short codes)", group = grpV)
tagSize    = input.string(size.small, "Tag size", options = [size.tiny, size.small, size.normal, size.large], group = grpV)
colLevel   = input.color(color.new(color.white, 40), "Active level", group = grpV)
colBO      = input.color(#4CAF50, "Breakout", group = grpV)
colFBO     = input.color(#FFEB3B, "False breakout", group = grpV)
colBD      = input.color(#F23645, "Breakdown", group = grpV)
colFBD     = input.color(#2962FF, "False breakdown", group = grpV)
 
txtBO  = fullText ? "Breakout"        : "BO"
txtFBO = fullText ? "False Breakout"  : "FBO"
txtBD  = fullText ? "Breakdown"       : "BD"
txtFBD = fullText ? "False Breakdown" : "FBD"
 
// ─────────────────────────────────────────────────────────────
// Levels
// ─────────────────────────────────────────────────────────────
type Lvl
    int   dir          // 1 = swing high, -1 = swing low
    float price
    int   state = 0    // 0 = untouched, 1 = BO/BD, 2 = FBO/FBD
    int   evBar = 0    // bar of the latest event (window start)
    label tag
    line  ln
 
var watched = array.new<Lvl>()
var Lvl actHi = na
var Lvl actLo = na
 
// Decisive closes beyond a level
above(float p) => close > p * (1 + bufPct / 100)
below(float p) => close < p * (1 - bufPct / 100)
 
// Record an event: remove the level's old tag, put the new one on this candle, restart the window
setEvent(Lvl l, bool isBreak) =>
    string ps = str.tostring(l.price, format.mintick)
    if not na(l.tag)
        label.delete(l.tag)
    if l.dir == 1
        string m = isBreak ? "Breakout: closed above " + ps : "False breakout: closed below " + ps
        l.tag := label.new(bar_index, high, isBreak ? txtBO : txtFBO, style = label.style_label_down, color = isBreak ? colBO : colFBO, textcolor = color.black, size = tagSize, tooltip = m)
        alert(m, alert.freq_once_per_bar_close)
    else
        string m = isBreak ? "Breakdown: closed below " + ps : "False breakdown: closed above " + ps
        l.tag := label.new(bar_index, low, isBreak ? txtBD : txtFBD, style = label.style_label_up, color = isBreak ? colBD : colFBD, textcolor = color.white, size = tagSize, tooltip = m)
        alert(m, alert.freq_once_per_bar_close)
    l.evBar := bar_index
    l.state := isBreak ? 1 : 2
    if not na(l.ln)
        l.ln.set_x2(bar_index)
        l.ln.set_style(line.style_solid)
        l.ln.set_color(l.dir == 1 ? (isBreak ? colBO : colFBO) : (isBreak ? colBD : colFBD))
    true
 
ph = ta.pivothigh(high, swingLen, swingConf)
pl = ta.pivotlow(low, swingLen, swingConf)
 
if barstate.isconfirmed
    // ── 1. Watch levels that already had an event
    if watched.size() > 0
        for i = watched.size() - 1 to 0
            Lvl l = watched.get(i)
            if l.dir == 1
                if l.state == 1 and below(l.price)
                    setEvent(l, false)                                   // BO  → FBO
                else if l.state == 2 and above(l.price)
                    setEvent(l, true)                                    // FBO → BO
            else
                if l.state == 1 and above(l.price)
                    setEvent(l, false)                                   // BD  → FBD
                else if l.state == 2 and below(l.price)
                    setEvent(l, true)                                    // FBD → BD
            // A full window passed since the latest event: tag is final
            if bar_index - l.evBar >= reviewBars
                watched.remove(i)
 
    // ── 2. New swings replace the previous untouched ones
    if not na(ph)
        if not na(actHi) and not na(actHi.ln)
            line.delete(actHi.ln)
        actHi := Lvl.new(dir = 1, price = ph, ln = showLevels ? line.new(bar_index - swingConf, ph, bar_index, ph, color = colLevel, style = line.style_dotted, width = 1) : na)
    if not na(pl)
        if not na(actLo) and not na(actLo.ln)
            line.delete(actLo.ln)
        actLo := Lvl.new(dir = -1, price = pl, ln = showLevels ? line.new(bar_index - swingConf, pl, bar_index, pl, color = colLevel, style = line.style_dotted, width = 1) : na)
 
    // ── 3. First event on the untouched swing high
    if not na(actHi)
        if above(actHi.price)
            setEvent(actHi, true)                                        // BO
            watched.push(actHi)
            actHi := na
        else if high > actHi.price and close <= actHi.price
            setEvent(actHi, false)                                       // FBO (wick above, closed back below)
            watched.push(actHi)
            actHi := na
 
    // ── 4. First event on the untouched swing low
    if not na(actLo)
        if below(actLo.price)
            setEvent(actLo, true)                                        // BD
            watched.push(actLo)
            actLo := na
        else if low < actLo.price and close >= actLo.price
            setEvent(actLo, false)                                       // FBD (wick below, closed back above)
            watched.push(actLo)
            actLo := na
 
// ─────────────────────────────────────────────────────────────
// Keep untouched levels extending to the current bar
// ─────────────────────────────────────────────────────────────
if not na(actHi) and not na(actHi.ln)
    actHi.ln.set_x2(bar_index)
if not na(actLo) and not na(actLo.ln)
    actLo.ln.set_x2(bar_index)

← 返回列表