本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("ADX + ADX Moving Average | Market Regimes", overlay=false)
// ============================================================================
// INPUTS
// ============================================================================
adxLen = input.int(14, "ADX Length", minval=2, group="ADX Settings")
threshold = input.float(25.0, "ADX Threshold", minval=1, group="ADX Settings")
adxMaLen = input.int(10, "ADX Moving Average Length", minval=1, group="ADX Settings")
adxMaType = input.string("EMA", "ADX Moving Average Type", options=["SMA", "EMA", "RMA", "WMA"], group="ADX Settings")
showRegimes = input.bool(true, "Show Background Regimes", group="Market Regimes")
showSignals = input.bool(true, "Show Bull / Bear Signals", group="Market Regimes")
// ============================================================================
// TRUE RANGE AND DIRECTIONAL MOVEMENT
// ============================================================================
tr = ta.tr(true)
upMove = high - high[1]
downMove = low[1] - low
plusDM = upMove > downMove and upMove > 0 ? upMove : 0.0
minusDM = downMove > upMove and downMove > 0 ? downMove : 0.0
// ============================================================================
// WILDER SMOOTHING
// ============================================================================
trSmoothed = ta.rma(tr, adxLen)
plusDMSmoothed = ta.rma(plusDM, adxLen)
minusDMSmoothed = ta.rma(minusDM, adxLen)
// ============================================================================
// DIRECTIONAL INDICES
// ============================================================================
plusDI = trSmoothed != 0 ? 100.0 * plusDMSmoothed / trSmoothed : 0.0
minusDI = trSmoothed != 0 ? 100.0 * minusDMSmoothed / trSmoothed : 0.0
// ============================================================================
// ADX CALCULATION
// ============================================================================
diSum = plusDI + minusDI
dx = diSum != 0 ? 100.0 * math.abs(plusDI - minusDI) / diSum : 0.0
adx = ta.rma(dx, adxLen)
// ============================================================================
// ADX MOVING AVERAGE
// ============================================================================
adxMA = switch adxMaType
"SMA" => ta.sma(adx, adxMaLen)
"EMA" => ta.ema(adx, adxMaLen)
"RMA" => ta.rma(adx, adxMaLen)
"WMA" => ta.wma(adx, adxMaLen)
=> ta.ema(adx, adxMaLen)
// ============================================================================
// MARKET REGIME CONDITIONS
// ============================================================================
strongAndRising = adx > threshold and adx > adxMA
strongButWeakening = adx > threshold and adx < adxMA
emergingTrend = adx < threshold and adx > adxMA
validADX = not na(adx) and not na(adxMA)
weakMarket = validADX and not strongAndRising and not strongButWeakening and not emergingTrend
// ============================================================================
// DIRECTION
// ============================================================================
bullishDirection = plusDI > minusDI
bearishDirection = minusDI > plusDI
// ============================================================================
// SIGNAL CONDITIONS
// ============================================================================
bullTrendSignal = strongAndRising and bullishDirection
bearTrendSignal = strongAndRising and bearishDirection
emergingBullTrend = emergingTrend and bullishDirection
emergingBearTrend = emergingTrend and bearishDirection
newBullTrend = bullTrendSignal and not bullTrendSignal[1]
newBearTrend = bearTrendSignal and not bearTrendSignal[1]
// ============================================================================
// ADX CROSSOVER CONDITIONS
// ============================================================================
adxCrossAboveMA = ta.crossover(adx, adxMA)
adxCrossBelowMA = ta.crossunder(adx, adxMA)
// ============================================================================
// INDICATOR PLOTS
// ============================================================================
plot(plusDI, title="+DI | Bullish Direction", color=color.green)
plot(minusDI, title="-DI | Bearish Direction", color=color.red)
plot(adx, title="ADX | Trend Strength", color=color.orange, linewidth=2)
plot(adxMA, title="ADX Moving Average", color=color.blue, linewidth=2)
hline(threshold, "ADX Threshold", color=color.gray, linestyle=hline.style_dashed)
// ============================================================================
// ADX / MA CROSSOVER MARKERS
// ============================================================================
plotshape(adxCrossAboveMA, title="ADX Strength Increasing", location=location.bottom, style=shape.circle, color=color.lime, size=size.tiny)
plotshape(adxCrossBelowMA, title="ADX Strength Decreasing", location=location.top, style=shape.circle, color=color.red, size=size.tiny)
// ============================================================================
// TREND SIGNAL MARKERS
// ============================================================================
plotshape(showSignals and newBullTrend, title="Strong Bull Trend", location=location.bottom, style=shape.triangleup, color=color.green, size=size.small, text="BULL")
plotshape(showSignals and newBearTrend, title="Strong Bear Trend", location=location.top, style=shape.triangledown, color=color.red, size=size.small, text="BEAR")
// ============================================================================
// NAMED BACKGROUND REGIMES
// Separate bgcolor() calls create individually named Style controls.
// ============================================================================
bgcolor(showRegimes and strongAndRising ? color.new(color.green, 92) : na, title="Strong Trend - Strengthening")
bgcolor(showRegimes and strongButWeakening ? color.new(color.orange, 92) : na, title="Strong Trend - Weakening")
bgcolor(showRegimes and emergingTrend ? color.new(color.blue, 94) : na, title="Emerging Trend")
bgcolor(showRegimes and weakMarket ? color.new(color.gray, 96) : na, title="Weak / Consolidating Market")
// ============================================================================
// ALERTS
// ============================================================================
alertcondition(adxCrossAboveMA, title="ADX Strength Increasing", message="ADX crossed above its moving average on {{ticker}}")
alertcondition(adxCrossBelowMA, title="ADX Strength Decreasing", message="ADX crossed below its moving average on {{ticker}}")
alertcondition(newBullTrend, title="Strong Bull Trend", message="Strong bullish ADX trend detected on {{ticker}}")
alertcondition(newBearTrend, title="Strong Bear Trend", message="Strong bearish ADX trend detected on {{ticker}}")
alertcondition(emergingBullTrend and not emergingBullTrend[1], title="Emerging Bull Trend", message="Emerging bullish ADX trend on {{ticker}}")
alertcondition(emergingBearTrend and not emergingBearTrend[1], title="Emerging Bear Trend", message="Emerging bearish ADX trend on {{ticker}}")