Liquidity Sweep Engulfing + Trade Manager

tradingone932 · study · 256 行 · 点赞 59 · TradingView 原页

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

Pine Script

//@version=6
indicator("Liquidity Sweep Engulfing + Trade Manager", overlay=true, max_boxes_count=500, max_labels_count=500)
 
//====================================================
// INPUTS
//====================================================
 
// Candle Settings
groupCandle = "Candle Conditions"
 
candleMode = input.string(
     "Mixed",
     "Previous Candle Direction",
     options=["Same Direction", "Mixed"],
     group=groupCandle)
 
// Trend Filter
groupTrend = "Trend Filter"
 
useTrend = input.bool(true, "Enable EMA Trend Filter", group=groupTrend)
emaLength = input.int(200, "EMA Length", minval=1, group=groupTrend)
 
// Risk Management
groupRisk = "Risk Management"
 
atrLength = input.int(14, "ATR Length", minval=1, group=groupRisk)
 
slMode = input.string(
     "ATR",
     "Stop Loss Method",
     options=["ATR", "Signal Candle High/Low"],
     group=groupRisk)
 
slAtrMult = input.float(1.5, "Stop Loss ATR Multiplier", minval=0.1, step=0.1, group=groupRisk)
 
rrRatio = input.float(2.0, "Risk / Reward Ratio", minval=0.1, step=0.1, group=groupRisk)
 
tpAtrMult = input.float(3.0, "Take Profit ATR Multiplier", minval=0.1, step=0.1, group=groupRisk)
 
// Display
groupDisplay = "Display Settings"
 
showSignals = input.bool(true, "Show Buy/Sell Signals", group=groupDisplay)
showTradeBoxes = input.bool(true, "Show TP/SL Boxes", group=groupDisplay)
initialBars = input.int(5, "Initial Box Extension (Bars)", minval=1, maxval=100, group=groupDisplay)
 
//====================================================
// EMA TREND FILTER
//====================================================
 
emaValue = ta.ema(close, emaLength)
 
emaColor = close >= emaValue ? color.lime : color.red
 
plot(useTrend ? emaValue : na, "Trend EMA", color=emaColor, linewidth=2)
 
bullTrend = not useTrend or close > emaValue
bearTrend = not useTrend or close < emaValue
 
//====================================================
// CANDLE CONDITIONS
//====================================================
 
bullishPrevious = close[1] > open[1]
bearishPrevious = close[1] < open[1]
 
bullishCurrent = close > open
bearishCurrent = close < open
 
bullPreviousAllowed = candleMode == "Mixed" or bullishPrevious
bearPreviousAllowed = candleMode == "Mixed" or bearishPrevious
 
// Liquidity sweep + engulfing
 
bullishSweep = low < low[1] and close > high[1] and bullishCurrent
 
bearishSweep = high > high[1] and close < low[1] and bearishCurrent
 
//====================================================
// ATR
//====================================================
 
atrValue = ta.atr(atrLength)
 
//====================================================
// TRADE MANAGEMENT VARIABLES
//====================================================
 
var bool tradeActive = false
var int tradeDirection = 0
 
var float entryPrice = na
var float stopPrice = na
var float targetPrice = na
 
var int entryBar = na
 
var box stopBox = na
var box targetBox = na
 
//====================================================
// EXIT CONDITIONS
//====================================================
 
bool exitThisBar = false
bool stopHit = false
bool targetHit = false
 
if tradeActive and bar_index > entryBar
 
    stopHit := tradeDirection == 1 ? low <= stopPrice : high >= stopPrice
 
    targetHit := tradeDirection == 1 ? high >= targetPrice : low <= targetPrice
 
    // Conservative handling if both levels are touched
    // on the same candle: stop loss takes priority.
 
    if stopHit or targetHit
 
        exitThisBar := true
 
        if stopHit
            label.new(
                 bar_index,
                 stopPrice,
                 "SL",
                 style=tradeDirection == 1 ? label.style_label_down : label.style_label_up,
                 color=color.red,
                 textcolor=color.white,
                 size=size.tiny)
 
        else
            label.new(
                 bar_index,
                 targetPrice,
                 "TP",
                 style=tradeDirection == 1 ? label.style_label_down : label.style_label_up,
                 color=color.green,
                 textcolor=color.white,
                 size=size.tiny)
 
        tradeActive := false
 
        if showTradeBoxes
            box.set_right(stopBox, bar_index)
            box.set_right(targetBox, bar_index)
 
