SWEEP Target Hit + Candle Trend | Albert Setups

AlbertSetups · study · 110 行 · 点赞 29 · 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/
// © AlbertSetups
 
 
 //@version=6
indicator("SWEEP Target Hit + Candle Trend | Albert Setups", overlay=true, max_labels_count=200, max_lines_count=100)
 
// ───── INPUTS ─────
swingLen = input.int(5, "Liquidity Swing Length", minval=2)
fastLen = input.int(21, "Fast EMA", minval=1)
slowLen = input.int(50, "Slow EMA", minval=1)
atrLen = input.int(14, "ATR Length", minval=1)
targetATR = input.float(2.0, "Target Distance (ATR)", minval=0.5, step=0.5)
showEMA = input.bool(true, "Show Trend EMAs")
showSweep = input.bool(true, "Show Sweep Signals")
showTargets = input.bool(true, "Show Target Levels")
showCandleTrend = input.bool(true, "Color Trend Candles")
 
// ───── TREND ─────
emaFast = ta.ema(close, fastLen)
emaSlow = ta.ema(close, slowLen)
atrValue = ta.atr(atrLen)
 
bullTrend = emaFast > emaSlow and close > emaSlow
bearTrend = emaFast < emaSlow and close < emaSlow
 
plot(showEMA ? emaFast : na, "Fast EMA", color=color.aqua, linewidth=2)
plot(showEMA ? emaSlow : na, "Slow EMA", color=color.orange, linewidth=2)
 
barcolor(showCandleTrend ? bullTrend ? color.lime : bearTrend ? color.red : color.gray : na)
 
// ───── LIQUIDITY LEVELS ─────
pivotHigh = ta.pivothigh(high, swingLen, swingLen)
pivotLow = ta.pivotlow(low, swingLen, swingLen)
 
var float lastHigh = na
var float lastLow = na
var bool highSwept = false
var bool lowSwept = false
 
if not na(pivotHigh)
    lastHigh := pivotHigh
    highSwept := false
 
if not na(pivotLow)
    lastLow := pivotLow
    lowSwept := false
 
// ───── SWEEP DETECTION ─────
bearSweep = not na(lastHigh) and high > lastHigh and close < lastHigh and not highSwept
bullSweep = not na(lastLow) and low < lastLow and close > lastLow and not lowSwept
 
if bearSweep
    highSwept := true
 
if bullSweep
    lowSwept := true
 
buySignal = bullSweep and bullTrend and close > open
sellSignal = bearSweep and bearTrend and close < open
 
plotshape(showSweep and buySignal, title="BUY Sweep", text="BUY SWEEP", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.small)
plotshape(showSweep and sellSignal, title="SELL Sweep", text="SELL SWEEP", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.small)
 
// ───── TARGET TRACKING ─────
var float activeTarget = na
var int activeDirection = 0
var bool targetHit = false
var line targetLine = na
var label targetLabel = na
 
newBuy = buySignal and barstate.isconfirmed
newSell = sellSignal and barstate.isconfirmed
 
if newBuy
    activeDirection := 1
    activeTarget := close + atrValue * targetATR
    targetHit := false
    if not na(targetLine)
        line.delete(targetLine)
    if not na(targetLabel)
        label.delete(targetLabel)
    if showTargets
        targetLine := line.new(bar_index, activeTarget, bar_index + 30, activeTarget, color=color.lime, width=2, style=line.style_dashed)
        targetLabel := label.new(bar_index + 30, activeTarget, "BUY TARGET", style=label.style_label_left, color=color.green, textcolor=color.white)
 
if newSell
    activeDirection := -1
    activeTarget := close - atrValue * targetATR
    targetHit := false
    if not na(targetLine)
        line.delete(targetLine)
    if not na(targetLabel)
        label.delete(targetLabel)
    if showTargets
        targetLine := line.new(bar_index, activeTarget, bar_index + 30, activeTarget, color=color.red, width=2, style=line.style_dashed)
        targetLabel := label.new(bar_index + 30, activeTarget, "SELL TARGET", style=label.style_label_left, color=color.red, textcolor=color.white)
 
hitBuyTarget = activeDirection == 1 and not targetHit and not na(activeTarget) and high >= activeTarget
hitSellTarget = activeDirection == -1 and not targetHit and not na(activeTarget) and low <= activeTarget
targetHitNow = hitBuyTarget or hitSellTarget
 
if targetHitNow
    targetHit := true
    label.new(bar_index, activeTarget, "TARGET HIT ✓", style=activeDirection == 1 ? label.style_label_down : label.style_label_up, color=color.blue, textcolor=color.white, size=size.normal)
 
// ───── ALERTS ─────
alertcondition(newBuy, title="Bullish Sweep BUY", message="Bullish liquidity sweep BUY on {{ticker}} at {{close}}")
alertcondition(newSell, title="Bearish Sweep SELL", message="Bearish liquidity sweep SELL on {{ticker}} at {{close}}")
alertcondition(targetHitNow, title="Target Hit", message="The active sweep target has been reached on {{ticker}}")

← 返回列表