Ahmed BSL SSL Rebound Map

Ahmedaminnn · study · 230 行 · 点赞 27 · TradingView 原页

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

Pine Script

//@version=6
// Ahmed Amin — BSL SSL Rebound Map, research release 2.1 (compact English boxes).
// Independent implementation of confirmed pivots, local confirmation and level targets.
// Informational indicator, not order execution. No automatic partial exits.
indicator("Ahmed BSL SSL Rebound Map", overlay=true, max_labels_count=300, max_lines_count=20)
left = input.int(20, "Pivot left bars", minval=1, maxval=200)
right = input.int(3, "Pivot confirmation bars", minval=1, maxval=50)
waitBars = input.int(5, "Local confirmation window", minval=1, maxval=50)
age = input.int(500, "Historical level age", minval=20, maxval=5000)
pad = input.float(0.2, "SL buffer ATR", minval=0, step=0.05)
front = input.float(0.05, "TP before level ATR", minval=0, step=0.01)
separation = input.float(0.25, "Minimum target separation ATR", minval=0.01)
minNetR = input.float(0.5, "Minimum net TP1 / risk", minval=0.1, step=0.1)
cost = input.float(0.60, "Assumed round-trip price cost", minval=0)
previewOn = input.bool(true, "Show pending plan before signal")
showSwings = input.bool(true, "Show confirmed swing labels")
showZones = input.bool(true, "Show latest BSL / SSL lines")
boxGap = input.int(12, "Plan box spacing (bars)", minval=5, maxval=100)
planText = input.string("tiny", "Plan text size", options=["tiny", "small", "normal"])
showTargetLines = input.bool(false, "Show target and stop lines")
volAverage = ta.sma(volume[1], 20)
var label bslBox = na
var label sslBox = na
var int bslBoxBar = na
var int sslBoxBar = na
var float planAnchor = na
var int planDirection = 0
atr = ta.atr(14)
ph = ta.pivothigh(high, left, right)
pl = ta.pivotlow(low, left, right)
var prices = array.new_float()
var bars = array.new_int()
var float bsl = na
var float ssl = na
var float bvol = na
var float svol = na
var int pending = 0
var int pendingBar = na
var float extreme = na
var int side = 0
var int planBar = na
var float entry = na
var float stop = na
var float tp1 = na
var float tp2 = na
var float tp3 = na
var bool hit1 = false
var bool hit2 = false
var bool hit3 = false
var string status = "Waiting for a confirmed pivot"
var bool confirmedPlan = false
bool buyEvent = false
bool sellEvent = false
bool stopEvent = false
bool tp1Event = false
bool tp2Event = false
bool tp3Event = false
bool endEvent = false
bool ambiguousEvent = false
px(float v) => na(v) ? "Unavailable" : str.tostring(v, format.mintick)
volumeBox(string name, float price, float v, float previous, float average) =>
    string change = not na(previous) and previous > 0 ? str.tostring(100 * (v / previous - 1), "+#.00;-#.00") + "%" : "N/A"
    string relative = not na(average) and average > 0 ? str.tostring(v / average, "#.00") + "x" : "N/A"
    name + ": " + px(price) + "\nConfirmed pivot\nVolume: " + str.tostring(v, format.volume) + "\nPrevious: " + str.tostring(previous, format.volume) + "\nChange: " + change + "\nPrior average: " + str.tostring(average, format.volume) + "\nRelative volume: " + relative
targets(int d, float e, float a) =>
    float n1 = na
    float n2 = na
    float n3 = na
    if array.size(prices) > 0
        for j = 0 to array.size(prices) - 1
            float dist = d * (array.get(prices, j) - e)
            if dist > front * a + syminfo.mintick and (na(n1) or dist < n1)
                n1 := dist
        if not na(n1)
            for j = 0 to array.size(prices) - 1
                float dist = d * (array.get(prices, j) - e)
                if dist >= n1 + separation * a and (na(n2) or dist < n2)
                    n2 := dist
        if not na(n2)
            for j = 0 to array.size(prices) - 1
                float dist = d * (array.get(prices, j) - e)
                if dist >= n2 + separation * a and (na(n3) or dist < n3)
                    n3 := dist
    [math.round_to_mintick(e + d * (n1 - front * a)), math.round_to_mintick(e + d * (n2 - front * a)), math.round_to_mintick(e + d * (n3 - front * a))]
