Combines medium-term Bollinger envelopes with an ADX-weighted trailing stop logic. The ADX controls how far the stop sits from price and how wide the potential turning/ranging zone becomes, so strong trends pull the stop tight while low ADX expands the zone to highlight likely consolidation or reversal areas.
Use bullTrail as a dynamic long stop: stay in bullish trades while price holds above it and exit/reverse if it closes below. Use bearTrail similarly for shorts. When turnTop and turnBot expand (low ADX), expect ranging or potential turning points; fading moves near those bands is safer than trend-following. In strong trends, rely on the tightened trail to ride the move and avoid fighting momentum.
// PRC_AdaptTS with ADX Filter
// https://www.prorealcode.com
// Trading with ProRealTime
// 2026-02-04
// Nicolas
// ====================================
iprice = customclose
period = 20
adxPeriod = 14
upperBand = BollingerUp[period](iprice)
lowerBand = BollingerDown[period](iprice)
midline = (upperBand + lowerBand) / 2
adxValue = ADX[adxPeriod]
trendStrength = (adxValue - 15) / 35
if trendStrength < 0 then
trendStrength = 0
elsif trendStrength > 1 then
trendStrength = 1
endif
rangeFactor = 1 - trendStrength
// Adaptive bases for the trailing stops
bullBase = lowerBand + (midline - lowerBand) * trendStrength
bearBase = upperBand - (upperBand - midline) * trendStrength
// Initialize trails on the first bar
if barindex = 0 then
bullTrail = bullBase
bearTrail = bearBase
endif
// Bullish adaptive trail: ratchets upward in uptrends
if iprice > bullBase then
bullTrail = max(bullBase, bullTrail[1])
else
bullTrail = bullBase
endif
// Bearish adaptive trail: ratchets downward in downtrends
if iprice < bearBase then
bearTrail = min(bearBase, bearTrail[1])
else
bearTrail = bearBase
endif
// Turning/ranging zone shrinks with strong trends and expands with weak trends
turnTop = midline + (upperBand - midline) * rangeFactor
turnBot = midline - (midline - lowerBand) * rangeFactor
if trend>=0 and iprice crosses under turnbot then
trend = -1
alphabull=0
alphabear=255
colorR=2
colorG=72
colorB=115
endif
if trend<=0 and iprice crosses over turntop then
trend = 1
alphabear=0
alphabull=255
colorR=165
colorG=42
colorB=42
endif
RETURN bullTrail COLOURED(colorR,colorG,colorB,alphabull) style(line,2), bearTrail COLOURED(colorR,colorG,colorB,alphabear) style(line,2)