本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("DivMacdObFvg", shorttitle="DivMacdObFvg", overlay=false, precision=4, max_lines_count=500, max_boxes_count=500)
// ============================================================
// === INPUTS =================================================
// ============================================================
// --- MACD ---
fastLength = input.int(12, "Fast Length", minval=1, group="MACD")
slowLength = input.int(26, "Slow Length", minval=1, group="MACD")
signalLength = input.int(9, "Signal Length", minval=2, group="MACD")
histFourColors = input.bool(true, "Histogram 4 Colors", group="MACD")
showExtremes = input.bool(true, "Show Histogram Highs/Lows Signals", group="MACD")
// --- Lower timeframe confirmation ---
lowerTf = input.timeframe("60", "Confirmation Timeframe (must be below chart TF)", group="Confirmation")
confirmBars = input.int(1, "Lower TF Closes to Wait (1-3)", minval=1, maxval=3, group="Confirmation")
// --- Divergences ---
showDiv = input.bool(true, "Show Divergences", group="Divergences")
showHidden = input.bool(false, "Show Hidden Divergences", group="Divergences")
lbL = input.int(5, "Pivot: Bars Left", minval=1, group="Divergences")
lbR = input.int(3, "Pivot: Bars Right (confirmation delay)", minval=1, group="Divergences")
rangeMin = input.int(5, "Min Distance Between Pivots (bars)", minval=1, group="Divergences")
rangeMax = input.int(60, "Max Distance Between Pivots (bars)", minval=1, group="Divergences")
onlyZeroSide = input.bool(true, "Bullish only if hist<0, Bearish only if hist>0", group="Divergences")
// --- FVG / Order Blocks ---
showFVG = input.bool(true, "Show FVG", group="FVG / OB")
fvgMinAtr = input.float(0.3, "FVG: Min Size (x ATR14)", minval=0.0, step=0.1, group="FVG / OB")
fvgMax = input.int(15, "FVG: Max Active", minval=1, maxval=100, group="FVG / OB")
fvgDelete = input.bool(true, "FVG: Delete When Filled (otherwise stop extending)", group="FVG / OB")
fvgBullCol = input.color(color.new(color.lime, 80), "Bullish FVG", group="FVG / OB")
fvgBearCol = input.color(color.new(color.orange, 80), "Bearish FVG", group="FVG / OB")
showOB = input.bool(true, "Show Order Blocks", group="FVG / OB")
obSwingLen = input.int(5, "OB: Swing Length (pivot)", minval=2, group="FVG / OB")
obMax = input.int(10, "OB: Max Active", minval=1, maxval=100, group="FVG / OB")
obDelete = input.bool(true, "OB: Delete When Mitigated (otherwise stop extending)", group="FVG / OB")
obBullCol = input.color(color.new(color.blue, 65), "Bullish OB", group="FVG / OB")
obBearCol = input.color(color.new(color.purple, 65), "Bearish OB", group="FVG / OB")
// ============================================================
// === MACD ON CHART TIMEFRAME ================================
// ============================================================
float fastEma = ta.ema(close, fastLength)
float slowEma = ta.ema(close, slowLength)
float macd = fastEma - slowEma
float signal = ta.sma(macd, signalLength)
float hist = macd - signal
// ============================================================
// === PROVISIONAL HISTOGRAM FROM LOWER TF CLOSES =============
// ============================================================
array<float> ltfCloses = request.security_lower_tf(syminfo.tickerid, lowerTf, close)
float alphaF = 2.0 / (fastLength + 1)
float alphaS = 2.0 / (slowLength + 1)
float sumPrev = math.sum(macd[1], signalLength - 1)
float provHist = na
if array.size(ltfCloses) > confirmBars
float c = array.get(ltfCloses, confirmBars - 1)
float pf = alphaF * c + (1 - alphaF) * nz(fastEma[1])
float ps = alphaS * c + (1 - alphaS) * nz(slowEma[1])
float pm = pf - ps
float psig = (sumPrev + pm) / signalLength
provHist := pm - psig
// ============================================================
// === HISTOGRAM COLORS =======================================
// ============================================================
bool histA_IsUp = hist > nz(hist[1]) and hist > 0
bool histA_IsDown = hist < nz(hist[1]) and hist > 0
bool histB_IsDown = hist < nz(hist[1]) and hist <= 0
bool histB_IsUp = hist > nz(hist[1]) and hist <= 0
color hist_color = histFourColors ? (histA_IsUp ? color.aqua : histA_IsDown ? color.blue : histB_IsDown ? color.red : histB_IsUp ? color.maroon : color.yellow) : color.gray
// ============================================================
// === HIGHS / LOWS CONFIRMED BY LOWER TF =====================
// ============================================================
bool isPeak = not na(provHist) and hist[1] > hist[2] and provHist <= hist[1] and hist[1] > 0
bool isTrough = not na(provHist) and hist[1] < hist[2] and provHist >= hist[1] and hist[1] < 0
// ============================================================
// === PRICE / HISTOGRAM DIVERGENCES ==========================
// ============================================================
float pivLo = ta.pivotlow(hist, lbL, lbR)
float pivHi = ta.pivothigh(hist, lbL, lbR)
var float pLoHist = na
var float pLoPrice = na
var int pLoBar = na
var float pHiHist = na
var float pHiPrice = na
var int pHiBar = na
bool bullReg = false
bool bullHid = false
bool bearReg = false
bool bearHid = false
if not na(pivLo)
float curPrice = low[lbR]
int curBar = bar_index - lbR
if not na(pLoBar)
int dist = curBar - pLoBar
bool ok = dist >= rangeMin and dist <= rangeMax
bullReg := ok and curPrice < pLoPrice and pivLo > pLoHist and (not onlyZeroSide or pivLo < 0)
bullHid := ok and curPrice > pLoPrice and pivLo < pLoHist and (not onlyZeroSide or pivLo < 0)
if showDiv and (bullReg or (showHidden and bullHid))
color lc = bullReg ? color.lime : color.new(color.lime, 50)
line.new(pLoBar, pLoHist, curBar, pivLo, color=lc, width=2)
line.new(pLoBar, pLoPrice, curBar, curPrice, color=lc, width=2, force_overlay=true)
pLoHist := pivLo
pLoPrice := curPrice
pLoBar := curBar
if not na(pivHi)
float curPrice = high[lbR]
int curBar = bar_index - lbR
if not na(pHiBar)
int dist = curBar - pHiBar
bool ok = dist >= rangeMin and dist <= rangeMax
bearReg := ok and curPrice > pHiPrice and pivHi < pHiHist and (not onlyZeroSide or pivHi > 0)
bearHid := ok and curPrice < pHiPrice and pivHi > pHiHist and (not onlyZeroSide or pivHi > 0)
if showDiv and (bearReg or (showHidden and bearHid))
color lc = bearReg ? color.red : color.new(color.red, 50)
line.new(pHiBar, pHiHist, curBar, pivHi, color=lc, width=2)
line.new(pHiBar, pHiPrice, curBar, curPrice, color=lc, width=2, force_overlay=true)
pHiHist := pivHi
pHiPrice := curPrice
pHiBar := curBar
// ============================================================
// === FVG ON PRICE CHART =====================================
// ============================================================
float atrV = ta.atr(14)
var array<box> fvgBoxes = array.new<box>()
var array<int> fvgDir = array.new<int>()
// update / close existing ones
if showFVG and array.size(fvgBoxes) > 0
for i = array.size(fvgBoxes) - 1 to 0
box b = array.get(fvgBoxes, i)
int d = array.get(fvgDir, i)
bool filled = d == 1 ? close < box.get_bottom(b) : close > box.get_top(b)
box.set_right(b, bar_index)
if filled
if fvgDelete
box.delete(b)
array.remove(fvgBoxes, i)
array.remove(fvgDir, i)
// new FVGs (candle [1] is the impulsive one, gap between high[2] and low)
bool bullFvg = low > high[2] and close[1] > high[2] and (low - high[2]) > atrV * fvgMinAtr
bool bearFvg = high < low[2] and close[1] < low[2] and (low[2] - high) > atrV * fvgMinAtr
if showFVG and bullFvg
array.push(fvgBoxes, box.new(bar_index - 2, low, bar_index, high[2], border_color=na, bgcolor=fvgBullCol, force_overlay=true))
array.push(fvgDir, 1)
if showFVG and bearFvg
array.push(fvgBoxes, box.new(bar_index - 2, low[2], bar_index, high, border_color=na, bgcolor=fvgBearCol, force_overlay=true))
array.push(fvgDir, -1)
if array.size(fvgBoxes) > fvgMax
box.delete(array.shift(fvgBoxes))
array.shift(fvgDir)
// ============================================================
// === ORDER BLOCKS ON PRICE CHART ============================
// ============================================================
// Bullish OB: candle with the lowest low between the pivot high and its break
// Bearish OB: candle with the highest high between the pivot low and its break
var array<box> obBoxes = array.new<box>()
var array<int> obDir = array.new<int>()
float obPH = ta.pivothigh(high, obSwingLen, obSwingLen)
float obPL = ta.pivotlow(low, obSwingLen, obSwingLen)
var float lastPH = na
var int lastPHbar = na
var bool phBroken = true
var float lastPL = na
var int lastPLbar = na
var bool plBroken = true
if not na(obPH)
lastPH := obPH
lastPHbar := bar_index - obSwingLen
phBroken := false
if not na(obPL)
lastPL := obPL
lastPLbar := bar_index - obSwingLen
plBroken := false
// mitigation of existing OBs
if showOB and array.size(obBoxes) > 0
for i = array.size(obBoxes) - 1 to 0
box b = array.get(obBoxes, i)
int d = array.get(obDir, i)
bool mit = d == 1 ? close < box.get_bottom(b) : close > box.get_top(b)
box.set_right(b, bar_index)
if mit
if obDelete
box.delete(b)
array.remove(obBoxes, i)
array.remove(obDir, i)
// upside break -> bullish OB
if showOB and not phBroken and not na(lastPH) and close > lastPH
phBroken := true
int span = math.min(bar_index - lastPHbar, 400)
int idx = 0
float lowest = low
for k = 0 to span
if low[k] < lowest
lowest := low[k]
idx := k
array.push(obBoxes, box.new(bar_index - idx, high[idx], bar_index, low[idx], border_color=color.blue, bgcolor=obBullCol, force_overlay=true))
array.push(obDir, 1)
// downside break -> bearish OB
if showOB and not plBroken and not na(lastPL) and close < lastPL
plBroken := true
int span = math.min(bar_index - lastPLbar, 400)
int idx = 0
float highest = high
for k = 0 to span
if high[k] > highest
highest := high[k]
idx := k
array.push(obBoxes, box.new(bar_index - idx, high[idx], bar_index, low[idx], border_color=color.purple, bgcolor=obBearCol, force_overlay=true))
array.push(obDir, -1)
if array.size(obBoxes) > obMax
box.delete(array.shift(obBoxes))
array.shift(obDir)
// ============================================================
// === PLOTS ==================================================
// ============================================================
plot(hist, "Histogram", color=hist_color, style=plot.style_columns)
hline(0, "Zero Line", color=color.white, linestyle=hline.style_solid, linewidth=2)
// Highs / lows (offset=-1: dot on the bar that actually made the extreme)
plot(showExtremes and isPeak ? hist[1] : na, "Histogram High", color=color.yellow, style=plot.style_circles, linewidth=4, offset=-1)
plot(showExtremes and isTrough ? hist[1] : na, "Histogram Low", color=color.fuchsia, style=plot.style_circles, linewidth=4, offset=-1)
// Divergence markers on the histogram
plotshape(showDiv and bullReg ? pivLo : na, "Bullish Div (MACD)", location=location.absolute, style=shape.diamond, color=color.lime, size=size.tiny, offset=-lbR)
plotshape(showDiv and bearReg ? pivHi : na, "Bearish Div (MACD)", location=location.absolute, style=shape.diamond, color=color.red, size=size.tiny, offset=-lbR)
plotshape(showDiv and showHidden and bullHid ? pivLo : na, "Hidden Bullish Div (MACD)", location=location.absolute, style=shape.diamond, color=color.new(color.lime, 50), size=size.tiny, offset=-lbR)
plotshape(showDiv and showHidden and bearHid ? pivHi : na, "Hidden Bearish Div (MACD)", location=location.absolute, style=shape.diamond, color=color.new(color.red, 50), size=size.tiny, offset=-lbR)
// Divergence markers on the price chart
plotshape(showDiv and bullReg, "Bullish Div (Price)", location=location.belowbar, style=shape.labelup, color=color.lime, textcolor=color.black, text="Div↑", size=size.tiny, offset=-lbR, force_overlay=true)
plotshape(showDiv and bearReg, "Bearish Div (Price)", location=location.abovebar, style=shape.labeldown, color=color.red, textcolor=color.white, text="Div↓", size=size.tiny, offset=-lbR, force_overlay=true)
plotshape(showDiv and showHidden and bullHid, "Hidden Bullish Div (Price)", location=location.belowbar, style=shape.labelup, color=color.new(color.lime, 50), textcolor=color.black, text="H↑", size=size.tiny, offset=-lbR, force_overlay=true)
plotshape(showDiv and showHidden and bearHid, "Hidden Bearish Div (Price)", location=location.abovebar, style=shape.labeldown, color=color.new(color.red, 50), textcolor=color.white, text="H↓", size=size.tiny, offset=-lbR, force_overlay=true)
// ============================================================
// === ALERTS =================================================
// ============================================================
alertcondition(isPeak, "Histogram High", "MACD hist 4h: high confirmed by 1h")
alertcondition(isTrough, "Histogram Low", "MACD hist 4h: low confirmed by 1h")
alertcondition(bullReg, "Bullish Divergence", "Bullish divergence price/MACD hist")
alertcondition(bearReg, "Bearish Divergence", "Bearish divergence price/MACD hist")
alertcondition(bullFvg, "Bullish FVG", "New bullish FVG")
alertcondition(bearFvg, "Bearish FVG", "New bearish FVG")