本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// ─────────────────────────────────────────────────────────────────
// ███████╗██████╗ ██████╗ ██╗ █████╗ ██████╗
// ██╔════╝██╔══██╗██╔═══██╗██║ ██╔══██╗██╔══██╗
// █████╗ ██║ ██║██║ ██║██║ ███████║██████╔╝
// ██╔══╝ ██║ ██║██║ ██║██║ ██╔══██║██╔══██╗
// ███████╗██████╔╝╚██████╔╝███████╗██║ ██║██████╔╝
// ╚══════╝╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝
// ─────────────────────────────────────────────────────────────────
// ███╗ ███╗ █████╗ ██████╗ ██╗ ██╗███████╗████████╗███████╗
// ████╗ ████║██╔══██╗██╔══██╗██║ ██╔╝██╔════╝╚══██╔══╝██╔════╝
// ██╔████╔██║███████║██████╔╝█████╔╝ █████╗ ██║ ███████╗
// ██║╚██╔╝██║██╔══██║██╔══██╗██╔═██╗ ██╔══╝ ██║ ╚════██║
// ██║ ╚═╝ ██║██║ ██║██║ ██║██║ ██╗███████╗ ██║ ███████║
// ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝
// ─────────────────────────────────────────────────────────────────
// © Edolab Markets
//@version=6
indicator("Edo Mitigation Blocks", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500)
// ================== SETTINGS — STRUCTURE ==================
profile = input.string("Swing", "Structure Profile", options = ["Scalper", "Swing", "Long Term"], group = "Structure", tooltip = "Scalper: fast swings (intraday)\nSwing: balanced (4H / D)\nLong Term: major swings only (W)")
obScan = input.int(20, "Impulse Lookback", minval = 3, maxval = 60, group = "Structure", tooltip = "Bars to search back for the candle that originated the impulse.")
maxMB = input.int(8, "Max Blocks per side", minval = 1, maxval = 30, group = "Structure")
mitMode = input.string("Wick", "Mitigation trigger", options = ["Wick", "Close"], group = "Structure", tooltip = "Wick: zone mitigated when a wick returns to it.\nClose: requires a candle close inside the zone.")
swingLen = profile == "Scalper" ? 5 : profile == "Long Term" ? 21 : 10
// ================== SETTINGS — STYLE ==================
bullCol = input.color(#26a69a, "Bullish block", group = "Style")
bearCol = input.color(#ef5350, "Bearish block", group = "Style")
pendOpacity = input.int(88, "Pending Opacity %", minval = 0, maxval = 95, step = 5, group = "Style", tooltip = "Active (not yet mitigated) blocks are dimmer; mitigated blocks are highlighted.")
mitOpacity = input.int(70, "Mitigated Opacity %", minval = 0, maxval = 95, step = 5, group = "Style")
labelSize = input.string("Small", "Label Size", options = ["Tiny", "Small", "Normal"], group = "Style")
themeMode = input.string("Dark", "Theme Mode", options = ["Dark", "Light"], group = "Style", tooltip = "Match your TradingView background theme.")
// ================== SETTINGS — PANEL ==================
showPanel = input.bool(true, "Show Panel", group = "Panel")
panelPos = input.string("Top Right", "Panel Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = "Panel")
panelSize = input.string("Small", "Panel Size", options = ["Tiny", "Small", "Normal"], group = "Panel")
// ================== THEME ==================
isDark = themeMode == "Dark"
textCol = isDark ? #ffffff : #131722
headerBg = isDark ? #2a2e39 : #d1d4dc
subBg = isDark ? #363a45 : #e0e3eb
cellBg = isDark ? #1e222d : #f0f3fa
cellTxt = isDark ? #d1d4dc : #363a45
lblSizeTxt = labelSize == "Tiny" ? size.tiny : labelSize == "Normal" ? size.normal : size.small
panSizeTxt = panelSize == "Tiny" ? size.tiny : panelSize == "Normal" ? size.normal : size.small
// ================== SWING DETECTION + STRUCTURE ==================
ph = ta.pivothigh(high, swingLen, swingLen)
pl = ta.pivotlow(low, swingLen, swingLen)
var float upperLevel = na
var float lowerLevel = na
var bool upperBroken = false
var bool lowerBroken = false
if not na(ph)
upperLevel := ph
upperBroken := false
if not na(pl)
lowerLevel := pl
lowerBroken := false
bullBreak = barstate.isconfirmed and not na(upperLevel) and not upperBroken and close > upperLevel
bearBreak = barstate.isconfirmed and not na(lowerLevel) and not lowerBroken and close < lowerLevel
if bullBreak
upperBroken := true
if bearBreak
lowerBroken := true
// ================== MITIGATION BLOCK STORE ==================
// A block is the origin candle of an impulse. Its focus is the moment price RETURNS to it
// (mitigation) — confirming the institutional orders there were absorbed.
var box[] mbBox = array.new<box>()
var label[] mbLbl = array.new<label>()
var bool[] mbIsBull = array.new<bool>()
var float[] mbTop = array.new<float>()
var float[] mbBtm = array.new<float>()
var int[] mbState = array.new<int>() // 0 pending, 1 mitigated, 2 broken
var int[] mbStart = array.new<int>()
makeBlock(bool isBull) =>
idx = -1
maxScan = math.min(obScan, bar_index - 1)
if maxScan >= 1
for j = 1 to maxScan
isBearCandle = close[j] < open[j]
isBullCandle = close[j] > open[j]
if isBull and isBearCandle
idx := j
break
if not isBull and isBullCandle
idx := j
break
if idx >= 0
t = high[idx]
b = low[idx]
col = isBull ? bullCol : bearCol
bx = box.new(bar_index - idx, t, bar_index + 10, b, border_color = color.new(col, 55), bgcolor = color.new(col, pendOpacity), border_width = 1, border_style = line.style_dotted)
lb = label.new(bar_index - idx, isBull ? b : t, isBull ? "Bull Block" : "Bear Block", style = isBull ? label.style_label_up : label.style_label_down, color = color.new(col, 88), textcolor = col, size = size.tiny)
mbBox.push(bx)
mbLbl.push(lb)
mbIsBull.push(isBull)
mbTop.push(t)
mbBtm.push(b)
mbState.push(0)
mbStart.push(bar_index)
if bullBreak
makeBlock(true)
if bearBreak
makeBlock(false)
// ================== MITIGATION + BREAK STATE ==================
bullMitigated = false
bearMitigated = false
blockBroken = false
if mbBox.size() > 0
for i = mbBox.size() - 1 to 0
st = mbState.get(i)
isB = mbIsBull.get(i)
t = mbTop.get(i)
b = mbBtm.get(i)
bx = mbBox.get(i)
col = isB ? bullCol : bearCol
if st != 2
box.set_right(bx, bar_index + 10)
if bar_index > mbStart.get(i) and barstate.isconfirmed
if st == 0
// pending -> mitigated when price returns into the zone
mit = isB ? (mitMode == "Close" ? close <= t and close >= b : low <= t) : (mitMode == "Close" ? close >= b and close <= t : high >= b)
if mit
mbState.set(i, 1)
box.set_bgcolor(bx, color.new(col, mitOpacity))
box.set_border_style(bx, line.style_solid)
box.set_border_width(bx, 2)
label.new(bar_index, isB ? b : t, "Mitigated", style = isB ? label.style_label_up : label.style_label_down, color = color.new(col, 80), textcolor = col, size = lblSizeTxt)
if isB
bullMitigated := true
else
bearMitigated := true
else
// mitigated -> broken when price closes through the far side
brk = isB ? close < b : close > t
if brk
mbState.set(i, 2)
box.set_bgcolor(bx, color.new(col, math.min(mitOpacity + 15, 95)))
box.set_border_style(bx, line.style_dashed)
box.set_border_width(bx, 1)
box.set_right(bx, bar_index)
blockBroken := true
// ================== TRIM PER SIDE ==================
trimSide(bool sideBull) =>
cnt = 0
if mbBox.size() > 0
for i = mbBox.size() - 1 to 0
if mbIsBull.get(i) == sideBull
cnt += 1
if cnt > maxMB
box.delete(mbBox.get(i))
label.delete(mbLbl.get(i))
mbBox.remove(i)
mbLbl.remove(i)
mbIsBull.remove(i)
mbTop.remove(i)
mbBtm.remove(i)
mbState.remove(i)
mbStart.remove(i)
trimSide(true)
trimSide(false)
// ================== PANEL ==================
if showPanel and barstate.islast
panelPosTV = switch panelPos
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
var table panel = table.new(panelPosTV, 2, 4, frame_color = isDark ? #363a45 : #b2b5be, frame_width = 1, border_color = isDark ? #434651 : #c8cbda, border_width = 1)
pending = 0
mitigated = 0
if mbBox.size() > 0
for i = 0 to mbBox.size() - 1
s = mbState.get(i)
if s == 0
pending += 1
else if s == 1
mitigated += 1
table.cell(panel, 0, 0, "EDO MITIGATION BLOCKS", text_color = textCol, bgcolor = headerBg, text_size = panSizeTxt)
table.merge_cells(panel, 0, 0, 1, 0)
table.cell(panel, 0, 1, "Pending", text_color = cellTxt, bgcolor = cellBg, text_size = panSizeTxt)
table.cell(panel, 1, 1, str.tostring(pending), text_color = textCol, bgcolor = cellBg, text_size = panSizeTxt)
table.cell(panel, 0, 2, "Mitigated", text_color = cellTxt, bgcolor = subBg, text_size = panSizeTxt)
table.cell(panel, 1, 2, str.tostring(mitigated), text_color = textCol, bgcolor = subBg, text_size = panSizeTxt)
table.cell(panel, 0, 3, "Bull / Bear pending", text_color = cellTxt, bgcolor = cellBg, text_size = panSizeTxt)
cntB = 0
cntS = 0
if mbBox.size() > 0
for i = 0 to mbBox.size() - 1
if mbState.get(i) == 0
if mbIsBull.get(i)
cntB += 1
else
cntS += 1
table.cell(panel, 1, 3, str.tostring(cntB) + " / " + str.tostring(cntS), text_color = textCol, bgcolor = cellBg, text_size = panSizeTxt)
// ================== ALERTS ==================
alertcondition(bullMitigated, "Bullish Block mitigated", "Edo Mitigation Blocks — Bullish block mitigated on {{ticker}}")
alertcondition(bearMitigated, "Bearish Block mitigated", "Edo Mitigation Blocks — Bearish block mitigated on {{ticker}}")
alertcondition(blockBroken, "Block broken", "Edo Mitigation Blocks — A mitigated block was broken on {{ticker}}")