本页源码来自 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/
// © MAlphaCore
//@version=6
// M·Alpha·Wall — price levels where unusually large volume changed hands ("walls")
// A bar counts as a large-volume bar when its volume is at least X times its recent average. Its dollar volume is
// spread over the bar's price range and accumulated into price levels. Levels below the current price act as
// support walls, levels above as resistance walls. Older walls can fade out (decay) and are dropped after a maximum age.
indicator("M·Alpha·Wall", shorttitle = "M·Alpha·Wall", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ───────────────────────────── Detection ─────────────────────────────
grDet = "Detection"
avgLen = input.int(50, "Average volume over (bars)", minval = 5, maxval = 500, group = grDet)
volFactor = input.float(2.0, "Large volume = at least x times average", minval = 1.0, maxval = 20, step = 0.1, group = grDet)
spreadMode = input.string("Bar range", "Place the volume at", options = ["Bar range", "Bar center (HLC3)"], group = grDet, tooltip = "Bar range spreads the volume evenly from low to high; bar center puts it on one level.")
levelWidth = input.float(0.25, "Level width (% of price)", minval = 0.02, maxval = 2, step = 0.01, group = grDet)
decayPct = input.float(0.0, "Decay per bar (%)", minval = 0, maxval = 10, step = 0.1, group = grDet, tooltip = "Older walls lose this share of weight on every bar. 0 = off.")
maxAge = input.int(0, "Drop walls older than (bars, 0 = never)", minval = 0, maxval = 20000, group = grDet)
calcBars = input.int(3000, "Calculate over the last bars", minval = 200, maxval = 20000, group = grDet, tooltip = "More bars = more historical walls, but slower.")
// ───────────────────────────── Style ─────────────────────────────
grStyle = "Style"
drawMode = input.string("Lines", "Display", options = ["Lines", "Cloud", "Cloud + Lines"], group = grStyle)
wallCount = input.int(12, "Strongest walls per side", minval = 3, maxval = 240, group = grStyle)
peaksOnly = input.bool(true, "Peaks only (strongest level in its neighbourhood)", group = grStyle)
peakRange = input.float(0.5, "Neighbourhood for peaks (± % of price)", minval = 0.1, maxval = 10, step = 0.1, group = grStyle)
minPct = input.float(20.0, "Only walls from (% of strongest)", minval = 0, maxval = 90, step = 1, group = grStyle)
nearCount = input.int(5, "Always show nearest walls per side", minval = 0, maxval = 50, group = grStyle, tooltip = "Walls closest to the current price are shown even when they are small compared with the strongest walls.")
colSupport = input.color(#ffeb3b, "Support walls (below price)", group = grStyle)
colResist = input.color(#b2b5be, "Resistance walls (above price)", group = grStyle)
transWeak = input.int(60, "Transparency of weak walls", minval = 0, maxval = 100, group = grStyle)
transStrong = input.int(15, "Transparency of strong walls", minval = 0, maxval = 100, group = grStyle)
lineStyleIn = input.string("Dashed", "Line style", options = ["Solid", "Dashed", "Dotted"], group = grStyle)
lineWidth = input.int(1, "Line width", minval = 1, maxval = 5, group = grStyle)
widthByStrength = input.bool(false, "Thicker lines for stronger walls", group = grStyle)
showBorder = input.bool(false, "Border around clouds", group = grStyle)
extendRight = input.int(30, "Extend to the right (bars)", minval = 0, maxval = 500, group = grStyle)
lookBack = input.int(200, "Anchor older walls this many bars back", minval = 20, maxval = 4900, group = grStyle, tooltip = "Walls older than this are anchored here and extended to the left edge of the chart.")
grLabel = "Labels"
showAmount = input.bool(true, "Show amounts", group = grLabel)
labelMode = input.string("Nearest to price", "Which walls to label", options = ["Nearest to price", "Strongest", "All shown"], group = grLabel)
amountCount = input.int(10, "How many to label (per side)", minval = 0, maxval = 30, group = grLabel)
mergeGap = input.float(0.3, "Merge labels closer than (% of price)", minval = 0, maxval = 5, step = 0.1, group = grLabel, tooltip = "Labels this close to each other are combined into one (amounts added up). 0 = never merge.")
textSizeIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grLabel)
colText = input.color(#e8eaf0, "Text color", group = grLabel)
showLegend = input.bool(true, "Show legend", group = grLabel)
legendPos = input.string("Top right", "Legend position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grLabel)
// ───────────────────────────── Wall storage ─────────────────────────────
var step = math.log(1 + levelWidth / 100.0)
keyOf(float p) => int(math.round(math.log(p) / step))
priceOf(int k) => math.exp(k * step)
var map<int, float> walls = map.new<int, float>()
var map<int, int> born = map.new<int, int>()
MAX_WALLS = 1200
avgVol = ta.sma(volume, avgLen)
isLarge = not na(avgVol) and avgVol > 0 and volume >= volFactor * avgVol
addWall(float price, float amount) =>
if price > 0 and amount > 0
k = keyOf(price)
walls.put(k, nz(walls.get(k)) + amount)
if not born.contains(k)
born.put(k, bar_index)
if barstate.isconfirmed and bar_index >= last_bar_index - calcBars
if decayPct > 0
f = 1 - decayPct / 100.0
for k in walls.keys()
walls.put(k, walls.get(k) * f)
if maxAge > 0
for k in walls.keys()
if bar_index - nz(born.get(k), bar_index) > maxAge
walls.remove(k)
born.remove(k)
if isLarge
usd = volume * hlc3
if spreadMode == "Bar center (HLC3)" or high <= low
addWall(hlc3, usd)
else
kLow = keyOf(low)
kHigh = keyOf(high)
n = kHigh - kLow + 1
for k = kLow to kHigh
addWall(priceOf(k), usd / n)
if walls.size() > MAX_WALLS
ks = walls.keys()
vs = walls.values()
idx = array.sort_indices(vs, order.ascending)
for i = 0 to walls.size() - MAX_WALLS - 1
k = ks.get(idx.get(i))
walls.remove(k)
born.remove(k)
// ───────────────────────────── Drawing ─────────────────────────────
var array<box> boxes = array.new<box>()
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()
lineStyle = lineStyleIn == "Solid" ? line.style_solid : lineStyleIn == "Dashed" ? line.style_dashed : line.style_dotted
textSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
amountText(float v) =>
v >= 1e9 ? str.tostring(v / 1e9, "#.##") + "B" : v >= 1e6 ? str.tostring(v / 1e6, "#.#") + "M" : v >= 1e3 ? str.tostring(v / 1e3, "#") + "K" : str.tostring(v, "#")
placeLabels(array<float> prices, array<float> values) =>
pickP = array.new<float>()
pickV = array.new<float>()
m = prices.size()
if labelMode == "All shown"
for j = 0 to m - 1
pickP.push(prices.get(j))
pickV.push(values.get(j))
else
keyArr = array.new<float>()
for j = 0 to m - 1
keyArr.push(labelMode == "Nearest to price" ? math.abs(prices.get(j) - close) : -values.get(j))
order_ = array.sort_indices(keyArr, order.ascending)
for j = 0 to math.min(amountCount, m) - 1
pickP.push(prices.get(order_.get(j)))
pickV.push(values.get(order_.get(j)))
if pickP.size() > 0
byPrice = array.sort_indices(pickP, order.ascending)
groupP = 0.0
groupV = 0.0
groupW = 0.0
right = bar_index + extendRight
for j = 0 to byPrice.size() - 1
p = pickP.get(byPrice.get(j))
v = pickV.get(byPrice.get(j))
if groupV > 0 and (mergeGap <= 0 or (p - groupP / groupW) / p * 100 > mergeGap)
labels.push(label.new(right, groupP / groupW, amountText(groupV), style = label.style_label_left, color = color.new(color.black, 100), textcolor = colText, size = textSize))
groupP := 0.0
groupV := 0.0
groupW := 0.0
groupP += p * v
groupW += v
groupV += v
if groupV > 0
labels.push(label.new(right, groupP / groupW, amountText(groupV), style = label.style_label_left, color = color.new(color.black, 100), textcolor = colText, size = textSize))
drawSide(bool isSupport, float maxValue) =>
// isSupport: walls below the current close; otherwise walls above it
ks = walls.keys()
vs = walls.values()
selK = array.new<int>()
selV = array.new<float>()
for i = 0 to math.max(ks.size() - 1, -1)
if ks.size() == 0
break
p = priceOf(ks.get(i))
if (isSupport and p < close) or (not isSupport and p >= close)
isPeak = true
if peaksOnly
r = math.max(1, int(math.round(math.log(1 + peakRange / 100.0) / step)))
for d = -r to r
if d != 0 and nz(walls.get(ks.get(i) + d)) > vs.get(i)
isPeak := false
break
if not isPeak
continue
selK.push(ks.get(i))
selV.push(vs.get(i))
if selK.size() > 0 and maxValue > 0
idx = array.sort_indices(selV, order.descending)
n = math.min(wallCount, selK.size())
// selection: the strongest walls plus the walls nearest to the current price
pick = array.new<int>()
isNear = array.new<bool>()
for i = 0 to n - 1
pick.push(idx.get(i))
isNear.push(false)
if nearCount > 0
dist = array.new<float>()
for k in selK
dist.push(math.abs(priceOf(k) - close))
byDist = array.sort_indices(dist, order.ascending)
m = math.min(nearCount, selK.size())
for j = 0 to m - 1
d = byDist.get(j)
pos = pick.indexof(d)
if pos < 0
pick.push(d)
isNear.push(true)
else
isNear.set(pos, true)
shownPrice = array.new<float>()
shownValue = array.new<float>()
base = isSupport ? colSupport : colResist
for i = 0 to pick.size() - 1
k = selK.get(pick.get(i))
v = selV.get(pick.get(i))
near = isNear.get(i)
// near walls stay readable even when they are small
strength = near ? math.max(v / maxValue, 0.5) : v / maxValue
if near or strength * 100 >= minPct
p = priceOf(k)
bornAt = nz(born.get(k), bar_index)
left = math.max(bornAt, bar_index - lookBack)
ext = bornAt < bar_index - lookBack ? extend.left : extend.none
right = bar_index + extendRight
c = color.new(base, math.round(transWeak - (transWeak - transStrong) * strength))
if drawMode != "Lines"
boxes.push(box.new(left, p * (1 + levelWidth / 200.0), right, p / (1 + levelWidth / 200.0), border_color = showBorder ? color.new(base, transStrong) : na, bgcolor = c, extend = ext))
if drawMode != "Cloud"
w = widthByStrength ? math.max(1, int(math.round(lineWidth * (0.5 + strength)))) : lineWidth
lines.push(line.new(left, p, right, p, color = color.new(base, math.min(transStrong, 60)), style = lineStyle, width = w, extend = ext))
shownPrice.push(p)
shownValue.push(v)
if showAmount and amountCount > 0 and shownPrice.size() > 0
placeLabels(shownPrice, shownValue)
if barstate.islast
for b in boxes
b.delete()
for l in lines
l.delete()
for s in labels
s.delete()
boxes.clear()
lines.clear()
labels.clear()
maxAll = walls.size() > 0 ? walls.values().max() : 0.0
drawSide(true, maxAll)
drawSide(false, maxAll)
// ───────────────────────────── Legend ─────────────────────────────
var table legend = table.new(legendPos == "Top left" ? position.top_left : legendPos == "Bottom right" ? position.bottom_right : legendPos == "Bottom left" ? position.bottom_left : position.top_right, 1, 2, bgcolor = color.new(#0f1218, 15), border_width = 0)
if barstate.islast and showLegend
maxAll = walls.size() > 0 ? walls.values().max() : 0.0
table.cell(legend, 0, 0, "M·Alpha·Wall", text_color = colText, text_size = textSize)
table.cell(legend, 0, 1, "Large volume ≥ " + str.tostring(volFactor, "#.#") + "× average · strongest wall $" + amountText(maxAll), text_color = colText, text_size = textSize)