本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cage37999
//@version=6
indicator("CART3R DOL | Primary Liquidity Engine", overlay=true, max_lines_count=10, max_labels_count=10)
//====================================================
// CART3R DOL - INDEPENDENT IMPLEMENTATION
// Single primary Draw on Liquidity target
// Confirmed-bar structural calculations
//====================================================
// INPUTS
groupCore = "Core Engine"
pivotLen = input.int(5, "Liquidity Pivot Length", minval=2, maxval=20, group=groupCore)
maxATR = input.float(8.0, "Maximum Target Distance (ATR)", minval=1, maxval=20, group=groupCore)
maxLevels = input.int(80, "Maximum Stored Liquidity Levels", minval=20, maxval=200, group=groupCore)
halfLife = input.float(150.0, "Freshness Half-Life (Bars)", minval=10, group=groupCore)
groupStructure = "Market Structure"
structureLen = input.int(15, "Structure Break Length", minval=3, group=groupStructure)
obLookback = input.int(12, "Order Block Search", minval=2, maxval=30, group=groupStructure)
showPD = input.bool(true, "Previous Day Levels", group=groupStructure)
showPW = input.bool(true, "Previous Week Levels", group=groupStructure)
showFVG = input.bool(true, "Fair Value Gaps", group=groupStructure)
showOB = input.bool(true, "Order Blocks", group=groupStructure)
groupScoring = "Target Scoring"
wProximity = input.float(35, "Proximity Weight", minval=0, maxval=100, group=groupScoring)
wFreshness = input.float(20, "Freshness Weight", minval=0, maxval=100, group=groupScoring)
wConfluence = input.float(30, "Confluence Weight", minval=0, maxval=100, group=groupScoring)
wDensity = input.float(15, "Density Weight", minval=0, maxval=100, group=groupScoring)
minScore = input.float(25, "Minimum Target Score", minval=0, maxval=100, group=groupScoring)
groupVisual = "Display"
showLabel = input.bool(true, "Show Target Label", group=groupVisual)
showScore = input.bool(true, "Show Target Score", group=groupVisual)
bullColor = input.color(color.rgb(0, 220, 140), "Buy-Side Target Color", group=groupVisual)
bearColor = input.color(color.rgb(255, 65, 80), "Sell-Side Target Color", group=groupVisual)
//====================================================
// MARKET DATA
//====================================================
atr = ta.atr(14)
pivotHigh = ta.pivothigh(high, pivotLen, pivotLen)
pivotLow = ta.pivotlow(low, pivotLen, pivotLen)
// Confirmed higher-timeframe data
bias1H = request.security(syminfo.tickerid, "60", close[1] > ta.ema(close, 50)[1] ? 1 : close[1] < ta.ema(close, 50)[1] ? -1 : 0, lookahead=barmerge.lookahead_on)
bias4H = request.security(syminfo.tickerid, "240", close[1] > ta.ema(close, 50)[1] ? 1 : close[1] < ta.ema(close, 50)[1] ? -1 : 0, lookahead=barmerge.lookahead_on)
htfBias = bias1H + bias4H
// Previous day/week levels
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
pwh = request.security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
pwl = request.security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
//====================================================
// LIQUIDITY STORAGE
//====================================================
// Direction: 1 = Above price / BSL
// Direction: -1 = Below price / SSL
var float[] levels = array.new_float()
var int[] directions = array.new_int()
var int[] birthBars = array.new_int()
var float[] baseScores = array.new_float()
var int[] levelTypes = array.new_int()
// Level types:
// 1 = Swing liquidity
// 2 = Fair Value Gap
// 3 = Order Block
// 4 = Previous day
// 5 = Previous week
f_addLevel(float price, int direction, int kind, float baseScore) =>
if not na(price) and price > 0 and not na(atr)
bool duplicate = false
if array.size(levels) > 0
for i = 0 to array.size(levels) - 1
existing = array.get(levels, i)
existingDir = array.get(directions, i)
if existingDir == direction and math.abs(existing - price) <= atr * 0.12
duplicate := true
if not duplicate
array.push(levels, price)
array.push(directions, direction)
array.push(birthBars, bar_index)
array.push(baseScores, baseScore)
array.push(levelTypes, kind)
if array.size(levels) > maxLevels
array.shift(levels)
array.shift(directions)
array.shift(birthBars)
array.shift(baseScores)
array.shift(levelTypes)
//====================================================
// STRUCTURAL LEVEL DETECTION
//====================================================
if barstate.isconfirmed
// Swing liquidity
if not na(pivotHigh)
f_addLevel(pivotHigh, 1, 1, 70)
if not na(pivotLow)
f_addLevel(pivotLow, -1, 1, 70)
// Bullish and bearish fair value gaps
if showFVG and bar_index >= 2
// Bullish gap below price
if low > high[2]
f_addLevel(math.avg(low, high[2]), -1, 2, 58)
// Bearish gap above price
if high < low[2]
f_addLevel(math.avg(high, low[2]), 1, 2, 58)
// Bullish and bearish order blocks
bullBOS = close > ta.highest(high[1], structureLen)
bearBOS = close < ta.lowest(low[1], structureLen)
if showOB and bullBOS
float obPrice = na
for i = 1 to obLookback
if na(obPrice) and close[i] < open[i]
obPrice := math.avg(open[i], close[i])
if not na(obPrice)
f_addLevel(obPrice, -1, 3, 60)
if showOB and bearBOS
float obPrice = na
for i = 1 to obLookback
if na(obPrice) and close[i] > open[i]
obPrice := math.avg(open[i], close[i])
if not na(obPrice)
f_addLevel(obPrice, 1, 3, 60)
// Previous day levels
newDay = ta.change(time("D")) != 0
if showPD and newDay
f_addLevel(pdh, 1, 4, 85)
f_addLevel(pdl, -1, 4, 85)
// Previous week levels
newWeek = ta.change(time("W")) != 0
if showPW and newWeek
f_addLevel(pwh, 1, 5, 90)
f_addLevel(pwl, -1, 5, 90)
//====================================================
// TARGET SCORING ENGINE
//====================================================
float bestScore = na
float bestTarget = na
int bestDirection = 0
int bestType = 0
float totalWeight = wProximity + wFreshness + wConfluence + wDensity
if barstate.isconfirmed and array.size(levels) > 0 and not na(atr) and totalWeight > 0
// Remove already swept or invalidated levels
for i = array.size(levels) - 1 to 0
price = array.get(levels, i)
direction = array.get(directions, i)
swept = direction == 1 ? high >= price : low <= price
if swept
array.remove(levels, i)
array.remove(directions, i)
array.remove(birthBars, i)
array.remove(baseScores, i)
array.remove(levelTypes, i)
// Evaluate remaining candidates
if array.size(levels) > 0
for i = 0 to array.size(levels) - 1
price = array.get(levels, i)
direction = array.get(directions, i)
born = array.get(birthBars, i)
base = array.get(baseScores, i)
kind = array.get(levelTypes, i)
correctSide = direction == 1 ? price > close : price < close
distance = math.abs(price - close)
if correctSide and distance <= atr * maxATR
// 1. Proximity
proximity = 100 * (1 - math.min(distance / (atr * maxATR), 1))
// 2. Freshness decay
age = math.max(bar_index - born, 0)
freshness = 100 * math.pow(0.5, age / halfLife)
// 3. Confluence
confluence = base
aligned = direction == 1 ? htfBias > 0 : htfBias < 0
if aligned
confluence += 15
if kind == 4 or kind == 5
confluence += 5
confluence := math.min(confluence, 100)
// 4. Liquidity density
clusterCount = 0
for j = 0 to array.size(levels) - 1
otherPrice = array.get(levels, j)
otherDir = array.get(directions, j)
if i != j and otherDir == direction and math.abs(price - otherPrice) <= atr * 0.30
clusterCount += 1
density = math.min(100, 35 + clusterCount * 20)
// Final weighted score
score = (proximity * wProximity + freshness * wFreshness + confluence * wConfluence + density * wDensity) / totalWeight
if score >= minScore and (na(bestScore) or score > bestScore)
bestScore := score
bestTarget := price
bestDirection := direction
bestType := kind
//====================================================
// PRIMARY TARGET
//====================================================
var float activeTarget = na
var float activeScore = na
var int activeDirection = 0
var int activeType = 0
if barstate.isconfirmed
activeTarget := bestTarget
activeScore := bestScore
activeDirection := bestDirection
activeType := bestType
//====================================================
// SINGLE PRIMARY DOL LINE
//====================================================
var line dolLine = na
var label dolLabel = na
targetColor = activeDirection == 1 ? bullColor : bearColor
if not na(activeTarget)
if na(dolLine)
dolLine := line.new(bar_index, activeTarget, bar_index + 1, activeTarget, extend=extend.right, color=targetColor, width=2)
else
line.set_xy1(dolLine, bar_index, activeTarget)
line.set_xy2(dolLine, bar_index + 1, activeTarget)
line.set_color(dolLine, targetColor)
if showLabel
targetName = activeDirection == 1 ? "BSL TARGET" : "SSL TARGET"
scoreText = showScore ? " | SCORE " + str.tostring(activeScore, "#.0") : ""
labelText = "PRIMARY DOL: " + targetName + scoreText + "\n" + str.tostring(activeTarget, format.mintick)
if na(dolLabel)
dolLabel := label.new(bar_index + 1, activeTarget, labelText, style=label.style_label_left, color=targetColor, textcolor=color.white, size=size.small)
else
label.set_xy(dolLabel, bar_index + 1, activeTarget)
label.set_text(dolLabel, labelText)
label.set_color(dolLabel, targetColor)
else
if not na(dolLabel)
label.delete(dolLabel)
dolLabel := na
else
if not na(dolLine)
line.delete(dolLine)
dolLine := na
if not na(dolLabel)
label.delete(dolLabel)
dolLabel := na
//====================================================
// ALERTS
//====================================================
newTarget = not na(activeTarget) and (na(activeTarget[1]) or activeTarget != activeTarget[1])
alertcondition(newTarget, "New Primary DOL", "CART3R: A new primary DOL target has been selected.")
alertcondition(not na(activeTarget) and high >= activeTarget and activeDirection == 1, "BSL Target Touched", "CART3R: Buy-side liquidity target touched.")
alertcondition(not na(activeTarget) and low <= activeTarget and activeDirection == -1, "SSL Target Touched", "CART3R: Sell-side liquidity target touched.")