本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Liquidity Sweep + FVG | 2m Entries / 5m Levels v17", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=300)
string TZ = "America/Los_Angeles"
bool showLevels = input.bool(true, "Show 5m liquidity levels")
bool showFVG = input.bool(true, "Show FVG boxes")
bool showMidpoint = input.bool(true, "Show entry candle 50%")
int entryLineBars = input.int(8, "Trade line length", minval=2, maxval=50)
int recentCount = input.int(5, "Recent highs and lows", minval=1, maxval=20)
bool onTwoMinutes = timeframe.isminutes and timeframe.multiplier == 2
int minuteOfDay = hour(time, TZ) * 60 + minute(time, TZ)
int todayKey = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
int previousDay = time - 86400000
int previousKey = year(previousDay, TZ) * 10000 + month(previousDay, TZ) * 100 + dayofmonth(previousDay, TZ)
int sessionKey = minuteOfDay >= 900 ? todayKey : previousKey
bool tradeWindow = minuteOfDay >= 420 and minuteOfDay < 540
// FIVE-MINUTE LIQUIDITY DETECTION
// Solid swings and color rotations are independent.
f_levels5() =>
bool solidHigh = not na(high[3]) and high[2] > high[3] and high[2] > high[1]
bool solidLow = not na(low[3]) and low[2] < low[3] and low[2] < low[1]
bool rotationHigh = close[2] > open[2] and close[1] < open[1] and high[2] > high[1]
bool rotationLow = close[2] < open[2] and close[1] > open[1] and low[2] < low[1]
// Each pattern is checked separately.
// A rotation does not cancel a solid swing.
bool validHigh = (solidHigh or rotationHigh) and high[1] < high[2]
bool validLow = (solidLow or rotationLow) and low[1] > low[2]
float levelHigh = validHigh ? high[2] : na
float levelLow = validLow ? low[2] : na
[time[1], time[2], levelHigh, levelLow]
[confirmed5Time, pivotTime, fiveHigh, fiveLow] = request.security(syminfo.tickerid, "5", f_levels5(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
// LIQUIDITY STORAGE
var float[] activeHighs = array.new_float()
var float[] activeLows = array.new_float()
var line[] recentHighLines = array.new_line()
var line[] recentLowLines = array.new_line()
var bool[] highTouched = array.new_bool()
var bool[] lowTouched = array.new_bool()
var int[] highCreated = array.new_int()
var int[] lowCreated = array.new_int()
var int lastConfirmed5 = na
var int lastSession = na
var bool sweptAny = false
var bool traded = false
// SESSION RESET
if na(lastSession) or sessionKey != lastSession
array.clear(activeHighs)
array.clear(activeLows)
for ln in recentHighLines
line.delete(ln)
for ln in recentLowLines
line.delete(ln)
array.clear(recentHighLines)
array.clear(recentLowLines)
array.clear(highTouched)
array.clear(lowTouched)
array.clear(highCreated)
array.clear(lowCreated)
lastSession := sessionKey
sweptAny := false
traded := false
// COLLECT FIVE-MINUTE LEVELS
bool fresh5 = not na(confirmed5Time) and (na(lastConfirmed5) or confirmed5Time != lastConfirmed5)
if fresh5 and barstate.isconfirmed
lastConfirmed5 := confirmed5Time
if not traded
int pivotMinute = hour(pivotTime, TZ) * 60 + minute(pivotTime, TZ)
bool collectionTime = pivotMinute >= 900 or pivotMinute < 540
if collectionTime
if not na(fiveHigh)
array.push(activeHighs, fiveHigh)
if showLevels
line highLine = line.new(pivotTime, fiveHigh, time, fiveHigh, xloc=xloc.bar_time, extend=extend.right, color=color.red)
array.push(recentHighLines, highLine)
array.push(highTouched, false)
array.push(highCreated, time)
if not na(fiveLow)
array.push(activeLows, fiveLow)
if showLevels
line lowLine = line.new(pivotTime, fiveLow, time, fiveLow, xloc=xloc.bar_time, extend=extend.right, color=color.lime)
array.push(recentLowLines, lowLine)
array.push(lowTouched, false)
array.push(lowCreated, time)
while array.size(recentHighLines) > recentCount
line.delete(array.shift(recentHighLines))
array.shift(highTouched)
array.shift(highCreated)
while array.size(recentLowLines) > recentCount
line.delete(array.shift(recentLowLines))
array.shift(lowTouched)
array.shift(lowCreated)
// STOP EACH LEVEL AT ITS FIRST TOUCH
if barstate.isconfirmed and showLevels
if array.size(recentHighLines) > 0
for i = 0 to array.size(recentHighLines) - 1
if not array.get(highTouched, i)
line ln = array.get(recentHighLines, i)
float level = line.get_y1(ln)
int created = array.get(highCreated, i)
if time > created and high >= level and low <= level
line.set_extend(ln, extend.none)
line.set_x2(ln, time)
array.set(highTouched, i, true)
if array.size(recentLowLines) > 0
for i = 0 to array.size(recentLowLines) - 1
if not array.get(lowTouched, i)
line ln = array.get(recentLowLines, i)
float level = line.get_y1(ln)
int created = array.get(lowCreated, i)
if time > created and high >= level and low <= level
line.set_extend(ln, extend.none)
line.set_x2(ln, time)
array.set(lowTouched, i, true)
// LIQUIDITY SWEEP DETECTION
bool sweptThisBar = false
if onTwoMinutes and barstate.isconfirmed and tradeWindow and not traded
if array.size(activeLows) > 0
for i = array.size(activeLows) - 1 to 0
float level = array.get(activeLows, i)
if close[1] >= level and low < level
sweptThisBar := true
array.remove(activeLows, i)
if array.size(activeHighs) > 0
for i = array.size(activeHighs) - 1 to 0
float level = array.get(activeHighs, i)
if close[1] <= level and high > level
sweptThisBar := true
array.remove(activeHighs, i)
if sweptThisBar
sweptAny := true
// TWO-MINUTE FAIR VALUE GAPS
bool bullFVG = bar_index >= 2 and low > high[2]
bool bearFVG = bar_index >= 2 and high < low[2]
// ENTRY CONFIRMATION
bool longSignal = onTwoMinutes and barstate.isconfirmed and tradeWindow and not traded and sweptAny and bullFVG and close > open
bool shortSignal = onTwoMinutes and barstate.isconfirmed and tradeWindow and not traded and sweptAny and bearFVG and close < open
// DRAW FVG BOXES
if onTwoMinutes and barstate.isconfirmed and showFVG and bullFVG
box.new(bar_index - 2, low, bar_index, high[2], bgcolor=color.new(color.green, 90), border_color=color.new(color.green, 65))
if onTwoMinutes and barstate.isconfirmed and showFVG and bearFVG
box.new(bar_index - 2, low[2], bar_index, high, bgcolor=color.new(color.red, 90), border_color=color.new(color.red, 65))
// ENTRY, STOP LOSS, TARGET
bool firedLong = false
bool firedShort = false
if longSignal or shortSignal
float entry = close
float candleRange = high - low
float stop = longSignal ? low - candleRange * 0.24 : high + candleRange * 0.24
float risk = math.abs(entry - stop)
float target = longSignal ? entry + risk : entry - risk
if risk > syminfo.mintick
line.new(bar_index, entry, bar_index + entryLineBars, entry, color=color.white, width=2)
line.new(bar_index, stop, bar_index + entryLineBars, stop, color=color.red, width=2)
line.new(bar_index, target, bar_index + entryLineBars, target, color=color.lime, width=2)
if showMidpoint
float midpoint = (high + low) / 2.0
line.new(bar_index, midpoint, bar_index + entryLineBars, midpoint, color=color.blue, style=line.style_dashed)
label.new(bar_index, longSignal ? low : high, longSignal ? "LONG 1:1" : "SHORT 1:1", style=longSignal ? label.style_label_up : label.style_label_down, color=longSignal ? color.green : color.red, textcolor=color.white)
firedLong := longSignal
firedShort := shortSignal
traded := true
// ALERTS
alertcondition(firedLong, title="2m Long", message="2m bullish FVG confirmation after 5m liquidity sweep")
alertcondition(firedShort, title="2m Short", message="2m bearish FVG confirmation after 5m liquidity sweep")