本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MTC - Forex Estimated Price Areas", shorttitle = "MTC FX Areas", overlay = true, max_boxes_count = 500, max_lines_count = 100, max_labels_count = 100)
// MTC - Forex Estimated Price Areas · powered by Madeira Trading Club
// Splits the price range of the last N months into rows and counts how many times price
// touched each row. The most touched areas (peaks of that count) are drawn as zones.
// ───────────────────────── Inputs ─────────────────────────
grpLB = "Lookback"
lbMonths = input.int(60, "Lookback (months)", minval = 1, maxval = 60, group = grpLB)
lbMode = input.string("Rolling", "Window start", options = ["Rolling", "Start of month"], group = grpLB,
tooltip = "Rolling: exactly N months back from the last bar. Start of month: from the 1st day of the month N months ago.")
maxBars = input.int(50000, "Max bars in the window", minval = 100, maxval = 90000, group = grpLB,
tooltip = "Safety limit for low timeframes. On 1H, 60 months are about 31000 bars; on 4H about 7800; on Daily about 1300. The window can also be shorter than asked if the chart does not load that much history.")
grpC = "Calculation"
rowMode = input.string("Number of rows", "Row size", options = ["Number of rows", "Fixed pips"], group = grpC)
rows = input.int(120, "Number of rows", minval = 10, maxval = 400, group = grpC)
rowPips = input.float(10.0, "Row size (pips)", minval = 0.1, step = 1.0, group = grpC)
pipOvr = input.float(0.0, "Pip size (0 = auto)", minval = 0.0, step = 0.0001, group = grpC,
tooltip = "Auto: 10 × tick on forex (EURUSD → 0.0001), 1 tick on everything else. For 6E futures set 0.0001.")
touchSrc = input.string("High-Low range", "What counts as a touch", options = ["High-Low range", "Body (open-close)", "Close", "Wicks only", "Highs & lows"], group = grpC,
tooltip = "High-Low range: every row the bar traded through. Body: only the candle body. Close: only the closing price. Wicks only: rejected areas. Highs & lows: only the bar extremes (turning points).")
weightMode = input.string("Equal", "Weight", options = ["Equal", "Volume", "Recency", "Volume × recency"], group = grpC,
tooltip = "Recency gives more weight to recent bars (linear, from 0 at the window start to 1 at the last bar).")
smoothLen = input.int(2, "Smoothing (rows each side)", minval = 0, maxval = 20, group = grpC)
recalcTick = input.bool(false, "Recalculate on every tick", group = grpC,
tooltip = "Off: recalculates once per closed bar (lighter).")
grpZ = "Zones"
nZones = input.int(5, "Max number of zones", minval = 1, maxval = 20, group = grpZ)
minStrength = input.float(40.0, "Min strength (% of the most touched row)", minval = 0, maxval = 100, group = grpZ)
edgePct = input.float(70.0, "Zone edge (% of the zone peak)", minval = 10, maxval = 100, group = grpZ,
tooltip = "The zone grows from its peak row while the neighbouring rows stay above this % of the peak. Lower = wider zones.")
maxZoneRows = input.int(6, "Max zone height (rows)", minval = 1, maxval = 50, group = grpZ)
minSep = input.int(3, "Min gap between zones (rows)", minval = 0, maxval = 50, group = grpZ)
grpS = "Zone style"
colorMode = input.string("Strength gradient", "Color mode", options = ["Strength gradient", "Single color", "Above / below price"], group = grpS)
cStrong = input.color(#ff9800, "Strong zones", group = grpS)
cWeak = input.color(#2962ff, "Weak zones", group = grpS)
cSingle = input.color(#7e57c2, "Single color", group = grpS)
cAbove = input.color(#ef5350, "Zones above price", group = grpS)
cBelow = input.color(#26a69a, "Zones below price", group = grpS)
fillTransp = input.int(80, "Fill transparency", minval = 0, maxval = 100, group = grpS)
showBorder = input.bool(true, "Border", group = grpS)
borderStyle = input.string("Solid", "Border style", options = ["Solid", "Dashed", "Dotted"], group = grpS)
borderWidth = input.int(1, "Border width", minval = 1, maxval = 4, group = grpS)
borderTr = input.int(30, "Border transparency", minval = 0, maxval = 100, group = grpS)
extendR = input.bool(true, "Extend zones to the right", group = grpS)
drawFrom = input.string("Lookback start", "Draw zones from", options = ["Lookback start", "Last N bars"], group = grpS)
drawBars = input.int(150, "N bars", minval = 1, maxval = 5000, group = grpS)
showMid = input.bool(true, "Mid line", group = grpS)
midStyle = input.string("Dotted", "Mid line style", options = ["Solid", "Dashed", "Dotted"], group = grpS)
midWidth = input.int(1, "Mid line width", minval = 1, maxval = 4, group = grpS)
grpT = "Zone labels"
showZL = input.bool(true, "Show zone labels", group = grpT)
zlRank = input.bool(true, "Rank (#1 = most touched)", group = grpT)
zlPct = input.bool(true, "Strength %", group = grpT)
zlTouch = input.bool(true, "Number of touches", group = grpT)
zlRange = input.bool(false, "Price range", group = grpT)
zlDist = input.bool(false, "Distance to price (pips)", group = grpT)
zlPrefix = input.string("", "Prefix", group = grpT)
zlSide = input.string("Right", "Position", options = ["Right", "Left"], group = grpT)
zlOffset = input.int(5, "Right offset (bars)", minval = 0, maxval = 200, group = grpT)
zlSize = input.string("Small", "Size", options = ["Tiny", "Small", "Normal", "Large"], group = grpT)
zlUseCol = input.bool(true, "Text uses zone color", group = grpT)
zlTxtCol = input.color(color.gray, "Text color", group = grpT)
zlBg = input.bool(false, "Label background", group = grpT)
zlBgCol = input.color(color.new(color.gray, 85), "Background color", group = grpT)
grpP = "POC (most touched price)"
showPOC = input.bool(true, "Show POC line", group = grpP)
cPOC = input.color(#ffeb3b, "Color", group = grpP)
pocStyle = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpP)
pocWidth = input.int(2, "Width", minval = 1, maxval = 4, group = grpP)
pocLbl = input.bool(true, "POC label", group = grpP)
grpH = "Touch profile (histogram)"
showProf = input.bool(false, "Show touch profile", group = grpH)
profWidth = input.int(40, "Max width (bars)", minval = 5, maxval = 300, group = grpH)
profOffset = input.int(10, "Offset from last bar (bars)", minval = 0, maxval = 190, group = grpH)
cProf = input.color(color.new(color.gray, 70), "Color", group = grpH)
profZones = input.bool(true, "Rows inside zones use zone color", group = grpH)
grpTb = "Summary table"
showTb = input.bool(false, "Show table", group = grpTb)
tbPos = input.string("Top right", "Position", options = ["Top left", "Top right", "Bottom left", "Bottom right"], group = grpTb)
tbSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grpTb)
grpW = "Branding"
showMark = input.bool(true, "Show \"powered by Madeira Trading Club\"", group = grpW)
markPos = input.string("Bottom right", "Position", options = ["Top left", "Top right", "Bottom left", "Bottom right"], group = grpW)
markCol = input.color(color.new(color.gray, 20), "Text color", group = grpW)
// ───────────────────────── Helpers ─────────────────────────
styleOf(string s) =>
s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
sizeOf(string s) =>
s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
posOf(string s) =>
s == "Top left" ? position.top_left : s == "Top right" ? position.top_right : s == "Bottom left" ? position.bottom_left : position.bottom_right
pip = pipOvr > 0 ? pipOvr : syminfo.type == "forex" ? syminfo.mintick * 10 : syminfo.mintick
// start of the lookback window for a given bar time
windowStart(int t) =>
tot = year(t) * 12 + month(t) - 1 - lbMonths
yy = int(math.floor(tot / 12))
mm = tot - yy * 12 + 1
lbMode == "Start of month" ? timestamp(syminfo.timezone, yy, mm, 1, 0, 0) : timestamp(syminfo.timezone, yy, mm, math.min(dayofmonth(t), 28), hour(t), minute(t))
// adds weight w to every row between prices a and b
addRange(array<float> cnt, float a, float b, float lo, float step, int nR, float w) =>
i1 = math.max(0, math.min(nR - 1, int(math.floor((math.min(a, b) - lo) / step))))
i2 = math.max(0, math.min(nR - 1, int(math.floor((math.max(a, b) - lo) / step))))
for k = i1 to i2
cnt.set(k, cnt.get(k) + w)
// ───────────────────────── Window of bars ─────────────────────────
var aT = array.new<int>()
var aO = array.new<float>()
var aH = array.new<float>()
var aL = array.new<float>()
var aC = array.new<float>()
var aV = array.new<float>()
aT.push(time)
aO.push(open)
aH.push(high)
aL.push(low)
aC.push(close)
aV.push(nz(volume))
startT = windowStart(time)
while aT.size() > 0 and (aT.first() < startT or aT.size() > maxBars)
aT.shift()
aO.shift()
aH.shift()
aL.shift()
aC.shift()
aV.shift()
// ───────────────────────── Drawings kept between recalculations ─────────────────────────
var zBoxes = array.new<box>()
var zLines = array.new<line>()
var zLabels = array.new<label>()
var pBoxes = array.new<box>()
var line pocLine = na
var label pocLab = na
var table tb = table.new(posOf(tbPos), 5, 21, bgcolor = color.new(#263238, 15), border_width = 1)
var table mark = table.new(posOf(markPos), 1, 1)
doCalc = barstate.islast and (recalcTick or barstate.islastconfirmedhistory or barstate.isconfirmed or not barstate.isrealtime)
if doCalc and aT.size() > 1
// clear previous drawings
for b in zBoxes
b.delete()
for l in zLines
l.delete()
for lb in zLabels
lb.delete()
for b in pBoxes
b.delete()
zBoxes.clear()
zLines.clear()
zLabels.clear()
pBoxes.clear()
line.delete(pocLine)
label.delete(pocLab)
tb.clear(0, 0, 4, 20)
n = aT.size()
lo = aL.min()
hi = aH.max()
float step = math.max(rowMode == "Fixed pips" ? rowPips * pip : (hi - lo) / rows, syminfo.mintick)
// at most 400 rows
step := math.max(step, (hi - lo) / 400)
int nR = math.max(1, int(math.ceil((hi - lo) / step)))
// ── touch count per row
raw = array.new<float>(nR, 0.0)
useVol = (weightMode == "Volume" or weightMode == "Volume × recency") and aV.sum() > 0
useRec = weightMode == "Recency" or weightMode == "Volume × recency"
for i = 0 to n - 1
o = aO.get(i)
h = aH.get(i)
l = aL.get(i)
c = aC.get(i)
w = (useVol ? aV.get(i) : 1.0) * (useRec ? (i + 1.0) / n : 1.0)
if touchSrc == "High-Low range"
addRange(raw, l, h, lo, step, nR, w)
else if touchSrc == "Body (open-close)"
addRange(raw, o, c, lo, step, nR, w)
else if touchSrc == "Close"
addRange(raw, c, c, lo, step, nR, w)
else if touchSrc == "Wicks only"
addRange(raw, math.max(o, c), h, lo, step, nR, w)
addRange(raw, l, math.min(o, c), lo, step, nR, w)
else
addRange(raw, h, h, lo, step, nR, w)
addRange(raw, l, l, lo, step, nR, w)
// ── smoothing
sm = array.new<float>(nR, 0.0)
for k = 0 to nR - 1
s = 0.0
cntK = 0
for j = math.max(0, k - smoothLen) to math.min(nR - 1, k + smoothLen)
s += raw.get(j)
cntK += 1
sm.set(k, s / cntK)
maxC = sm.max()
if maxC > 0
// ── peaks above the minimum strength
candV = array.new<float>()
candI = array.new<int>()
for k = 0 to nR - 1
v = sm.get(k)
left = k > 0 ? sm.get(k - 1) : -1.0
right = k < nR - 1 ? sm.get(k + 1) : -1.0
if v >= minStrength / 100 * maxC and v >= left and v > right
candV.push(v)
candI.push(k)
// ── zones: strongest peak first, grow while rows stay above the edge %, skip overlaps
zBot = array.new<int>()
zTop = array.new<int>()
zVal = array.new<float>()
zCol = array.new<color>()
if candV.size() > 0
ranked = candV.sort_indices(order.descending)
for idx in ranked
if zBot.size() >= nZones
break
k = candI.get(idx)
v = candV.get(idx)
b = k
t = k
growing = true
while growing and t - b + 1 < maxZoneRows
growing := false
if b > 0 and sm.get(b - 1) >= edgePct / 100 * v
b -= 1
growing := true
if t - b + 1 < maxZoneRows and t < nR - 1 and sm.get(t + 1) >= edgePct / 100 * v
t += 1
growing := true
clash = false
for z = 0 to math.max(0, zBot.size() - 1)
if zBot.size() > 0 and not (t + minSep < zBot.get(z) or b - minSep > zTop.get(z))
clash := true
if not clash
zBot.push(b)
zTop.push(t)
zVal.push(v)
// ── draw zones
tStart = drawFrom == "Lookback start" ? aT.first() : aT.get(math.max(0, n - drawBars))
nz_ = zBot.size()
if nz_ > 0
for z = 0 to nz_ - 1
zb = lo + zBot.get(z) * step
zt = lo + (zTop.get(z) + 1) * step
pct = zVal.get(z) / maxC * 100
mid = (zb + zt) / 2
col = colorMode == "Single color" ? cSingle : colorMode == "Above / below price" ? (mid > close ? cAbove : cBelow) :
color.from_gradient(pct, minStrength, 100, cWeak, cStrong)
zCol.push(col)
// bars whose high-low range touched the zone
touches = 0
for i = 0 to n - 1
if aH.get(i) >= zb and aL.get(i) <= zt
touches += 1
zBoxes.push(box.new(tStart, zt, time, zb, xloc = xloc.bar_time, extend = extendR ? extend.right : extend.none,
border_color = showBorder ? color.new(col, borderTr) : na, border_width = borderWidth,
border_style = styleOf(borderStyle), bgcolor = color.new(col, fillTransp)))
if showMid
zLines.push(line.new(tStart, mid, time, mid, xloc = xloc.bar_time, extend = extendR ? extend.right : extend.none,
color = color.new(col, borderTr), style = styleOf(midStyle), width = midWidth))
if showZL
txt = zlPrefix
if zlRank
txt += (txt == "" ? "" : " ") + "#" + str.tostring(z + 1)
if zlPct
txt += (txt == "" ? "" : " · ") + str.tostring(pct, "#") + "%"
if zlTouch
txt += (txt == "" ? "" : " · ") + str.tostring(touches) + " touches"
if zlRange
txt += (txt == "" ? "" : " · ") + str.tostring(zb, format.mintick) + " – " + str.tostring(zt, format.mintick)
if zlDist
d = close > zt ? close - zt : close < zb ? zb - close : 0.0
txt += (txt == "" ? "" : " · ") + str.tostring(d / pip, "#.#") + " pips"
tCol = zlUseCol ? col : zlTxtCol
bCol = zlBg ? zlBgCol : color.new(color.black, 100)
if zlSide == "Right"
zLabels.push(label.new(bar_index + zlOffset, mid, txt, xloc = xloc.bar_index, style = label.style_label_left,
color = bCol, textcolor = tCol, size = sizeOf(zlSize)))
else
zLabels.push(label.new(tStart, mid, txt, xloc = xloc.bar_time, style = label.style_label_left,
color = bCol, textcolor = tCol, size = sizeOf(zlSize)))
if showTb
ts = tbSize == "Tiny" ? size.tiny : tbSize == "Normal" ? size.normal : size.small
tb.cell(0, z + 1, "#" + str.tostring(z + 1), text_color = col, text_size = ts)
tb.cell(1, z + 1, str.tostring(zb, format.mintick) + " – " + str.tostring(zt, format.mintick), text_color = color.white, text_size = ts)
tb.cell(2, z + 1, str.tostring(pct, "#") + "%", text_color = color.white, text_size = ts)
tb.cell(3, z + 1, str.tostring(touches), text_color = color.white, text_size = ts)
d = close > zt ? close - zt : close < zb ? -(zb - close) : 0.0
tb.cell(4, z + 1, str.tostring(-d / pip, "#.#"), text_color = color.white, text_size = ts)
if showTb
ts = tbSize == "Tiny" ? size.tiny : tbSize == "Normal" ? size.normal : size.small
tb.cell(0, 0, "Zone", text_color = color.gray, text_size = ts)
tb.cell(1, 0, "Price area", text_color = color.gray, text_size = ts)
tb.cell(2, 0, "Strength", text_color = color.gray, text_size = ts)
tb.cell(3, 0, "Touches", text_color = color.gray, text_size = ts)
tb.cell(4, 0, "Pips from price", text_color = color.gray, text_size = ts)
// ── POC
pocRow = sm.indexof(maxC)
pocPx = lo + (pocRow + 0.5) * step
if showPOC
pocLine := line.new(tStart, pocPx, time, pocPx, xloc = xloc.bar_time, extend = extendR ? extend.right : extend.none,
color = cPOC, style = styleOf(pocStyle), width = pocWidth)
if pocLbl
pocLab := label.new(bar_index + zlOffset, pocPx, "POC " + str.tostring(pocPx, format.mintick), xloc = xloc.bar_index,
style = label.style_label_left, color = color.new(color.black, 100), textcolor = cPOC, size = sizeOf(zlSize))
// ── touch profile (right aligned, at most 400 rows)
if showProf
x2 = bar_index + profOffset + profWidth
for k = 0 to nR - 1
wBars = int(math.round(sm.get(k) / maxC * profWidth))
if wBars > 0 and pBoxes.size() < 400
pc = cProf
if profZones and zBot.size() > 0
for z = 0 to zBot.size() - 1
if k >= zBot.get(z) and k <= zTop.get(z)
pc := color.new(zCol.get(z), 40)
pBoxes.push(box.new(x2 - wBars, lo + (k + 1) * step, x2, lo + k * step, xloc = xloc.bar_index,
border_width = 0, bgcolor = pc))
// ───────────────────────── Branding ─────────────────────────
if showMark and barstate.islast
mark.cell(0, 0, "MTC · powered by Madeira Trading Club", text_color = markCol, text_size = size.small)