script("Swing Signals", kind="indicator") input atrLength = 14 input efficiencyLength = 20 input volumeLength = 30 input fastLength = 8 input slowLength = 21 input minMultiplier = 1.5 input maxMultiplier = 4 input flipMargin = 0.25 input useVolume = 1 input useRegime = 1 input regimeThreshold = 0.75 input warmupBars = 40 input bullColor = "#76F7B8" input bearColor = "#FF3D1A" input fillOpacity = 0.32 input showBackground = 0 input showLabels = 1 input showShapes = 0 atrPeriod = math.clamp(math.floor(atrLength), 1, 200) efficiencyPeriod = math.clamp(math.floor(efficiencyLength), 1, 200) volumePeriod = math.clamp(math.floor(volumeLength), 1, 200) fastPeriod = math.clamp(math.floor(fastLength), 1, 200) slowPeriod = math.clamp(math.floor(slowLength), 1, 200) barNumber = barIndex + 1 priceChange = barNumber > 1 ? close - close[1] : 0 trueRange = high - low if barNumber > 1 { trueRange = math.max(trueRange, math.max(math.abs(high - close[1]), math.abs(low - close[1]))) } atrValue = ta.atr(atrPeriod) safeAtr = math.max(atrValue, 0.000000000001) fastAverage = ta.ema(close, fastPeriod) slowAverage = ta.ema(close, slowPeriod) travel = ta.sum(math.abs(priceChange), efficiencyPeriod) efficiency = barNumber > efficiencyPeriod and travel > 0 ? math.clamp(math.abs(close - close[efficiencyPeriod]) / travel, 0, 1) : 0 momentum = math.clamp(math.abs(fastAverage - slowAverage) / safeAtr, 0, 1) volumeAverage = ta.sma(volume, volumePeriod) volumeScore = useVolume == 1 and volumeAverage > 0 and volume > 0 ? math.clamp(volume / volumeAverage / 2, 0, 1) : 0.5 conviction = math.clamp(0.5 * efficiency + 0.3 * momentum + 0.2 * volumeScore, 0, 1) minimumWidth = math.max(0.1, math.min(minMultiplier, maxMultiplier)) maximumWidth = math.max(minimumWidth, math.max(minMultiplier, maxMultiplier)) multiplier = maximumWidth - (maximumWidth - minimumWidth) * conviction midPrice = (high + low) / 2 lowerCandidate = midPrice - safeAtr * multiplier upperCandidate = midPrice + safeAtr * multiplier reversalBuffer = safeAtr * math.max(0, flipMargin) * (1 - conviction) readyBars = math.max(math.max(efficiencyPeriod + 1, volumePeriod), math.max(atrPeriod, warmupBars)) ready = barNumber >= readyBars regimeUp = useRegime == 1 and conviction >= regimeThreshold and fastAverage > slowAverage and priceChange > safeAtr regimeDown = useRegime == 1 and conviction >= regimeThreshold and fastAverage < slowAverage and priceChange < -safeAtr trend = barNumber == 1 ? (close >= open ? 1 : -1) : trend[1] trail = barNumber == 1 ? (trend == 1 ? lowerCandidate : upperCandidate) : trail[1] if barNumber > 1 { if ready and trend[1] == 1 and (close < trail[1] - reversalBuffer or regimeDown) { trend = -1 trail = upperCandidate } else { if ready and trend[1] == -1 and (close > trail[1] + reversalBuffer or regimeUp) { trend = 1 trail = lowerCandidate } else { trail = trend == 1 ? math.max(trail[1], lowerCandidate) : math.min(trail[1], upperCandidate) } } } buySignal = ready and trend == 1 and trend[1] == -1 sellSignal = ready and trend == -1 and trend[1] == 1 bullLine = ready and trend == 1 ? trail : na bearLine = ready and trend == -1 ? trail : na plot(bullLine, title="Bullish trail", color=bullColor, width=3, style="step") plot(bearLine, title="Bearish trail", color=bearColor, width=3, style="step") plotText(low, when=buySignal and showLabels == 1, text="▲ BUY", style="label", background=bullColor, color="#163D2B", position="below", fontSize=10, closedOnly=1) plotText(high, when=sellSignal and showLabels == 1, text="▼ SELL", style="label", background=bearColor, color="#FFFFFF", position="above", fontSize=10, closedOnly=1) bullPrice = ready and trend == 1 ? midPrice : na bearPrice = ready and trend == -1 ? midPrice : na fill(bullLine, bullPrice, color=bullColor, opacity=fillOpacity, gradient=1, style="step") fill(bearLine, bearPrice, color=bearColor, opacity=fillOpacity, gradient=1, style="step") bullBackground = ready and trend == 1 and showBackground == 1 bearBackground = ready and trend == -1 and showBackground == 1 bgcolor(bullBackground, color=bullColor, opacity=0.04) bgcolor(bearBackground, color=bearColor, opacity=0.04) plotShape(low, when=buySignal and showShapes == 1, shape="triangleUp", color=bullColor, position="below", size=8) plotShape(high, when=sellSignal and showShapes == 1, shape="triangleDown", color=bearColor, position="above", size=8) alertcondition(buySignal, title="Buy signal", message="Adaptive trend turned bullish") alertcondition(sellSignal, title="Sell signal", message="Adaptive trend turned bearish") alertcondition(buySignal or sellSignal, title="Signal change", message="Adaptive trend changed direction")