OPEN-SOURCE SCRIPT
Crossover Whipsaw Guard

**Crossover Whipsaw Guard**
**What it does**
Turns a moving-average crossover into a two-step state: *pending* the moment the fast MA crosses the slow one, *confirmed* once the two averages have separated by at least a minimum percentage (default 0.30 % of the slow MA). Optional extra conditions for confirmation: volume above its 20-bar average, and a higher high plus higher low over the last N bars (mirrored for bearish crosses). A confirmed state holds until the averages cross back; it is never downgraded.
**What you see**
- Both averages (defaults EMA 9 and DEMA 200; EMA, SMA and DEMA selectable for each).
- The area between them filled gray while pending, teal or red once confirmed.
- A small dot on the bar where confirmation happened.
- A table with the current separation, the threshold, active extra filters and the state.
- Alerts for each of the four transitions: bullish pending, bullish confirmed, bearish pending, bearish confirmed.
**Why I built it — the measurement behind it**
Crossovers of a fast and a slow average produce many entries that get stopped out within days. I measured which conditions at the crossover bar reduced that, on 47,013 daily bullish crossovers (EMA 9 over DEMA 200) across 1,758 US stocks, 2014–2026, including later-delisted names, each followed by the same trailing exit:
| Condition at the crossover bar | Share of crossovers kept | Share of trades ending positive | vs. the crossovers it removed |
|---|---:|---:|---:|
| none (all crossovers) | 100 % | 31.0 % | — |
| separation ≥ 0.10 % | 77 % | 31.8 % | 28.6 % |
| separation ≥ 0.30 % | 45 % | 33.1 % | 29.4 % |
| volume > 1.1 × 20-bar average | 36 % | 32.8 % | 30.1 % |
| higher high & higher low (10 bars) | 85 % | 31.5 % | 28.8 % |
All four held in the second half of the sample (from September 2020, +2.1 to +2.8 points each). The filters overlap: combining separation with volume kept 20 % of crossovers at 33.5 %, not more.
**What the measurement does not show — please read**
- The improvement is in *fewer stop-outs*, not in better returns afterwards. Measured without any exit, 20 and 60 bars after the crossover, the separation and volume conditions left the kept and removed groups within a point of each other; the higher-high/higher-low condition improved the 20-bar outcome (+3.4 points) but not the 60-bar one. This is a whipsaw filter, not a return predictor.
- The absolute hit rates (31 %) belong to one specific exit rule with a tight trailing threshold, typical for trend following. Your exit will give different absolute numbers.
- Tested on daily bars and on one pair of averages. The separation threshold is a percentage of price, so it needs adjusting for other timeframes and volatilities — the table shows the current separation to help with that.
- A confirmed state is a description of where the averages are. It is not a recommendation to do anything.
No buy or sell signals are generated, and none are implied.
**What it does**
Turns a moving-average crossover into a two-step state: *pending* the moment the fast MA crosses the slow one, *confirmed* once the two averages have separated by at least a minimum percentage (default 0.30 % of the slow MA). Optional extra conditions for confirmation: volume above its 20-bar average, and a higher high plus higher low over the last N bars (mirrored for bearish crosses). A confirmed state holds until the averages cross back; it is never downgraded.
**What you see**
- Both averages (defaults EMA 9 and DEMA 200; EMA, SMA and DEMA selectable for each).
- The area between them filled gray while pending, teal or red once confirmed.
- A small dot on the bar where confirmation happened.
- A table with the current separation, the threshold, active extra filters and the state.
- Alerts for each of the four transitions: bullish pending, bullish confirmed, bearish pending, bearish confirmed.
**Why I built it — the measurement behind it**
Crossovers of a fast and a slow average produce many entries that get stopped out within days. I measured which conditions at the crossover bar reduced that, on 47,013 daily bullish crossovers (EMA 9 over DEMA 200) across 1,758 US stocks, 2014–2026, including later-delisted names, each followed by the same trailing exit:
| Condition at the crossover bar | Share of crossovers kept | Share of trades ending positive | vs. the crossovers it removed |
|---|---:|---:|---:|
| none (all crossovers) | 100 % | 31.0 % | — |
| separation ≥ 0.10 % | 77 % | 31.8 % | 28.6 % |
| separation ≥ 0.30 % | 45 % | 33.1 % | 29.4 % |
| volume > 1.1 × 20-bar average | 36 % | 32.8 % | 30.1 % |
| higher high & higher low (10 bars) | 85 % | 31.5 % | 28.8 % |
All four held in the second half of the sample (from September 2020, +2.1 to +2.8 points each). The filters overlap: combining separation with volume kept 20 % of crossovers at 33.5 %, not more.
**What the measurement does not show — please read**
- The improvement is in *fewer stop-outs*, not in better returns afterwards. Measured without any exit, 20 and 60 bars after the crossover, the separation and volume conditions left the kept and removed groups within a point of each other; the higher-high/higher-low condition improved the 20-bar outcome (+3.4 points) but not the 60-bar one. This is a whipsaw filter, not a return predictor.
- The absolute hit rates (31 %) belong to one specific exit rule with a tight trailing threshold, typical for trend following. Your exit will give different absolute numbers.
- Tested on daily bars and on one pair of averages. The separation threshold is a percentage of price, so it needs adjusting for other timeframes and volatilities — the table shows the current separation to help with that.
- A confirmed state is a description of where the averages are. It is not a recommendation to do anything.
No buy or sell signals are generated, and none are implied.
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.