本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Liquidity Target Mapper | NQ/MNQ", overlay=true, max_lines_count=100, max_labels_count=100)
// ============================================
// 1. SETTINGS
// ============================================
g1 = "01 | Liquidity Sources"
showPD = input.bool(true, "Previous day high / low", group=g1)
showSessions = input.bool(true, "Session highs / lows", group=g1)
showSwings = input.bool(true, "Confirmed swing highs / lows", group=g1)
pivotLen = input.int(5, "Swing confirmation bars", minval=1, maxval=30, group=g1)
swingCount = input.int(8, "Recent swings per side", minval=1, maxval=30, group=g1)
duplicateTicks = input.int(8, "Merge nearby levels (ticks)", minval=0, group=g1)
g2 = "02 | Session Settings"
tz = input.string("America/Chicago", "Session timezone", group=g2)
asiaSess = input.session("1800-2300", "Asia (CT)", group=g2)
londonSess = input.session("0200-0500", "London (CT)", group=g2)
nySess = input.session("0830-1100", "New York morning (CT)", group=g2)
g3 = "03 | ATR Targets"
showATR = input.bool(true, "Display ATR projections", group=g3)
atrLen = input.int(14, "ATR length", minval=1, group=g3)
atrTarget1 = input.float(1.0, "ATR target 1", step=0.25, minval=0.25, group=g3)
atrTarget2 = input.float(2.0, "ATR target 2", step=0.25, minval=0.25, group=g3)
g4 = "04 | Trade Planner"
usePlan = input.bool(false, "Enable manual trade planning", group=g4)
tradeDirection = input.string("Long", "Trade direction", options=["Long", "Short"], group=g4)
manualEntry = input.float(28000.0, "Planned entry price", step=0.25, group=g4)
manualStop = input.float(27950.0, "Planned stop price", step=0.25, group=g4)
g5 = "05 | Appearance"
showLines = input.bool(true, "Show ranked target lines", group=g5)
showLabels = input.bool(true, "Show target labels", group=g5)
showTable = input.bool(true, "Show target dashboard", group=g5)
showSourcePlots = input.bool(false, "Show source levels across chart", group=g5)
lineLength = input.int(45, "Line length (bars)", minval=5, maxval=150, group=g5)
// ============================================
// 2. BASE INDICATORS
// ============================================
float tick = syminfo.mintick
float atr = ta.atr(atrLen)
float anchor = usePlan ? manualEntry : close
float risk = math.abs(manualEntry - manualStop)
bool validPlan = usePlan and (
(tradeDirection == "Long" and manualStop < manualEntry) or
(tradeDirection == "Short" and manualStop > manualEntry)
) and risk >= tick
// Previous completed exchange trading day.
// Offset + lookahead_on gives known previous-day
// values without referencing future daily highs.
[pdh, pdl] = request.security(
syminfo.tickerid,
"D",
[high[1], low[1]],
lookahead=barmerge.lookahead_on
)
// ============================================
// 3. SESSION HIGH / LOW TRACKING
// ============================================
bool inAsia = not na(time(timeframe.period, asiaSess, tz))
bool inLondon = not na(time(timeframe.period, londonSess, tz))
bool inNY = not na(time(timeframe.period, nySess, tz))
var float aRunHigh = na
var float aRunLow = na
var float aHigh = na
var float aLow = na
var float lRunHigh = na
var float lRunLow = na
var float lHigh = na
var float lLow = na
var float nRunHigh = na
var float nRunLow = na
var float nHigh = na
var float nLow = na
// ASIA
if inAsia and not inAsia[1]
aRunHigh := high
aRunLow := low
else if inAsia
aRunHigh := math.max(aRunHigh, high)
aRunLow := math.min(aRunLow, low)
if not inAsia and inAsia[1]
aHigh := aRunHigh
aLow := aRunLow
// LONDON
if inLondon and not inLondon[1]
lRunHigh := high
lRunLow := low
else if inLondon
lRunHigh := math.max(lRunHigh, high)
lRunLow := math.min(lRunLow, low)
if not inLondon and inLondon[1]
lHigh := lRunHigh
lLow := lRunLow
// NEW YORK
if inNY and not inNY[1]
nRunHigh := high
nRunLow := low
else if inNY
nRunHigh := math.max(nRunHigh, high)
nRunLow := math.min(nRunLow, low)
if not inNY and inNY[1]
nHigh := nRunHigh
nLow := nRunLow
// ============================================
// 4. CONFIRMED SWING LIQUIDITY
// ============================================
float ph = ta.pivothigh(high, pivotLen, pivotLen)
float pl = ta.pivotlow(low, pivotLen, pivotLen)
var array<float> swingHighs = array.new_float()
var array<float> swingLows = array.new_float()
if not na(ph) and barstate.isconfirmed
array.unshift(swingHighs, ph)
if array.size(swingHighs) > swingCount
array.pop(swingHighs)
if not na(pl) and barstate.isconfirmed
array.unshift(swingLows, pl)
if array.size(swingLows) > swingCount
array.pop(swingLows)
// ============================================
// 5. HELPER FUNCTIONS
// ============================================
// Adds a level only if it is sufficiently
// different from existing candidates.
addLevel(array<float> prices, array<string> names, float level, string name, float mergeDistance) =>
if not na(level)
bool duplicate = false
if array.size(prices) > 0
for j = 0 to array.size(prices) - 1
if math.abs(array.get(prices, j) - level) <= mergeDistance
duplicate := true
break
if not duplicate
array.push(prices, level)
array.push(names, name)
// Find closest candidate on a given side.
// side: 1 = above, -1 = below.
// Removes the selected candidate so the next
// call returns the next distinct liquidity pool.
getNearest(array<float> prices, array<string> names, float reference, int side) =>
float selected = na
string selectedName = "None"
int selectedIndex = -1
if array.size(prices) > 0
for j = 0 to array.size(prices) - 1
float level = array.get(prices, j)
float distance = (level - reference) * side
if distance > 0
if na(selected) or distance < (selected - reference) * side
selected := level
selectedName := array.get(names, j)
selectedIndex := j
if selectedIndex >= 0
array.remove(prices, selectedIndex)
array.remove(names, selectedIndex)
[selected, selectedName]
// Format target R for planned trades.
getR(float level, float reference, float riskDistance, bool planValid, int side) =>
string result = "—"
if planValid and not na(level) and riskDistance > 0
bool correctSide = (tradeDirection == "Long" and side == 1) or (tradeDirection == "Short" and side == -1)
if correctSide
float multiple = math.abs(level - reference) / riskDistance
result := str.tostring(multiple, "#.##") + "R"
result
// ============================================
// 6. SOURCE LEVEL PLOTS
// ============================================
plot(showSourcePlots and showPD ? pdh : na, "PDH", color=color.orange, style=plot.style_stepline)
plot(showSourcePlots and showPD ? pdl : na, "PDL", color=color.orange, style=plot.style_stepline)
plot(showSourcePlots and showSessions ? aHigh : na, "Asia High", color=color.aqua, style=plot.style_stepline)
plot(showSourcePlots and showSessions ? aLow : na, "Asia Low", color=color.aqua, style=plot.style_stepline)
plot(showSourcePlots and showSessions ? lHigh : na, "London High", color=color.purple, style=plot.style_stepline)
plot(showSourcePlots and showSessions ? lLow : na, "London Low", color=color.purple, style=plot.style_stepline)
plot(showSourcePlots and showSessions ? nHigh : na, "NY High", color=color.yellow, style=plot.style_stepline)
plot(showSourcePlots and showSessions ? nLow : na, "NY Low", color=color.yellow, style=plot.style_stepline)
// ============================================
// 7. DRAWING OBJECT STORAGE
// ============================================
var array<line> targetLines = array.new_line()
var array<label> targetLabels = array.new_label()
var table dashboard = table.new(
position.top_right,
3,
11,
bgcolor=color.new(color.black, 12),
border_width=1
)
// ============================================
// 8. TARGET SELECTION AND DISPLAY
// ============================================
if barstate.islast
// Remove previous drawings.
if array.size(targetLines) > 0
for j = 0 to array.size(targetLines) - 1
line.delete(array.get(targetLines, j))
array.clear(targetLines)
if array.size(targetLabels) > 0
for j = 0 to array.size(targetLabels) - 1
label.delete(array.get(targetLabels, j))
array.clear(targetLabels)
// Build current candidate pools.
array<float> prices = array.new_float()
array<string> names = array.new_string()
float mergeDistance = duplicateTicks * tick
// Daily external liquidity.
if showPD
addLevel(prices, names, pdh, "PDH", mergeDistance)
addLevel(prices, names, pdl, "PDL", mergeDistance)
// Finalized session liquidity.
if showSessions
addLevel(prices, names, aHigh, "Asia High", mergeDistance)
addLevel(prices, names, aLow, "Asia Low", mergeDistance)
addLevel(prices, names, lHigh, "London High", mergeDistance)
addLevel(prices, names, lLow, "London Low", mergeDistance)
addLevel(prices, names, nHigh, "NY High", mergeDistance)
addLevel(prices, names, nLow, "NY Low", mergeDistance)
// Confirmed local swing pools.
if showSwings
if array.size(swingHighs) > 0
for j = 0 to array.size(swingHighs) - 1
addLevel(prices, names, array.get(swingHighs, j), "Swing High", mergeDistance)
if array.size(swingLows) > 0
for j = 0 to array.size(swingLows) - 1
addLevel(prices, names, array.get(swingLows, j), "Swing Low", mergeDistance)
// Duplicate working arrays, one for each direction.
array<float> upperPrices = array.copy(prices)
array<string> upperNames = array.copy(names)
array<float> lowerPrices = array.copy(prices)
array<string> lowerNames = array.copy(names)
array<float> rankedAbove = array.new_float()
array<string> rankedAboveNames = array.new_string()
array<float> rankedBelow = array.new_float()
array<string> rankedBelowNames = array.new_string()
// Rank three closest liquidity levels each way.
for k = 0 to 2
[abovePrice, aboveName] = getNearest(upperPrices, upperNames, anchor, 1)
[belowPrice, belowName] = getNearest(lowerPrices, lowerNames, anchor, -1)
array.push(rankedAbove, abovePrice)
array.push(rankedAboveNames, aboveName)
array.push(rankedBelow, belowPrice)
array.push(rankedBelowNames, belowName)
// ----------------------------------------
// DRAW TARGET LINES
// ----------------------------------------
if showLines
for k = 0 to 2
float upper = array.get(rankedAbove, k)
float lower = array.get(rankedBelow, k)
color upperColor = k == 0 ? color.lime : color.new(color.green, 25 + k * 15)
color lowerColor = k == 0 ? color.red : color.new(color.red, 25 + k * 15)
if not na(upper)
line lu = line.new(
bar_index - lineLength,
upper,
bar_index + 5,
upper,
color=upperColor,
style=k == 0 ? line.style_solid : line.style_dashed,
width=k == 0 ? 2 : 1
)
array.push(targetLines, lu)
if showLabels
string name = array.get(rankedAboveNames, k)
string caption = "T" + str.tostring(k + 1) + " UP | " + name + " | " + str.tostring(upper, format.mintick)
label lab = label.new(
bar_index + 5,
upper,
caption,
style=label.style_label_left,
color=color.new(color.green, 65),
textcolor=color.white,
size=size.tiny
)
array.push(targetLabels, lab)
if not na(lower)
line ld = line.new(
bar_index - lineLength,
lower,
bar_index + 5,
lower,
color=lowerColor,
style=k == 0 ? line.style_solid : line.style_dashed,
width=k == 0 ? 2 : 1
)
array.push(targetLines, ld)
if showLabels
string name = array.get(rankedBelowNames, k)
string caption = "T" + str.tostring(k + 1) + " DOWN | " + name + " | " + str.tostring(lower, format.mintick)
label lab = label.new(
bar_index + 5,
lower,
caption,
style=label.style_label_left,
color=color.new(color.red, 65),
textcolor=color.white,
size=size.tiny
)
array.push(targetLabels, lab)
// ----------------------------------------
// ATR REFERENCE PROJECTIONS
// ----------------------------------------
if showATR and not na(atr)
float atrUp1 = anchor + atr * atrTarget1
float atrUp2 = anchor + atr * atrTarget2
float atrDn1 = anchor - atr * atrTarget1
float atrDn2 = anchor - atr * atrTarget2
array.push(targetLines, line.new(bar_index-15, atrUp1, bar_index+3, atrUp1, color=color.new(color.blue, 35), style=line.style_dotted))
array.push(targetLines, line.new(bar_index-15, atrUp2, bar_index+3, atrUp2, color=color.new(color.blue, 55), style=line.style_dotted))
array.push(targetLines, line.new(bar_index-15, atrDn1, bar_index+3, atrDn1, color=color.new(color.blue, 35), style=line.style_dotted))
array.push(targetLines, line.new(bar_index-15, atrDn2, bar_index+3, atrDn2, color=color.new(color.blue, 55), style=line.style_dotted))
// ----------------------------------------
// MANUAL TRADE PLAN
// ----------------------------------------
if usePlan
array.push(targetLines, line.new(bar_index-lineLength, manualEntry, bar_index+5, manualEntry, color=color.white, width=2))
array.push(targetLines, line.new(bar_index-lineLength, manualStop, bar_index+5, manualStop, color=color.red, width=2))
if showLabels
array.push(targetLabels, label.new(bar_index+5, manualEntry, "ENTRY", style=label.style_label_left, color=color.gray, textcolor=color.white, size=size.tiny))
array.push(targetLabels, label.new(bar_index+5, manualStop, "STOP", style=label.style_label_left, color=color.red, textcolor=color.white, size=size.tiny))
// ----------------------------------------
// DASHBOARD
// ----------------------------------------
if showTable
table.cell(dashboard, 0, 0, "TARGET", bgcolor=color.new(color.blue, 50), text_color=color.white)
table.cell(dashboard, 1, 0, "PRICE / SOURCE", bgcolor=color.new(color.blue, 50), text_color=color.white)
table.cell(dashboard, 2, 0, "RR", bgcolor=color.new(color.blue, 50), text_color=color.white)
for k = 0 to 2
float upper = array.get(rankedAbove, k)
float lower = array.get(rankedBelow, k)
string upperName = array.get(rankedAboveNames, k)
string lowerName = array.get(rankedBelowNames, k)
int rowUp = k + 1
int rowDn = k + 5
string upperText = na(upper) ? "None" : str.tostring(upper, format.mintick) + " " + upperName
string lowerText = na(lower) ? "None" : str.tostring(lower, format.mintick) + " " + lowerName
table.cell(dashboard, 0, rowUp, "UP " + str.tostring(k+1), text_color=color.lime, text_size=size.tiny)
table.cell(dashboard, 1, rowUp, upperText, text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 2, rowUp, getR(upper, anchor, risk, validPlan, 1), text_color=color.lime, text_size=size.tiny)
table.cell(dashboard, 0, rowDn, "DOWN " + str.tostring(k+1), text_color=color.red, text_size=size.tiny)
table.cell(dashboard, 1, rowDn, lowerText, text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 2, rowDn, getR(lower, anchor, risk, validPlan, -1), text_color=color.red, text_size=size.tiny)
table.cell(dashboard, 0, 4, "REFERENCE", text_color=color.silver, text_size=size.tiny)
table.cell(dashboard, 1, 4, str.tostring(anchor, format.mintick), text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 2, 4, usePlan ? tradeDirection : "Live", text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 0, 8, "ATR (14)", text_color=color.silver, text_size=size.tiny)
table.cell(dashboard, 1, 8, str.tostring(atr, "#.##") + " pts", text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 2, 8, "", text_color=color.white)
table.cell(dashboard, 0, 9, "STOP RISK", text_color=color.silver, text_size=size.tiny)
table.cell(dashboard, 1, 9, usePlan ? str.tostring(risk, "#.##") + " pts" : "Plan off", text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 2, 9, validPlan ? "Valid" : "—", text_color=validPlan ? color.lime : color.gray, text_size=size.tiny)
table.cell(dashboard, 0, 10, "STATUS", text_color=color.silver, text_size=size.tiny)
table.cell(dashboard, 1, 10, usePlan and not validPlan ? "Check stop direction" : "Levels updated", text_color=color.white, text_size=size.tiny)
table.cell(dashboard, 2, 10, "", text_color=color.white)
// ============================================
// END
// ============================================