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.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.