本页源码来自 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/
// © GoldenTraumaMD
//
//
// GoldenTraumaMD - Market Structure Map
// One chart, three layers: Supply / Demand zones with BOS / CHoCH, pivot liquidity levels that stay
// until filled, and the dealing range (Premium / Equilibrium / Discount, Strong / Weak High-Low).
// BOS / CHoCH come only from closed-candle zone breaks, overlapping zones are skipped and every
// drawing stays inside TradingView's object limits.
//@version=6
indicator("GoldenTraumaMD - Market Structure Map", shorttitle="GTMD Structure Map", overlay=true, max_boxes_count=500, max_labels_count=500, max_lines_count=500)
// ═══════════════════════════════════════════════
// SETTINGS
// ═══════════════════════════════════════════════
const string G_LIQ = "Structure & Liquidity"
const string G_SD = "Supply / Demand zones + BOS / CHoCH"
const string G_COL = "Colours"
plhSwing = input.int(50, "Swing structure length", minval=5, group=G_LIQ, tooltip="Bars on the left of a swing high / low. Used for Strong / Weak High-Low, Premium / Discount and the HH / LH / HL / LL labels.")
plhConf = input.int(10, "Structure confirm bars", minval=1, maxval=50, group=G_LIQ, tooltip="Bars on the right that confirm a swing. Each swing label appears this many candles later.")
plhZonePct = input.float(15.0, "Premium / Discount zone depth %", minval=1, maxval=50, step=0.5, group=G_LIQ, tooltip="Top X% of the dealing range = Premium, bottom X% = Discount. 50% line = Equilibrium.")
plhPivL = input.int(15, "Liquidity pivot left bars", minval=1, group=G_LIQ)
plhPivR = input.int(10, "Liquidity pivot right bars", minval=1, group=G_LIQ, tooltip="Each liquidity level appears this many candles after the pivot (the pivot must be confirmed first).")
plhBoxW = input.float(0.3, "Level box width", step=0.1, group=G_LIQ, tooltip="Box height = price × width / 1000.")
plhLineSty = input.string("Solid", "Level line style", options=["Solid", "Dashed", "Dotted"], group=G_LIQ)
plhLineWid = input.int(1, "Level line width", minval=1, maxval=4, group=G_LIQ)
plhMaxLvl = input.int(100, "Max open levels", minval=10, maxval=150, group=G_LIQ)
plhMaxDone = input.int(100, "Max filled levels kept on chart", minval=0, maxval=100, group=G_LIQ)
plhLevels = input.bool(true, "Show liquidity levels", group=G_LIQ)
plhExtend = input.bool(true, "Extend levels until filled", group=G_LIQ)
plhHideFil = input.bool(false, "Hide filled levels", group=G_LIQ)
plhBubbles = input.bool(false, "Dots on pivots", group=G_LIQ)
plhZones = input.bool(false, "Show Premium / Discount zones", group=G_LIQ)
plhSwings = input.bool(true, "Show Strong / Weak High-Low", group=G_LIQ)
plhHLLabel = input.bool(false, "Show HH / LH / HL / LL labels", group=G_LIQ)
sdOn = input.bool(true, "Show Supply / Demand zones", group=G_SD)
sdSwing = input.int(10, "Zone swing length", minval=1, maxval=50, group=G_SD, tooltip="Each zone appears this many candles after its pivot (the pivot must be confirmed first).")
sdHist = input.int(20, "Zones kept per side", minval=5, maxval=50, group=G_SD)
sdWidth = input.float(2.5, "Minimum zone thickness", minval=0.5, maxval=10, step=0.5, group=G_SD, tooltip="A zone is the wick of the swing candle (from the extreme to the body). It is never thinner than ATR × (value / 10) and never thicker than 1.5 × ATR.")
sdAtrLen = input.int(14, "ATR length", minval=1, group=G_SD)
sdPALbl = input.bool(false, "HH / LH / HL / LL labels of the zones", group=G_SD)
sdStruct = input.bool(true, "Show BOS / CHoCH from zone breaks", group=G_SD)
structName = input.string("BOS / CHoCH", "Structure label text", options=["BOS / CHoCH", "Break of Structure / Change of Character"], group=G_SD)
sdLblSize = input.string("Small", "BOS / CHoCH label size", options=["Tiny", "Small", "Normal", "Large"], group=G_SD)
sdLblPos = input.string("Above", "Label position", options=["Above", "Below"], group=G_SD)
sdLblOff = input.float(0.20, "Label distance (× ATR)", minval=0.05, maxval=2.0, step=0.05, group=G_SD)
sdMaxBrk = input.int(150, "Max BOS / CHoCH lines kept", minval=10, maxval=200, group=G_SD)
colSell = input.color(color.rgb(255, 60, 60), "Bearish / Supply", group=G_COL)
colBuy = input.color(color.rgb(0, 200, 90), "Bullish / Demand", group=G_COL)
// ═══════════════════════════════════════════════
// HELPERS
// ═══════════════════════════════════════════════
bi = bar_index
const int MAXBACK = 4000
noCol = color.new(color.black, 100)
// Keeps x coordinates inside the range TradingView allows
clampL(int x) =>
math.max(bi - MAXBACK, math.min(x, bi + 500))
f_structTxt(bool isChoch) =>
structName == "BOS / CHoCH" ? (isChoch ? "CHoCH" : "BOS") : (isChoch ? "Change of Character" : "Break of Structure")
f_size(string x) =>
x == "Tiny" ? size.tiny : x == "Small" ? size.small : x == "Normal" ? size.normal : size.large
// ═══════════════════════════════════════════════
// MARKET STRUCTURE (GoldenTraumaMD method)
// Swing high / low = pivot with "Swing structure length" bars on the left and
// "Structure confirm bars" on the right. A closed candle beyond the last swing sets the trend.
// ═══════════════════════════════════════════════
stPH = ta.pivothigh(high, plhSwing, plhConf)
stPL = ta.pivotlow(low, plhSwing, plhConf)
var float lastSH = na
var int lastSHx = na
var bool shBroken = true
var float lastSL = na
var int lastSLx = na
var bool slBroken = true
var int strTrend = 0 // 1 = bullish, -1 = bearish
// Dealing range: from the latest swing high / low, widened by any new extreme made since then
var float rngHi = na
var int rngHiX = na
var float rngLo = na
var int rngLoX = na
if not na(stPH)
int x = bi - plhConf
if plhHLLabel and not na(lastSH)
label.new(clampL(x), stPH, stPH > lastSH ? "HH" : "LH", color=noCol, textcolor=colSell, style=label.style_label_down, size=size.tiny)
lastSH := stPH
lastSHx := x
shBroken := false
// the highest high since the pivot candle (pivot right side is always lower)
rngHi := stPH
rngHiX := x
if not na(stPL)
int x = bi - plhConf
if plhHLLabel and not na(lastSL)
label.new(clampL(x), stPL, stPL < lastSL ? "LL" : "HL", color=noCol, textcolor=colBuy, style=label.style_label_up, size=size.tiny)
lastSL := stPL
lastSLx := x
slBroken := false
rngLo := stPL
rngLoX := x
if not na(rngHi) and high > rngHi
rngHi := high
rngHiX := bi
if not na(rngLo) and low < rngLo
rngLo := low
rngLoX := bi
if barstate.isconfirmed
if not shBroken and close > lastSH
shBroken := true
strTrend := 1
if not slBroken and close < lastSL
slBroken := true
strTrend := -1
// ═══════════════════════════════════════════════
// PREMIUM / EQUILIBRIUM / DISCOUNT and STRONG / WEAK (live, on the last candle)
// Re-created on the last bar so TradingView's auto-cleanup of old drawings can never remove them.
// Strong = the extreme that protects the current trend, Weak = the one price is expected to take.
// ═══════════════════════════════════════════════
var box pmBox = na
var box dcBox = na
var line eqLine = na
var label pmLbl = na
var label dcLbl = na
var label eqLbl = na
var line hiLine = na
var line loLine = na
var label hiLbl = na
var label loLbl = na
if barstate.islast
pmBox.delete()
dcBox.delete()
eqLine.delete()
pmLbl.delete()
dcLbl.delete()
eqLbl.delete()
hiLine.delete()
loLine.delete()
hiLbl.delete()
loLbl.delete()
if not na(rngHi) and not na(rngLo) and rngHi > rngLo
float rng = rngHi - rngLo
float zPct = plhZonePct / 100.0
float premLo = rngHi - rng * zPct
float discHi = rngLo + rng * zPct
float eq = (rngHi + rngLo) / 2.0
int x0 = clampL(math.min(rngHiX, rngLoX))
if plhZones
pmBox := box.new(x0, rngHi, bi + 20, premLo, bgcolor=color.new(colSell, 85), border_color=na)
dcBox := box.new(x0, discHi, bi + 20, rngLo, bgcolor=color.new(colBuy, 85), border_color=na)
eqLine := line.new(x0, eq, bi + 20, eq, color=color.gray, style=line.style_dashed)
pmLbl := label.new(bi + 20, (rngHi + premLo) / 2.0, "PREMIUM", color=noCol, textcolor=colSell, style=label.style_label_left, size=size.small)
dcLbl := label.new(bi + 20, (rngLo + discHi) / 2.0, "DISCOUNT", color=noCol, textcolor=colBuy, style=label.style_label_left, size=size.small)
eqLbl := label.new(bi + 20, eq, "EQUILIBRIUM", color=noCol, textcolor=color.gray, style=label.style_label_left, size=size.tiny)
if plhSwings
hiLine := line.new(clampL(rngHiX), rngHi, bi + 20, rngHi, color=colSell, width=2)
loLine := line.new(clampL(rngLoX), rngLo, bi + 20, rngLo, color=colBuy, width=2)
hiLbl := label.new(clampL(rngHiX), rngHi, strTrend == -1 ? "Strong High" : "Weak High", color=noCol, textcolor=colSell, style=label.style_label_down, size=size.small)
loLbl := label.new(clampL(rngLoX), rngLo, strTrend == 1 ? "Strong Low" : "Weak Low", color=noCol, textcolor=colBuy, style=label.style_label_up, size=size.small)
// ═══════════════════════════════════════════════
// SUPPLY / DEMAND ZONES + BOS / CHoCH FROM ZONE BREAKS
// Supply zone at every swing high, Demand zone at every swing low.
// Candle closes above a Supply zone → BOS / CHoCH up (zone removed).
// Candle closes below a Demand zone → BOS / CHoCH down (zone removed).
// ═══════════════════════════════════════════════
sdAtr = ta.atr(sdAtrLen)
sdPH = ta.pivothigh(high, sdSwing, sdSwing)
sdPL = ta.pivotlow(low, sdSwing, sdSwing)
var int sdTrend = 0 // 1 = up, -1 = down, 0 = unknown
var float sdLastHi = na
var float sdLastLo = na
var box[] supBox = array.new_box(sdHist, na)
var box[] demBox = array.new_box(sdHist, na)
var box[] supPoi = array.new_box(sdHist, na)
var box[] demPoi = array.new_box(sdHist, na)
var line[] brkLines = array.new_line()
var label[] brkLbls = array.new_label()
var int upLblBar = -1
var int dnLblBar = -1
// Puts a new box in front and deletes the oldest one
f_pushBox(box[] arr, box b) =>
box old = arr.pop()
if not na(old)
old.delete()
arr.unshift(b)
// Skips a new zone if its middle is within 2 × ATR of an existing zone
f_sdFree(float poi, box[] arr, float atrV) =>
bool ok = true
for bx in arr
if not na(bx)
float mid = (bx.get_top() + bx.get_bottom()) / 2.0
if math.abs(poi - mid) <= atrV * 2.0
ok := false
break
ok
// POI = middle of the zone, thin dashed line
f_poiBox(int x0, float y, color c) =>
box.new(x0, y, bi, y, border_color=color.new(c, 50), border_style=line.style_dashed, bgcolor=color.new(c, 100), extend=extend.none)
// Break line with a cap on how many stay on the chart
// Every break line keeps its own label; when the oldest is removed, both go together
// (so a BOS / CHoCH text is never left floating without its line)
f_brkLine(int x1, float y, color c) =>
brkLines.push(line.new(x1, y, bi, y, color=c))
brkLbls.push(label(na))
if brkLines.size() > sdMaxBrk
brkLines.shift().delete()
brkLbls.shift().delete()
f_brkLabel(label lb) =>
brkLbls.set(brkLbls.size() - 1, lb)
// New zones
if not na(sdPH) and not na(sdAtr)
int x0 = clampL(bi - sdSwing)
if sdPALbl and not na(sdLastHi)
label.new(x0, sdPH, sdPH >= sdLastHi ? "HH" : "LH", style=label.style_none, textcolor=colSell, color=noCol, size=size.tiny)
sdLastHi := sdPH
// Supply = upper wick of the pivot candle (high → top of its body), min / max thickness by ATR
float zTop = sdPH
float zBot = math.max(math.min(math.max(open[sdSwing], close[sdSwing]), sdPH - sdAtr * sdWidth / 10.0), sdPH - sdAtr * 1.5)
float zPoi = (zTop + zBot) / 2.0
if sdOn and f_sdFree(zPoi, supBox, sdAtr)
f_pushBox(supBox, box.new(x0, zTop, bi, zBot, border_color=color.new(colSell, 40), bgcolor=color.new(colSell, 75), extend=extend.none, text="SUPPLY", text_halign=text.align_right, text_valign=text.align_center, text_color=color.white, text_size=size.tiny))
f_pushBox(supPoi, f_poiBox(x0, zPoi, colSell))
if not na(sdPL) and not na(sdAtr)
int x0 = clampL(bi - sdSwing)
if sdPALbl and not na(sdLastLo)
label.new(x0, sdPL, sdPL >= sdLastLo ? "HL" : "LL", style=label.style_none, textcolor=colBuy, color=noCol, size=size.tiny)
sdLastLo := sdPL
// Demand = lower wick of the pivot candle (low → bottom of its body), min / max thickness by ATR
float zBot = sdPL
float zTop = math.min(math.max(math.min(open[sdSwing], close[sdSwing]), sdPL + sdAtr * sdWidth / 10.0), sdPL + sdAtr * 1.5)
float zPoi = (zTop + zBot) / 2.0
if sdOn and f_sdFree(zPoi, demBox, sdAtr)
f_pushBox(demBox, box.new(x0, zTop, bi, zBot, border_color=color.new(colBuy, 40), bgcolor=color.new(colBuy, 75), extend=extend.none, text="DEMAND", text_halign=text.align_right, text_valign=text.align_center, text_color=color.white, text_size=size.tiny))
f_pushBox(demPoi, f_poiBox(x0, zPoi, colBuy))
// Zone breaks (closed candle only → nothing appears and disappears inside a live candle)
if sdOn and barstate.isconfirmed
// Supply: close above the top of the zone
int prevT = sdTrend
for i = 0 to supBox.size() - 1
box bx = supBox.get(i)
if not na(bx)
if close >= bx.get_top()
string txt = f_structTxt(prevT == -1)
sdTrend := 1
if sdStruct
float mid = (bx.get_top() + bx.get_bottom()) / 2.0
int x1 = bx.get_left()
f_brkLine(x1, mid, colBuy)
// One label per candle, even if two zones break together
if upLblBar != bi
float ly = sdLblPos == "Above" ? mid + sdAtr * sdLblOff : mid - sdAtr * sdLblOff
f_brkLabel(label.new(int(math.round((x1 + bi) / 2.0)), ly, txt, style=label.style_none, textcolor=colBuy, color=noCol, size=f_size(sdLblSize)))
upLblBar := bi
bx.delete()
supBox.set(i, na)
box pb = supPoi.get(i)
if not na(pb)
pb.delete()
supPoi.set(i, na)
// Demand: close below the bottom of the zone
prevT := sdTrend
for i = 0 to demBox.size() - 1
box bx = demBox.get(i)
if not na(bx)
if close <= bx.get_bottom()
string txt = f_structTxt(prevT == 1)
sdTrend := -1
if sdStruct
float mid = (bx.get_top() + bx.get_bottom()) / 2.0
int x1 = bx.get_left()
f_brkLine(x1, mid, colSell)
if dnLblBar != bi
float ly = sdLblPos == "Above" ? mid + sdAtr * sdLblOff : mid - sdAtr * sdLblOff
f_brkLabel(label.new(int(math.round((x1 + bi) / 2.0)), ly, txt, style=label.style_none, textcolor=colSell, color=noCol, size=f_size(sdLblSize)))
dnLblBar := bi
bx.delete()
demBox.set(i, na)
box pb = demPoi.get(i)
if not na(pb)
pb.delete()
demPoi.set(i, na)
// Active zones reach a little to the right of the last candle so the text is readable
if barstate.islast
for bx in supBox
if not na(bx)
bx.set_right(bi + 15)
for bx in demBox
if not na(bx)
bx.set_right(bi + 15)
for bx in supPoi
if not na(bx)
bx.set_right(bi + 15)
for bx in demPoi
if not na(bx)
bx.set_right(bi + 15)
// ═══════════════════════════════════════════════
// LIQUIDITY LEVELS FROM PIVOTS
// ═══════════════════════════════════════════════
pivHi = ta.pivothigh(high, plhPivL, plhPivR)
pivLo = ta.pivotlow(low, plhPivL, plhPivR)
boxW1 = 0.001 * plhBoxW
lnSty = plhLineSty == "Solid" ? line.style_solid : plhLineSty == "Dashed" ? line.style_dashed : line.style_dotted
var box[] lvlBox = array.new_box()
var line[] lvlLine = array.new_line()
var box[] doneBox = array.new_box() // filled levels that stay visible
var line[] doneLine = array.new_line()
// Moves a filled level to the "filled" list and keeps that list short
f_done(box bx, line ln) =>
doneBox.push(bx)
doneLine.push(ln)
while doneBox.size() > plhMaxDone
doneBox.shift().delete()
doneLine.shift().delete()
if not na(pivHi) and plhLevels
lvlBox.push(box.new(clampL(bi - plhPivR), pivHi * (1 + boxW1), bi, pivHi, border_color=na, bgcolor=color.new(colSell, 80)))
lvlLine.push(line.new(clampL(bi - plhPivR), pivHi, bi, pivHi, color=colSell, style=lnSty, width=plhLineWid))
if not na(pivLo) and plhLevels
lvlBox.push(box.new(clampL(bi - plhPivR), pivLo, bi, pivLo * (1 - boxW1), border_color=na, bgcolor=color.new(colBuy, 80)))
lvlLine.push(line.new(clampL(bi - plhPivR), pivLo, bi, pivLo, color=colBuy, style=lnSty, width=plhLineWid))
// Every open level grows to the right until price touches it
if lvlBox.size() > 0
for k = lvlBox.size() - 1 to 0
box bx = lvlBox.get(k)
line ln = lvlLine.get(k)
float lvl = ln.get_y2()
bool filled = high >= lvl and low <= lvl
if not plhExtend
// Fixed length: stop growing right away
lvlLine.remove(k)
lvlBox.remove(k)
f_done(bx, ln)
continue
if filled
lvlLine.remove(k)
lvlBox.remove(k)
if plhHideFil
ln.delete()
bx.delete()
else
bx.set_right(bi)
ln.set_x2(bi)
f_done(bx, ln)
continue
bx.set_right(bi + 1)
ln.set_x2(bi + 1)
// Too many open levels → the oldest one is removed
while lvlBox.size() > plhMaxLvl
lvlBox.shift().delete()
lvlLine.shift().delete()
plotshape(plhBubbles and not na(pivHi) ? high[plhPivR] : na, "Pivot High", shape.circle, location.absolute, colSell, offset=-plhPivR, size=size.tiny)
plotshape(plhBubbles and not na(pivLo) ? low[plhPivR] : na, "Pivot Low", shape.circle, location.absolute, colBuy, offset=-plhPivR, size=size.tiny)