Liquidity Trap Radar PRO [Thomas]

Thomas-Sterling · study · 252 行 · 点赞 1,603 · 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/
// © Thomas-Sterling
 
 
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
// © Thomas-Sterling
 
//@version=6
indicator("Liquidity Trap Radar PRO [Thomas]", overlay=true, max_lines_count=20, max_labels_count=50)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 01. INPUT SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
string G_SWEEP = "01. Liquidity Trap Settings"
int sweepLookback = input.int(20, "Liquidity Lookback", minval=5, group=G_SWEEP)
int cooldownBars = input.int(8, "Signal Cooldown", minval=0, group=G_SWEEP)
bool useTrendFilter = input.bool(false, "Use EMA Trend Filter", group=G_SWEEP)
bool useVolumeFilter = input.bool(false, "Use Volume Confirmation", group=G_SWEEP)
float volumeMultiplier = input.float(1.0, "Relative Volume Minimum", minval=0.1, step=0.1, group=G_SWEEP)
 
string G_TREND = "02. Trend Settings"
int emaLength = input.int(200, "Trend EMA Length", minval=1, group=G_TREND)
bool showEMA = input.bool(true, "Show Trend EMA", group=G_TREND)
 
string G_STRUCTURE = "03. Market Structure"
int pivotLength = input.int(5, "Swing Pivot Length", minval=2, group=G_STRUCTURE)
bool showStructure = input.bool(true, "Show BOS / CHoCH", group=G_STRUCTURE)
 
string G_RISK = "04. Risk Management"
int atrLength = input.int(14, "ATR Length", minval=1, group=G_RISK)
float atrBuffer = input.float(0.25, "SL ATR Buffer", minval=0.0, step=0.05, group=G_RISK)
float tp1RR = input.float(1.0, "TP1 Risk:Reward", minval=0.1, step=0.1, group=G_RISK)
float tp2RR = input.float(2.0, "TP2 Risk:Reward", minval=0.1, step=0.1, group=G_RISK)
int extendBars = input.int(20, "Target Line Length", minval=5, maxval=200, group=G_RISK)
 
string G_VISUAL = "05. Visual Settings"
bool showSignals = input.bool(true, "Show BUY / SELL Signals", group=G_VISUAL)
bool showLevels = input.bool(true, "Show Entry / SL / TP Levels", group=G_VISUAL)
bool showDashboard = input.bool(true, "Show Dashboard", group=G_VISUAL)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 02. CORE CALCULATIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
float emaTrend = ta.ema(close, emaLength)
float atrValue = ta.atr(atrLength)
float avgVolume = ta.sma(volume, 20)
 
float previousHigh = ta.highest(high, sweepLookback)[1]
float previousLow = ta.lowest(low, sweepLookback)[1]
 
bool volumeOK = not useVolumeFilter or (not na(volume) and not na(avgVolume) and volume >= avgVolume * volumeMultiplier)
 
bool trendBuyOK = not useTrendFilter or close > emaTrend
bool trendSellOK = not useTrendFilter or close < emaTrend
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 03. LIQUIDITY SWEEP / TRAP DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// Price breaks above the previous range and closes back below it.
bool upperSweep = not na(previousHigh) and high > previousHigh and close < previousHigh
 
// Price breaks below the previous range and closes back above it.
bool lowerSweep = not na(previousLow) and low < previousLow and close > previousLow
 
bool bullishRejection = close > open
bool bearishRejection = close < open
 
var int lastSignalBar = na
bool cooldownOK = na(lastSignalBar) or bar_index - lastSignalBar > cooldownBars
 
bool buyTrap = barstate.isconfirmed and lowerSweep and bullishRejection and volumeOK and trendBuyOK and cooldownOK
bool sellTrap = barstate.isconfirmed and upperSweep and bearishRejection and volumeOK and trendSellOK and cooldownOK
 
if buyTrap or sellTrap
    lastSignalBar := bar_index
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 04. MARKET STRUCTURE: BOS / CHoCH
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
float pivotHigh = ta.pivothigh(high, pivotLength, pivotLength)
float pivotLow = ta.pivotlow(low, pivotLength, pivotLength)
 
