OPEN-SOURCE SCRIPT
Aggiornato Swing Trade Defender

You're in a trade. Defender watches it for the five ways swing trades fail — plus an ATR trailing stop — and shows one status: OK, CAUTION, or EXIT.
Finding the trade is the easy half. The hard half is admitting when it stops working — and doing it before a small loss becomes a big one. Swing Trade Defender watches your open trade bar by bar and does the admitting for you.
Set your direction (long or short) and, if you want, your entry price. It handles the rest — it works on its own, nothing else required.
Why five flags instead of one exit signal?
Every one of these signals fires false alarms by itself — an RSI dip, a single heavy-volume red bar, a wick through the 20 EMA.
Real trade failure rarely announces itself through one signal; it shows up as agreement between independent ones: trend, structure, momentum, participation, and volatility.
So Defender treats them as a vote. One or two flags is a warning to pay attention; three is a failed trade by definition — enough independent evidence that the odds have flipped. The one exception is the ATR trail, which triggers EXIT on its own, because a stop is a stop. The voting logic — not any individual component — is the tool.
The five failure flags — each worth 1 point
- Trend break — price closes through the 20-EMA (the trend's home line) the wrong way.
- Structure break — price takes out the prior swing low (for a long), the first crack in the pattern.
- Momentum rollover — RSI falls back through 50 and keeps dropping.
- Distribution — a down bar on 1.5× average volume or more: big sellers showing their hand.
- Trail hit — price closes through the ATR trailing stop (a chandelier-style stop that ratchets up behind price, 2.5 × ATR-14 by default; ATR = average bar range).
All five flip automatically for shorts — there a close above the MA, a break of the prior swing high, rising RSI, and heavy buying (accumulation) are the danger signs.
How the status reads
- OK — no flags. Let it work.
- CAUTION (1–2 flags) — an orange triangle marks the bar. Hover it and a tooltip lists exactly which flags are up.
- EXIT — 3 or more flags (you can set the threshold), or any trail hit on its own. A red ✕ marks the bar, with a box listing what failed.
Markers sit above the bar for longs and below it for shorts, so they never crowd the side your risk is on.
The status panel
One table shows the whole picture: status, flag count (x/5), each flag's own line (hold/break), the live ATR trail price, your entry, and open P/L when you've set an entry price.
Alerts
Two built-in alert conditions — "Exit signal" and "Caution" — so TradingView can ping your phone instead of you watching the screen.
Settings — defaults work out of the box
Tuned for swing trades held days to weeks:
- Trend MA length (20), swing lookback (8 bars), RSI length (14) and rollover level (50), volume spike multiple (1.5×).
- ATR trail — length (14) and multiple (2.5).
- Flags needed for EXIT (3 by default, 1–5).
- Panel position/size, marker history on/off, optional background shading, dark/light theme, colors.
⚠ Educational risk-management tool. EXIT means the trade has failed by these rules — whether to act is always your decision. It does not place orders, and a flag is not a prediction. Not financial advice.
Note di rilascio
v1.1 — display fix: the Open P/L percentage was printed wrongThe Open P/L row was formatted with the wrong pattern. In a Pine format
string only # and 0 act as digit placeholders — any other digit is printed
literally, and the number is rounded to a whole number first. The panel used
"#.1", so:
a true +2.4% was printed as "+2.1%"
a true -0.75% was printed as "-1.1%"
Every open P/L reading ended in .1 regardless of the actual figure.
The P/L calculation itself was never wrong, and no signal, flag, status or
alert used the printed text — the EXIT and CAUTION logic is unaffected by
this update. Only the number shown in that one row changes, and it now
matches what your position is actually doing.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.