stopPrice(int d, float p, float a) =>
    float v = p - d * math.max(pad * a, syminfo.mintick)
    d == 1 ? math.floor(v / syminfo.mintick) * syminfo.mintick : math.ceil(v / syminfo.mintick) * syminfo.mintick
if barstate.isconfirmed
    // Monitor from the bar AFTER the signal. OHLC cannot resolve intrabar ordering.
    if side != 0
        bool hs = side == 1 ? low <= stop : high >= stop
        bool h1 = not na(tp1) and (side == 1 ? high >= tp1 : low <= tp1)
        bool h2 = not na(tp2) and (side == 1 ? high >= tp2 : low <= tp2)
        bool h3 = not na(tp3) and (side == 1 ? high >= tp3 : low <= tp3)
        ambiguousEvent := hs and ((h1 and not hit1) or (h2 and not hit2) or (h3 and not hit3))
        if hs
            stopEvent := true
            endEvent := true
            status := ambiguousEvent ? "Ended: SL/TP order unknown" : "Ended: stop touched"
            side := 0
        else
            tp1Event := h1 and not hit1
            tp2Event := h2 and not hit2
            tp3Event := h3 and not hit3
            hit1 := hit1 or h1
            hit2 := hit2 or h2
            hit3 := hit3 or h3
            status := hit3 ? "TP3 touched" : hit2 ? "TP2 touched" : hit1 ? "TP1 touched" : "Confirmed plan"
            bool lastHit = not na(tp3) ? hit3 : not na(tp2) ? hit2 : hit1
            if lastHit
                endEvent := true
                side := 0
                status := "Ended: last available target touched"
    if not na(ph)
        bslBoxBar := bar_index
        if showSwings
            label.delete(bslBox)
            bslBox := label.new(bar_index, ph, volumeBox("BSL", ph, volume[right], bvol, volAverage[right]), style=label.style_label_right, color=color.new(color.green, 15), textcolor=color.white, size=size.small, tooltip="Shown on confirmation bar, not the historical pivot bar.")
        bsl := ph
        bvol := volume[right]
        array.push(prices, ph)
        array.push(bars, bar_index - right)
    if not na(pl)
        sslBoxBar := bar_index
        if showSwings
            label.delete(sslBox)
            sslBox := label.new(bar_index, pl, volumeBox("SSL", pl, volume[right], svol, volAverage[right]), style=label.style_label_right, color=color.new(color.red, 15), textcolor=color.white, size=size.small, tooltip="Shown on confirmation bar, not the historical pivot bar.")
        ssl := pl
        svol := volume[right]
        array.push(prices, pl)
        array.push(bars, bar_index - right)
    while array.size(bars) > 0
        if bar_index - array.get(bars, 0) > age
            array.shift(bars)
            array.shift(prices)
        else
            break
    if side == 0 and not endEvent
        bool highOnly = not na(ph) and na(pl)
        bool lowOnly = not na(pl) and na(ph)
        if highOnly or lowOnly
            pending := lowOnly ? 1 : -1
            pendingBar := bar_index
            extreme := lowOnly ? pl : ph
            planAnchor := extreme
            planDirection := pending
            confirmedPlan := false
            planBar := bar_index
            entry := close
            stop := stopPrice(pending, extreme, atr)
            [a, b, c] = targets(pending, entry, atr)
            tp1 := a
            tp2 := b
            tp3 := c
            hit1 := false
            hit2 := false
            hit3 := false
            status := "Pending: NOT an entry"
        if not na(ph) and not na(pl)
            pending := 0
            if not confirmedPlan
                status := "Cancelled: conflicting pivots"
        if pending != 0
            bool broken = pending == 1 ? low < extreme : high > extreme
            if broken or bar_index - pendingBar > waitBars
                pending := 0
                status := broken ? "Cancelled: pivot broken" : "Expired: no local confirmation"
            else
                // Local price confirmation, not a break of the distant opposite pivot.
                bool localOK = pending == 1 ? close > high[1] and close > open : close < low[1] and close < open
                if localOK and not na(atr)
                    float candidateStop = stopPrice(pending, extreme, atr)
                    float risk = pending * (close - candidateStop)
                    [a, b, c] = targets(pending, close, atr)
                    bool valid = risk >= syminfo.mintick and not na(a) and (pending * (a - close) - cost) / risk >= minNetR
                    if valid
                        side := pending
                        pending := 0
                        confirmedPlan := true
                        planBar := bar_index
                        entry := close
                        stop := candidateStop
                        tp1 := a
                        tp2 := b
                        tp3 := c
                        hit1 := false
                        hit2 := false
                        hit3 := false
                        buyEvent := side == 1
                        sellEvent := side == -1
                        status := "Confirmed plan"
                    else
                        status := "Pending: TP1 too close / unavailable"