var float lastSwingHigh = na
var float lastSwingLow = na
var int structureTrend = 0
 
if not na(pivotHigh)
    lastSwingHigh := pivotHigh
 
if not na(pivotLow)
    lastSwingLow := pivotLow
 
bool bullishBreak = barstate.isconfirmed and not na(lastSwingHigh) and ta.crossover(close, lastSwingHigh)
bool bearishBreak = barstate.isconfirmed and not na(lastSwingLow) and ta.crossunder(close, lastSwingLow)
 
if showStructure and bullishBreak
    string structureText = structureTrend == -1 ? "CHoCH" : "BOS"
    label.new(bar_index, low, structureText, style=label.style_label_up, color=color.new(color.green, 15), textcolor=color.white, size=size.tiny)
    structureTrend := 1
 
if showStructure and bearishBreak
    string structureText = structureTrend == 1 ? "CHoCH" : "BOS"
    label.new(bar_index, high, structureText, style=label.style_label_down, color=color.new(color.red, 15), textcolor=color.white, size=size.tiny)
    structureTrend := -1
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 05. EMA DISPLAY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plot(showEMA ? emaTrend : na, title="Trend EMA", color=color.orange, linewidth=2)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 06. SIGNAL DISPLAY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plotshape(showSignals and buyTrap, title="Liquidity Trap BUY", style=shape.labelup, location=location.belowbar, color=color.new(color.lime, 0), text="TRAP BUY", textcolor=color.black, size=size.small)
 
plotshape(showSignals and sellTrap, title="Liquidity Trap SELL", style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), text="TRAP SELL", textcolor=color.white, size=size.small)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 07. TRADE LEVEL OBJECTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var line entryLine = na
var line stopLine = na
var line target1Line = na
var line target2Line = na
 
var label entryLabel = na
var label stopLabel = na
var label target1Label = na
var label target2Label = na
 
var float activeEntry = na
var float activeSL = na
var float activeTP1 = na
var float activeTP2 = na
var string activeDirection = "WAITING"
 
if buyTrap or sellTrap
    float newEntry = close
    float newSL = buyTrap ? low - atrValue * atrBuffer : high + atrValue * atrBuffer
    float riskDistance = math.abs(newEntry - newSL)
 
    bool validRisk = not na(riskDistance) and riskDistance > syminfo.mintick
 
    if validRisk
        activeEntry := newEntry
        activeSL := newSL
        activeTP1 := buyTrap ? newEntry + riskDistance * tp1RR : newEntry - riskDistance * tp1RR
        activeTP2 := buyTrap ? newEntry + riskDistance * tp2RR : newEntry - riskDistance * tp2RR
        activeDirection := buyTrap ? "BUY" : "SELL"
 
        if not na(entryLine)
            line.delete(entryLine)
            line.delete(stopLine)
            line.delete(target1Line)
            line.delete(target2Line)
            label.delete(entryLabel)
            label.delete(stopLabel)
            label.delete(target1Label)
            label.delete(target2Label)
 
        entryLine := na
        stopLine := na
        target1Line := na
        target2Line := na
        entryLabel := na
        stopLabel := na
        target1Label := na
        target2Label := na
 
        if showLevels
            color entryColor = color.new(color.blue, 0)
            color stopColor = color.new(color.red, 0)
            color targetColor = color.new(color.lime, 0)
 
            entryLine := line.new(bar_index, activeEntry, bar_index + extendBars, activeEntry, color=entryColor, width=2)
            stopLine := line.new(bar_index, activeSL, bar_index + extendBars, activeSL, color=stopColor, width=2)
            target1Line := line.new(bar_index, activeTP1, bar_index + extendBars, activeTP1, color=targetColor, width=2)
            target2Line := line.new(bar_index, activeTP2, bar_index + extendBars, activeTP2, color=targetColor, width=2)
 
            entryLabel := label.new(bar_index + extendBars, activeEntry, "ENTRY: " + str.tostring(activeEntry, format.mintick), style=label.style_label_left, color=entryColor, textcolor=color.white, size=size.small)
            stopLabel := label.new(bar_index + extendBars, activeSL, "SL: " + str.tostring(activeSL, format.mintick), style=label.style_label_left, color=stopColor, textcolor=color.white, size=size.small)
            target1Label := label.new(bar_index + extendBars, activeTP1, "TP1: " + str.tostring(activeTP1, format.mintick), style=label.style_label_left, color=targetColor, textcolor=color.black, size=size.small)
            target2Label := label.new(bar_index + extendBars, activeTP2, "TP2: " + str.tostring(activeTP2, format.mintick), style=label.style_label_left, color=targetColor, textcolor=color.black, size=size.small)
 
