← All scripts
IndicatorOpen sourceVersion 1

Swing Signals

00

This strategy is combines an automatically adjusted trend boundary, directional markers, and alerts to help organize trend-following analysis.

Swing Signals — screenshot 1 of 1

Screenshot 1 of 1 · Open image to view full size

About this script

Swing Signals This strategy is combines an automatically adjusted trend boundary, directional markers, and alerts to help organize trend-following analysis. Market-Adaptive Sensitivity Price and volume behavior contribute to a conviction reading. Strong directional conditions bring the boundary closer to price; weaker conditions increase its distance to reduce premature reversals. Trailing Trend Reference The Supertrend framework places its active line below price during bullish phases and above price during bearish phases. Within an established direction, the boundary advances with the trend rather than retreating with every pullback. This provides a moving reference for reviewing whether price continues to support the current directional assessment. A plotted line can inform a stop-management rule, but the indicator itself does not place or manage a broker order. Adaptive Reversal Confirmation Standard reversals require a closing-price breach that clears a conviction-adjusted threshold. Uncertain conditions demand greater displacement. A separate regime check can also trigger a transition before the usual boundary breach when market behavior changes sufficiently. Directional Signal Markers Green upward triangles identify confirmed bullish transitions. Red downward triangles identify confirmed bearish transitions. These markers record changes in the indicator’s directional state. Built-In Alerts Three conditions support monitoring: Buy Signal , Sell Signal , and Any Signal Change . The combined condition tracks transitions in both directions through one alert. Visual Customization Five preset themes and custom bullish/bearish colors coordinate the display. Gradient fill, candle coloring, and background shading have independent controls, while label size and transparency can be adjusted. When incorporating the indicator into a trading process, define what a state change means for your strategy. It might prompt a setup review, invalidate an existing directional assumption, or trigger a separately defined exit rule. Assigning these roles in advance makes signals easier to evaluate consistently. For example, record the conditions at each signal and then review what followed: continued movement, a brief correction, or another reversal. Keep entry decisions, protective orders, and position sizing explicit so that the chart display supports a repeatable process. Supertrend signals can be false, and adaptation does not remove that limitation. Evaluate the readings alongside price structure and other relevant evidence before treating a directional change as a trade opportunity.

Source code

Read and review the complete source before using it in your account.

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")

Version history

  1. Version 1

    First ver

    View this version →

Discussion · 0

Support this script and join the discussion from your broker’s trading terminal using your author profile.

    No comments yet.