//====================================================
// SIGNAL CONDITIONS
//====================================================
 
buySignal = bullishSweep and bullPreviousAllowed and bullTrend and barstate.isconfirmed
 
sellSignal = bearishSweep and bearPreviousAllowed and bearTrend and barstate.isconfirmed
 
//====================================================
// CREATE NEW TRADE
//====================================================
 
if not tradeActive and not exitThisBar and barstate.isconfirmed and not na(atrValue)
 
    if buySignal or sellSignal
 
        tradeDirection := buySignal ? 1 : -1
 
        entryPrice := close
        entryBar := bar_index
 
        float riskDistance = slMode == "ATR" ? atrValue * slAtrMult : tradeDirection == 1 ? entryPrice - low : high - entryPrice
 
        // Reject invalid risk distances
        if riskDistance > 0
 
            stopPrice := tradeDirection == 1 ? entryPrice - riskDistance : entryPrice + riskDistance
 
            float rewardDistance = slMode == "ATR" ? atrValue * tpAtrMult : riskDistance * rrRatio
 
            // TP is ATR-based in ATR mode.
            // Signal candle mode uses the selected R/R ratio.
 
            targetPrice := tradeDirection == 1 ? entryPrice + rewardDistance : entryPrice - rewardDistance
 
            tradeActive := true
 
            // Create trade boxes
 
            if showTradeBoxes
 
                float stopTop = math.max(entryPrice, stopPrice)
                float stopBottom = math.min(entryPrice, stopPrice)
 
                float targetTop = math.max(entryPrice, targetPrice)
                float targetBottom = math.min(entryPrice, targetPrice)
 
                stopBox := box.new(
                     left=bar_index,
                     top=stopTop,
                     right=bar_index + initialBars,
                     bottom=stopBottom,
                     bgcolor=color.new(color.red, 80),
                     border_color=color.new(color.red, 20))
 
                targetBox := box.new(
                     left=bar_index,
                     top=targetTop,
                     right=bar_index + initialBars,
                     bottom=targetBottom,
                     bgcolor=color.new(color.green, 80),
                     border_color=color.new(color.green, 20))
 
//====================================================
// EXTEND BOXES WHILE TRADE IS ACTIVE
//====================================================
 
if tradeActive and showTradeBoxes
 
    box.set_right(stopBox, bar_index + initialBars)
    box.set_right(targetBox, bar_index + initialBars)
 
//====================================================
// BUY / SELL SIGNALS
//====================================================
 
plotshape(
     showSignals and buySignal and not tradeActive[1] and not exitThisBar,
     title="BUY",
     style=shape.labelup,
     location=location.belowbar,
     color=color.green,
     text="BUY",
     textcolor=color.white,
     size=size.small)
 
plotshape(
     showSignals and sellSignal and not tradeActive[1] and not exitThisBar,
     title="SELL",
     style=shape.labeldown,
     location=location.abovebar,
     color=color.red,
     text="SELL",
     textcolor=color.white,
     size=size.small)
 
//====================================================
// ALERTS
//====================================================
 
alertcondition(
     buySignal and not tradeActive[1] and not exitThisBar,
     title="Bullish Liquidity Sweep",
     message="Bullish Liquidity Sweep Engulfing Signal")
 
alertcondition(
     sellSignal and not tradeActive[1] and not exitThisBar,
     title="Bearish Liquidity Sweep",
     message="Bearish Liquidity Sweep Engulfing Signal")

← 返回列表