// Keep right-side labels aligned with the latest candle.
if showLevels and not na(entryLine)
    line.set_x2(entryLine, bar_index + extendBars)
    line.set_x2(stopLine, bar_index + extendBars)
    line.set_x2(target1Line, bar_index + extendBars)
    line.set_x2(target2Line, bar_index + extendBars)
 
    label.set_x(entryLabel, bar_index + extendBars)
    label.set_x(stopLabel, bar_index + extendBars)
    label.set_x(target1Label, bar_index + extendBars)
    label.set_x(target2Label, bar_index + extendBars)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 08. DASHBOARD
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var table dashboard = table.new(position.top_right, 2, 6, border_width=1)
 
if barstate.islast and showDashboard
    color headerColor = color.rgb(25, 35, 55)
    color cellColor = color.rgb(35, 42, 55)
 
    string trendText = close > emaTrend ? "BULLISH" : close < emaTrend ? "BEARISH" : "NEUTRAL"
    color trendColor = close > emaTrend ? color.lime : close < emaTrend ? color.red : color.gray
 
    string structureText = structureTrend == 1 ? "BULLISH" : structureTrend == -1 ? "BEARISH" : "NEUTRAL"
    string signalText = buyTrap ? "BUY TRAP" : sellTrap ? "SELL TRAP" : "WAITING"
    color signalColor = buyTrap ? color.lime : sellTrap ? color.red : color.silver
 
    table.cell(dashboard, 0, 0, "THOMAS", bgcolor=headerColor, text_color=color.white)
    table.cell(dashboard, 1, 0, "LIQUIDITY RADAR", bgcolor=headerColor, text_color=color.white)
 
    table.cell(dashboard, 0, 1, "Trend", bgcolor=cellColor, text_color=color.white)
    table.cell(dashboard, 1, 1, trendText, bgcolor=cellColor, text_color=trendColor)
 
    table.cell(dashboard, 0, 2, "Structure", bgcolor=cellColor, text_color=color.white)
    table.cell(dashboard, 1, 2, structureText, bgcolor=cellColor, text_color=color.white)
 
    table.cell(dashboard, 0, 3, "Last Signal", bgcolor=cellColor, text_color=color.white)
    table.cell(dashboard, 1, 3, signalText, bgcolor=cellColor, text_color=signalColor)
 
    table.cell(dashboard, 0, 4, "Active Trade", bgcolor=cellColor, text_color=color.white)
    table.cell(dashboard, 1, 4, activeDirection, bgcolor=cellColor, text_color=activeDirection == "BUY" ? color.lime : activeDirection == "SELL" ? color.red : color.silver)
 
    table.cell(dashboard, 0, 5, "ATR", bgcolor=cellColor, text_color=color.white)
    table.cell(dashboard, 1, 5, str.tostring(atrValue, format.mintick), bgcolor=cellColor, text_color=color.white)
 
if barstate.islast and not showDashboard
    table.clear(dashboard, 0, 0, 1, 5)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 09. ALERT CONDITIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(buyTrap, title="Liquidity Trap BUY", message="Thomas Liquidity Trap Radar PRO: BUY trap detected on {{ticker}} at {{close}}")
 
alertcondition(sellTrap, title="Liquidity Trap SELL", message="Thomas Liquidity Trap Radar PRO: SELL trap detected on {{ticker}} at {{close}}")
 
alertcondition(bullishBreak, title="Bullish BOS / CHoCH", message="Thomas Liquidity Trap Radar PRO: Bullish market structure break on {{ticker}}")
 
alertcondition(bearishBreak, title="Bearish BOS / CHoCH", message="Thomas Liquidity Trap Radar PRO: Bearish market structure break on {{ticker}}")

← 返回列表