plotshape(buyEvent, title="BUY", style=shape.labelup, location=location.belowbar, color=color.green, text="BUY", textcolor=color.white)
plotshape(sellEvent, title="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white)
plotshape(tp1Event, title="TP1 touched", style=shape.circle, location=location.abovebar, color=color.teal, text="TP1", size=size.tiny)
plotshape(tp2Event, title="TP2 touched", style=shape.circle, location=location.abovebar, color=color.green, text="TP2", size=size.tiny)
plotshape(tp3Event, title="TP3 touched", style=shape.circle, location=location.abovebar, color=color.lime, text="TP3", size=size.tiny)
plotshape(stopEvent, title="SL touched", style=shape.xcross, location=location.belowbar, color=color.red, text="SL", size=size.tiny)
plot(showZones ? bsl : na, "BSL", color.green, style=plot.style_stepline)
plot(showZones ? ssl : na, "SSL", color.red, style=plot.style_stepline)
var drawnLines = array.new_line()
var label planBox = na
if barstate.islast
    while array.size(drawnLines) > 0
        line.delete(array.pop(drawnLines))
    label.delete(planBox)
    if not na(planBar) and (confirmedPlan or previewOn)
        // Anchor beside the matching BSL/SSL box; do not move it to the chart corner.
        int zoneBar = planDirection == 1 ? sslBoxBar : bslBoxBar
        float zonePrice = planDirection == 1 ? ssl : bsl
        bool matching = not na(zonePrice) and math.abs(zonePrice - planAnchor) < syminfo.mintick / 2
        int x = math.max(matching and not na(zoneBar) ? zoneBar : planBar, bar_index - 500) + boxGap
        string heading = (planDirection == 1 ? "SSL / BUY" : "BSL / SELL") + (confirmedPlan ? " PLAN" : " PREVIEW")
        string content = heading + "\n" + status + "\n" + (confirmedPlan ? "Entry: " : "Reference: ") + px(entry) + "\nSL: " + px(stop) + "\nTP1: " + px(tp1) + (hit1 ? " (touched)" : "") + "\nTP2: " + px(tp2) + (hit2 ? " (touched)" : "") + "\nTP3: " + px(tp3) + (hit3 ? " (touched)" : "")
        if not matching
            content := content + "\nEarlier setup — level " + px(planAnchor)
        planBox := label.new(x, planAnchor, content, style=label.style_label_left, color=color.rgb(40, 47, 60), textcolor=color.white, size=planText == "tiny" ? size.tiny : planText == "small" ? size.small : size.normal, textalign=text.align_left)
        if showTargetLines
            for j = 0 to 3
                float v = j == 0 ? stop : j == 1 ? tp1 : j == 2 ? tp2 : tp3
                color col = j == 0 ? color.red : color.teal
                if not na(v)
                    array.push(drawnLines, line.new(math.max(planBar, bar_index - 500), v, bar_index + 5, v, color=col, width=1, style=confirmedPlan ? line.style_solid : line.style_dashed))
alertcondition(buyEvent, "BUY confirmed", "BUY local rebound confirmed: {{ticker}} {{interval}}")
alertcondition(sellEvent, "SELL confirmed", "SELL local rebound confirmed: {{ticker}} {{interval}}")
alertcondition(tp1Event, "TP1 touched", "TP1 touched: {{ticker}}")
alertcondition(tp2Event, "TP2 touched", "TP2 touched: {{ticker}}")
alertcondition(tp3Event, "TP3 touched", "TP3 touched: {{ticker}}")
alertcondition(stopEvent, "SL touched", "SL touched: {{ticker}}; check ambiguous candle ordering")

← 返回列表