本页源码来自 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/
// © algotim
//@version=6
indicator("MACD Pullback Validation with Divergence Filters [algotim]", overlay=true)
// MACD Inputs
fastLength = input.int(12, "Fast Length")
slowLength = input.int(26, "Slow Length")
signalLength = input.int(9, "Signal Length")
lookback = input.int(10, "Pullback Lookback", minval=1)
// MACD Calculation
[macdLine, signalLine, hist] = ta.macd(close, fastLength, slowLength, signalLength)
// =====================================================
// HISTOGRAM MOMENTUM CONDITIONS
// =====================================================
// Positive histogram increasing
histBullish = hist > 0 and hist > hist[1]
// Negative histogram decreasing
histBearish = hist < 0 and hist < hist[1]
// =====================================================
// PULLBACK CONDITIONS
// =====================================================
// Bearish crossover above zero
bearishCrossAboveZero =
ta.crossunder(macdLine, signalLine) and
macdLine > 0 and
signalLine > 0
// Bullish crossover below zero
bullishCrossBelowZero =
ta.crossover(macdLine, signalLine) and
macdLine < 0 and
signalLine < 0
barsSinceBearishCross = ta.barssince(bearishCrossAboveZero)
barsSinceBullishCross = ta.barssince(bullishCrossBelowZero)
recentBearishCross =
not na(barsSinceBearishCross) and
barsSinceBearishCross <= lookback
recentBullishCross =
not na(barsSinceBullishCross) and
barsSinceBullishCross <= lookback
// =====================================================
// ZERO LINE CONDITIONS
// =====================================================
macdCrossAboveZero = ta.crossover(macdLine, 0)
macdCrossBelowZero = ta.crossunder(macdLine, 0)
// =====================================================
// DIVERGENCE SETTINGS
// =====================================================
lbR = input.int(title="Pivot Lookback Right", defval=5)
lbL = input.int(title="Pivot Lookback Left", defval=5)
rangeUpper = input.int(title="Max of Lookback Range", defval=60)
rangeLower = input.int(title="Min of Lookback Range", defval=5)
plotBullish = input.bool(title="Plot Bullish", defval=true)
plotHiddenBullish = input.bool(title="Plot Hidden Bullish", defval=false)
plotBearish = input.bool(title="Plot Bearish", defval=true)
plotHiddenBearish = input.bool(title="Plot Hidden Bearish", defval=false)
// Colors
bearishColor = color.red
bullishColor = color.green
hiddenBullishColor = color.new(color.green, 80)
hiddenBearishColor = color.new(color.red, 80)
textColor = color.white
noneColor = color.new(color.white, 100)
// MACD Histogram
osc = hist
// Pivot Detection
plFound = not na(ta.pivotlow(osc, lbL, lbR))
phFound = not na(ta.pivothigh(osc, lbL, lbR))
_inRange(cond) =>
bars = ta.barssince(cond)
rangeLower <= bars and bars <= rangeUpper
// =====================================================
// REGULAR BULLISH DIVERGENCE
// =====================================================
inRangePl = _inRange(plFound[1])
oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and inRangePl
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
bullishCondAlert = priceLL and oscHL and plFound
bullishCond = plotBullish and bullishCondAlert
// =====================================================
// HIDDEN BULLISH DIVERGENCE
// =====================================================
oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and inRangePl
priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)
hiddenBullishCondAlert = priceHL and oscLL and plFound
hiddenBullishCond = plotHiddenBullish and hiddenBullishCondAlert
// =====================================================
// REGULAR BEARISH DIVERGENCE
// =====================================================
inRangePh = _inRange(phFound[1])
oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and inRangePh
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)
bearishCondAlert = priceHH and oscLH and phFound
bearishCond = plotBearish and bearishCondAlert
// =====================================================
// HIDDEN BEARISH DIVERGENCE
// =====================================================
oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and inRangePh
priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)
hiddenBearishCondAlert = priceLH and oscHH and phFound
hiddenBearishCond = plotHiddenBearish and hiddenBearishCondAlert
// Divergence Alerts
alertcondition(bullishCondAlert, title="Regular Bullish Divergence", message="MACD Histogram Regular Bullish Divergence")
alertcondition(hiddenBullishCondAlert, title="Hidden Bullish Divergence", message="MACD Histogram Hidden Bullish Divergence")
alertcondition(bearishCondAlert, title="Regular Bearish Divergence", message="MACD Histogram Regular Bearish Divergence")
alertcondition(hiddenBearishCondAlert, title="Hidden Bearish Divergence", message="MACD Histogram Hidden Bearish Divergence")
// =====================================================
// FINAL SIGNALS
// =====================================================
recentBullishDiv = ta.barssince(bullishCondAlert) <= 30
recentBearishDiv = ta.barssince(bearishCondAlert) <= 30
// =====================================================
// SUPPORT / RESISTANCE FILTER
// =====================================================
enableSRFilter = input.bool(false, "Enable Support/Resistance Filter")
leftBars = input.int(10, "Left Bars", minval=1)
rightBars = input.int(10, "Right Bars", minval=1)
bodyHigh = math.max(open, close)
bodyLow = math.min(open, close)
pivotHigh = ta.pivothigh(bodyHigh, leftBars, rightBars)
pivotLow = ta.pivotlow(bodyLow, leftBars, rightBars)
var float latestPivotHighCandleHigh = na
var float latestPivotHighCandleLow = na
if not na(pivotHigh)
latestPivotHighCandleHigh := high[rightBars]
latestPivotHighCandleLow := low[rightBars]
label.new(
bar_index - rightBars,
high[rightBars],
"PH",
style=label.style_label_down,
color=color.red,
textcolor=color.white)
var float latestPivotLowCandleHigh = na
var float latestPivotLowCandleLow = na
if not na(pivotLow)
latestPivotLowCandleHigh := high[rightBars]
latestPivotLowCandleLow := low[rightBars]
label.new(
bar_index - rightBars,
low[rightBars],
"PL",
style=label.style_label_up,
color=color.lime,
textcolor=color.black)
// Latest support & resistance from pivot bodies
supportLevel = latestPivotHighCandleHigh
resistanceLevel = latestPivotHighCandleLow
supportZoneTop = supportLevel
supportZoneBottom = supportLevel
resistanceZoneTop = resistanceLevel
resistanceZoneBottom = resistanceLevel
touchSupport =
not na(supportLevel) and
low <= supportZoneTop
touchResistance =
not na(resistanceLevel) and
high >= resistanceZoneBottom
// =====================================================
// BOLLINGER BAND FILTER
// =====================================================
enableBBFilter = input.bool(false, "Enable Bollinger Band Filter")
bbLength = input.int(20, "BB Length")
bbMult = input.float(2.0, "BB StdDev", step=0.1)
bbBasis = ta.sma(close, bbLength)
bbDev = bbMult * ta.stdev(close, bbLength)
bbUpper = bbBasis + bbDev
bbLower = bbBasis - bbDev
// Close outside bands
bbBullishCondition = close < bbLower
bbBearishCondition = close > bbUpper
plot(enableBBFilter ? bbUpper : na, "BB Upper", color=color.new(color.red, 50))
plot(enableBBFilter ? bbBasis : na, "BB Basis", color=color.new(color.gray, 50))
plot(enableBBFilter ? bbLower : na, "BB Lower", color=color.new(color.green, 50))
baseBullishSignal =
histBullish and
macdCrossAboveZero and
recentBearishCross and
recentBullishDiv
baseBearishSignal =
histBearish and
macdCrossBelowZero and
recentBullishCross and
recentBearishDiv
bullishSignal =
baseBullishSignal and
(
not enableSRFilter or
touchSupport
) and
(
not enableBBFilter or
bbBullishCondition
)
bearishSignal =
baseBearishSignal and
(
not enableSRFilter or
touchResistance
) and
(
not enableBBFilter or
bbBearishCondition
)
// =====================================================
// PLOTS
// =====================================================
plotshape(
bullishSignal,
title="BULLISH",
location=location.belowbar,
style=shape.labelup,
color=color.lime,
text="BULLISH",
textcolor=color.white,
size=size.normal)
plotshape(
bearishSignal,
title="BEARISH",
location=location.abovebar,
style=shape.labeldown,
color=color.red,
text="BEARISH",
textcolor=color.white,
size=size.normal)
// =====================================================
// ALERTS
// =====================================================
alertcondition(
bullishSignal,
title="Bullish Signal",
message="MACD Bullish Signal")
alertcondition(
bearishSignal,
title="Bearish Signal",
message="MACD Bearish Signal")