Smart Money Liquidation Exploits [AlgoAlpha]

AlgoAlpha · study · 327 行 · 点赞 2,635 · 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/
// © AlgoAlpha
 
//@version=6
indicator("Smart Money Liquidation Exploits [AlgoAlpha]", "AlgoAlpha - Smart Money Liquidation", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 5000)
 
// Inputs
useHTF = input.bool(false, "Use Higher Timeframe", tooltip = "Use data from a higher timeframe for pivots, liquidity sweeps, take-profit levels, and expiry. When disabled, the indicator uses the current chart timeframe.", group = "Timeframe") 
pivotLen = input.int(3, "Pivot Length", minval = 1, tooltip = "Number of bars used on each side of a pivot to confirm liquidity highs and lows.", group = "Timeframe")
htfInput = input.timeframe("60", "Higher Timeframe", tooltip = "Timeframe used when Higher Timeframe mode is enabled. The default is 1 hour.", group = "Timeframe")
expiryInput = input.int(10, "Level Expiry Bars", minval = 1, tooltip = "Number of active bars before a liquidity or take-profit level expires. In Higher Timeframe mode, these are higher-timeframe bars.", group = "Timeframe")
 
bullColor = input.color(#00ffbb, "Bullish Color", tooltip = "Color used for bullish liquidity levels, signals, and take-profit levels.", group = "Appearance", inline = "colors")
bearColor = input.color(#ff1100, "", tooltip = "Color used for bearish liquidity levels, signals, and take-profit levels.", group = "Appearance", inline = "colors")
 
// Timeframe validation
if useHTF and barstate.isfirst
    chartSeconds = timeframe.in_seconds(timeframe.period)
    htfSeconds = timeframe.in_seconds(htfInput)
 
    if not na(chartSeconds) and not na(htfSeconds) and htfSeconds < chartSeconds
        runtime.error("Higher Timeframe must be equal to or higher than the chart timeframe.")
 
// Higher timeframe data
getHTFData() =>
    _ph = ta.pivothigh(pivotLen, pivotLen)
    _pl = ta.pivotlow(pivotLen, pivotLen)
    _phTime = not na(_ph) ? time[pivotLen] : int(na)
    _plTime = not na(_pl) ? time[pivotLen] : int(na)
    [open, high, low, close, time, time_close, _ph, _pl, _phTime, _plTime]
 
[htfOpen, htfHigh, htfLow, htfClose, htfTime, htfTimeClose, htfPh, htfPl, htfPhTime, htfPlTime] = request.security(syminfo.tickerid, htfInput, getHTFData(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
 
// Current timeframe data
chartPh = ta.pivothigh(pivotLen, pivotLen)
chartPl = ta.pivotlow(pivotLen, pivotLen)
 
chartPhTime = not na(chartPh) ? time[pivotLen] : int(na)
chartPlTime = not na(chartPl) ? time[pivotLen] : int(na)
 
// Logic data
logicOpen = useHTF ? htfOpen : open
logicHigh = useHTF ? htfHigh : high
logicLow = useHTF ? htfLow : low
logicClose = useHTF ? htfClose : close
 
logicPh = useHTF ? htfPh : chartPh
logicPl = useHTF ? htfPl : chartPl
 
logicPhTime = useHTF ? htfPhTime : chartPhTime
logicPlTime = useHTF ? htfPlTime : chartPlTime
 
processLogicBar = not useHTF or (barstate.isconfirmed and time_close == htfTimeClose)
 
var logicBarCounter = -1
 
// Zigzag state
var lastType = 0
 
var latestHighLevel = float(na)
var latestHighTime = int(na)
 
var latestLowLevel = float(na)
var latestLowTime = int(na)
 
var currentPivotLineStore = array.new_line(1)
var currentPivotType = 0
 
// Liquidity levels
var liqLines = array.new_line()
var liqLevels = array.new_float()
var liqTypes = array.new_int()
var liqExpiry = array.new_int()
 
// Take profit levels
var tpLines = array.new_line()
var tpLevels = array.new_float()
var tpDirections = array.new_int()
var tpExpiry = array.new_int()
var tpStartBars = array.new_int()
var tpStartChartBars = array.new_int()
var tpStartWicks = array.new_float()
 
// Line extension
if array.size(liqLines) > 0
    for i = 0 to array.size(liqLines) - 1
        activeLine = array.get(liqLines, i)
        activeLevel = array.get(liqLevels, i)
        line.set_x2(activeLine, time)
        line.set_y2(activeLine, activeLevel)
 
if array.size(tpLines) > 0
    for i = 0 to array.size(tpLines) - 1
        activeLine = array.get(tpLines, i)
        activeLevel = array.get(tpLevels, i)
        line.set_x2(activeLine, time)
        line.set_y2(activeLine, activeLevel)
 
// Signal state
bullishSweep = false
bearishSweep = false
tpHitAbove = false
tpHitBelow = false
 
// Main logic
if processLogicBar
    logicBarCounter += 1
 
    if not na(logicPh)
        if lastType != 1
            latestHighLevel := logicPh
            latestHighTime := logicPhTime
 
            newLine = line.new(x1 = logicPhTime, y1 = logicPh, x2 = time, y2 = logicPh, xloc = xloc.bar_time, extend = extend.none, color = bearColor, width = 1)
 
            array.push(liqLines, newLine)
            array.push(liqLevels, logicPh)
            array.push(liqTypes, 1)
            array.push(liqExpiry, logicBarCounter + expiryInput - 1)
 
            array.set(currentPivotLineStore, 0, newLine)
            currentPivotType := 1
 
        else if na(latestHighLevel) or logicPh > latestHighLevel
            latestHighLevel := logicPh
            latestHighTime := logicPhTime
 
            currentPivotLine = array.get(currentPivotLineStore, 0)
            currentIndex = na(currentPivotLine) ? -1 : array.indexof(liqLines, currentPivotLine)
 
            if currentIndex >= 0 and currentPivotType == 1
                line.set_xy1(currentPivotLine, logicPhTime, logicPh)
                line.set_xy2(currentPivotLine, time, logicPh)
                array.set(liqLevels, currentIndex, logicPh)
                array.set(liqExpiry, currentIndex, logicBarCounter + expiryInput - 1)
 
            else
                newLine = line.new(x1 = logicPhTime, y1 = logicPh, x2 = time, y2 = logicPh, xloc = xloc.bar_time, extend = extend.none, color = bearColor, width = 1)
 
                array.push(liqLines, newLine)
                array.push(liqLevels, logicPh)
                array.push(liqTypes, 1)
                array.push(liqExpiry, logicBarCounter + expiryInput - 1)
 
                array.set(currentPivotLineStore, 0, newLine)
                currentPivotType := 1
 
        lastType := 1
 
    if not na(logicPl)
        if lastType != -1
            latestLowLevel := logicPl
            latestLowTime := logicPlTime
 
            newLine = line.new(x1 = logicPlTime, y1 = logicPl, x2 = time, y2 = logicPl, xloc = xloc.bar_time, extend = extend.none, color = bullColor, width = 1)
 
            array.push(liqLines, newLine)
            array.push(liqLevels, logicPl)
            array.push(liqTypes, -1)
            array.push(liqExpiry, logicBarCounter + expiryInput - 1)
 
            array.set(currentPivotLineStore, 0, newLine)
            currentPivotType := -1
 
        else if na(latestLowLevel) or logicPl < latestLowLevel
            latestLowLevel := logicPl
            latestLowTime := logicPlTime
 
            currentPivotLine = array.get(currentPivotLineStore, 0)
            currentIndex = na(currentPivotLine) ? -1 : array.indexof(liqLines, currentPivotLine)
 
            if currentIndex >= 0 and currentPivotType == -1
                line.set_xy1(currentPivotLine, logicPlTime, logicPl)
                line.set_xy2(currentPivotLine, time, logicPl)
                array.set(liqLevels, currentIndex, logicPl)
                array.set(liqExpiry, currentIndex, logicBarCounter + expiryInput - 1)
 
            else
                newLine = line.new(x1 = logicPlTime, y1 = logicPl, x2 = time, y2 = logicPl, xloc = xloc.bar_time, extend = extend.none, color = bullColor, width = 1)
 
                array.push(liqLines, newLine)
                array.push(liqLevels, logicPl)
                array.push(liqTypes, -1)
                array.push(liqExpiry, logicBarCounter + expiryInput - 1)
 
                array.set(currentPivotLineStore, 0, newLine)
                currentPivotType := -1
 
        lastType := -1
 
    bodyTop = math.max(logicOpen, logicClose)
    bodyBottom = math.min(logicOpen, logicClose)
 
    // Take profit monitoring
    if array.size(tpLines) > 0
        for i = array.size(tpLines) - 1 to 0
            tpLine = array.get(tpLines, i)
            tpLevel = array.get(tpLevels, i)
            tpDirection = array.get(tpDirections, i)
            expiryBar = array.get(tpExpiry, i)
            startBar = array.get(tpStartBars, i)
            startChartBar = array.get(tpStartChartBars, i)
            startWick = array.get(tpStartWicks, i)
 
            removeTp = false
 
            if logicBarCounter <= expiryBar
                if logicBarCounter > startBar
                    if tpDirection == 1 and logicHigh >= tpLevel
                        tpHitAbove := true
                        removeTp := true
                        line.new(x1 = startChartBar, y1 = startWick, x2 = bar_index, y2 = high, xloc = xloc.bar_index, extend = extend.none, color = color.new(chart.fg_color, 0), style = line.style_dotted, width = 1)
 
                    else if tpDirection == -1 and logicLow <= tpLevel
                        tpHitBelow := true
                        removeTp := true
                        line.new(x1 = startChartBar, y1 = startWick, x2 = bar_index, y2 = low, xloc = xloc.bar_index, extend = extend.none, color = color.new(chart.fg_color, 0), style = line.style_dotted, width = 1)
 
                if not removeTp and logicBarCounter == expiryBar
                    removeTp := true
 
            else
                removeTp := true
 
            if removeTp
                line.set_x2(tpLine, time)
                line.set_y2(tpLine, tpLevel)
 
                array.remove(tpLines, i)
                array.remove(tpLevels, i)
                array.remove(tpDirections, i)
                array.remove(tpExpiry, i)
                array.remove(tpStartBars, i)
                array.remove(tpStartChartBars, i)
                array.remove(tpStartWicks, i)
 
    // Liquidity sweep monitoring
    if array.size(liqLines) > 0
        for i = array.size(liqLines) - 1 to 0
            levelLine = array.get(liqLines, i)
            level = array.get(liqLevels, i)
            levelType = array.get(liqTypes, i)
            expiryBar = array.get(liqExpiry, i)
 
            removeLevel = false
 
            if logicBarCounter <= expiryBar
                if levelType == 1
                    wickTouch = logicHigh >= level
                    validSweep = logicHigh > level and bodyTop < level
 
                    if wickTouch
                        removeLevel := true
 
                        if validSweep
                            bearishSweep := true
 
                            if not na(latestLowLevel) and latestLowLevel < level
                                target = (level + latestLowLevel) / 2.0
                                newTp = line.new(x1 = time, y1 = target, x2 = time, y2 = target, xloc = xloc.bar_time, extend = extend.none, color = bearColor, style = line.style_dotted, width = 1)
 
                                array.push(tpLines, newTp)
                                array.push(tpLevels, target)
                                array.push(tpDirections, -1)
                                array.push(tpExpiry, logicBarCounter + expiryInput - 1)
                                array.push(tpStartBars, logicBarCounter)
                                array.push(tpStartChartBars, bar_index)
                                array.push(tpStartWicks, high)
 
                else
                    wickTouch = logicLow <= level
                    validSweep = logicLow < level and bodyBottom > level
 
                    if wickTouch
                        removeLevel := true
 
                        if validSweep
                            bullishSweep := true
 
                            if not na(latestHighLevel) and latestHighLevel > level
                                target = (level + latestHighLevel) / 2.0
                                newTp = line.new(x1 = time, y1 = target, x2 = time, y2 = target, xloc = xloc.bar_time, extend = extend.none, color = bullColor, style = line.style_dotted, width = 1)
 
                                array.push(tpLines, newTp)
                                array.push(tpLevels, target)
                                array.push(tpDirections, 1)
                                array.push(tpExpiry, logicBarCounter + expiryInput - 1)
                                array.push(tpStartBars, logicBarCounter)
                                array.push(tpStartChartBars, bar_index)
                                array.push(tpStartWicks, low)
 
                if not removeLevel and logicBarCounter == expiryBar
                    removeLevel := true
 
            else
                removeLevel := true
 
            if removeLevel
                line.set_x2(levelLine, time)
                line.set_y2(levelLine, level)
 
                array.remove(liqLines, i)
                array.remove(liqLevels, i)
                array.remove(liqTypes, i)
                array.remove(liqExpiry, i)
 
// Signal placement
recentHigh = ta.highest(high, 5)
recentLow = ta.lowest(low, 5)
 
bullishSweepY = bullishSweep ? recentLow : float(na)
bearishSweepY = bearishSweep ? recentHigh : float(na)
tpHitAboveY = tpHitAbove ? recentHigh : float(na)
tpHitBelowY = tpHitBelow ? recentLow : float(na)
 
// Signals
plotshape(bullishSweepY, title = "Bullish Liquidity Sweep", style = shape.labelup, location = location.absolute, color = bullColor, text = "▲", textcolor = chart.fg_color, size = size.tiny)
plotshape(bearishSweepY, title = "Bearish Liquidity Sweep", style = shape.labeldown, location = location.absolute, color = bearColor, text = "▼", textcolor = chart.fg_color, size = size.tiny)
 
plotshape(tpHitAboveY, title = "Bullish Take Profit Hit", style = shape.labeldown, location = location.absolute, color = color.new(chart.fg_color, 100), text = "✅", textcolor = chart.fg_color, size = size.tiny)
plotshape(tpHitBelowY, title = "Bearish Take Profit Hit", style = shape.labelup, location = location.absolute, color = color.new(chart.fg_color, 100), text = "✅", textcolor = chart.fg_color, size = size.tiny)
 
// Alerts
alertcondition(bullishSweep, "Bullish Liquidity Sweep", "Sell-side liquidity swept.")
alertcondition(bearishSweep, "Bearish Liquidity Sweep", "Buy-side liquidity swept.")
alertcondition(tpHitAbove, "Bullish Take Profit Hit", "Bullish liquidity-sweep take-profit level reached.")
alertcondition(tpHitBelow, "Bearish Take Profit Hit", "Bearish liquidity-sweep take-profit level reached.")

← 返回列表