PROTECTED SOURCE SCRIPT
D Money – EMA/TEMA Touch Strategy (Distance)

What it’s trying to capture
You want mean-reversion “tags” back to a moving average after price has stretched away and momentum flips:
Bearish setup (short): price has been above EMA(9) for a few bars, then MACD turns bearish, and price is far enough above the EMA (by an adaptive threshold). Exit when price tags the EMA.
Bullish setup (long): price has been below your chosen TEMA rail (actually an EMA of 50/100/200 you pick) for a few bars, then MACD turns bullish, and price is far enough below that TEMA. Exit when price tags that TEMA.
The moving averages it uses
EMA(9) — your fast “tag” for short take-profits.
“TEMA line” input = one of EMA(50) / EMA(100) / EMA(200). (Labelled “Chosen TEMA” in the plot; it’s an EMA rail you pick.)
When it will enter trades
It requires four things per side:
Short (EMA-Touch Short)
MACD bearish cross on the signal bar
If “Require NO MA touch on cross bar” = true, the bar’s low must be above EMA(9), so it didn’t touch EMA on the cross bar (fake-out guard).
Extension/Context: you’ve had at least barsAbove consecutive closes above EMA(9) (default 3), so it’s truly stretched.
Distance test: absolute % distance from price to EMA(9) must be ≥ minDistEMA_eff (an adaptive threshold; details below).
Bounce filter: there was no bullish bounce off the EMA in the last bounceLookback bars (excluding the current one).
If all pass and you’re inside the backtest window → strategy.entry short.
Long (TEMA-Touch Long)
MACD bullish cross on the signal bar
With the same fake-out guard: the bar’s high must be below the chosen TEMA if the guard is on.
Extension/Context: at least barsAbove consecutive closes below the chosen TEMA.
Distance test: absolute % distance from price to TEMA must be ≥ minDistTEMA_eff (adaptive).
Bounce filter: there was no bearish bounce off the TEMA in the last bounceLookback bars.
If all pass and you’re in the window → strategy.entry long.
MACD timing option:
If Pure MACD Timing = ON, it only checks for the cross.
If OFF (default), it also enforces “no touch on the cross bar” if that checkbox is true. That’s your “fake-out” filter.
The adaptive distance threshold (the “secret sauce”)
You can choose how “far enough away” is determined—per side:
Fixed %
Short uses Fixed: Min distance ABOVE EMA (%)
Long uses Fixed: Min distance BELOW TEMA (%)
Auto (ATR%) (default)
Short threshold = max(floorEMA, kAtrShort × ATR%)
Long threshold = max(floorTEMA, kAtrLong × ATR%)
This scales distance by recent volatility, with a floor.
Auto (AvgDist%)
Short threshold = max(floorEMA, kAvgShort × average(|Dist to EMA|) over avgLen)
Long threshold = max(floorTEMA, kAvgLong × average(|Dist to TEMA|) over avgLen)
This adapts to the instrument’s typical stretch away from the rails.
These become minDistEMA_eff and minDistTEMA_eff and are re-computed each bar.
Fake-out / bounce logic (the “don’t get tricked” part)
A touch means the bar’s high/low overlapped the MA ± a small buffer % (touchBufPct).
A bounce is a touch plus a close on the “wrong” side (e.g., touch EMA and close above it on shorts = bullish bounce).
The script blocks entries if a bounce happened within bounceLookback bars (excluding the current signal bar).
Exits & risk
Take profit: when price touches the target MA:
Short TP = touch EMA(9)
Long TP = touch chosen TEMA
Stop loss: either
ATR stop: entry ± (atrMultStop × ATR) (default ON), or
Percent stop: entry × (1±stopPct%)
Time stop: if timeExitBars > 0, close after that many bars if still open.
Quality-of-life features
Backtest window (btFrom, btTo) so you can limit evaluation.
Labels on signal bars that show:
MACD bucket (Small/Moderate/HUGE/Violent — based on % separation on the bar),
the current absolute distance to the target MA,
and the effective minimum the engine used (plus which engine mode).
Data Window fields so you can audit:
abs distance to EMA/TEMA,
the effective min distance used on each side,
ATR%,
average absolute distances (for the AvgDist mode).
Alerts fire when a short/long signal is confirmed.
Optional debug panel to see the exact booleans & thresholds the bar had.
Quick mental model
Are we properly stretched away from the rail (by an adaptive threshold) and held on that side for a few bars?
Did MACD flip the way we want without price already tagging the rail that bar?
Have we avoided recent bounces off that rail (no fake-out)?
→ If yes, enter and aim for a tag back to the rail, with ATR/% stop and optional time stop.
If you want, I can add a simple on-chart “rating” (0–100) similar to your Python scorer (distance beyond min, MACD bucket, extension streak) so you can visually rank signals in TradingView too.
You want mean-reversion “tags” back to a moving average after price has stretched away and momentum flips:
Bearish setup (short): price has been above EMA(9) for a few bars, then MACD turns bearish, and price is far enough above the EMA (by an adaptive threshold). Exit when price tags the EMA.
Bullish setup (long): price has been below your chosen TEMA rail (actually an EMA of 50/100/200 you pick) for a few bars, then MACD turns bullish, and price is far enough below that TEMA. Exit when price tags that TEMA.
The moving averages it uses
EMA(9) — your fast “tag” for short take-profits.
“TEMA line” input = one of EMA(50) / EMA(100) / EMA(200). (Labelled “Chosen TEMA” in the plot; it’s an EMA rail you pick.)
When it will enter trades
It requires four things per side:
Short (EMA-Touch Short)
MACD bearish cross on the signal bar
If “Require NO MA touch on cross bar” = true, the bar’s low must be above EMA(9), so it didn’t touch EMA on the cross bar (fake-out guard).
Extension/Context: you’ve had at least barsAbove consecutive closes above EMA(9) (default 3), so it’s truly stretched.
Distance test: absolute % distance from price to EMA(9) must be ≥ minDistEMA_eff (an adaptive threshold; details below).
Bounce filter: there was no bullish bounce off the EMA in the last bounceLookback bars (excluding the current one).
If all pass and you’re inside the backtest window → strategy.entry short.
Long (TEMA-Touch Long)
MACD bullish cross on the signal bar
With the same fake-out guard: the bar’s high must be below the chosen TEMA if the guard is on.
Extension/Context: at least barsAbove consecutive closes below the chosen TEMA.
Distance test: absolute % distance from price to TEMA must be ≥ minDistTEMA_eff (adaptive).
Bounce filter: there was no bearish bounce off the TEMA in the last bounceLookback bars.
If all pass and you’re in the window → strategy.entry long.
MACD timing option:
If Pure MACD Timing = ON, it only checks for the cross.
If OFF (default), it also enforces “no touch on the cross bar” if that checkbox is true. That’s your “fake-out” filter.
The adaptive distance threshold (the “secret sauce”)
You can choose how “far enough away” is determined—per side:
Fixed %
Short uses Fixed: Min distance ABOVE EMA (%)
Long uses Fixed: Min distance BELOW TEMA (%)
Auto (ATR%) (default)
Short threshold = max(floorEMA, kAtrShort × ATR%)
Long threshold = max(floorTEMA, kAtrLong × ATR%)
This scales distance by recent volatility, with a floor.
Auto (AvgDist%)
Short threshold = max(floorEMA, kAvgShort × average(|Dist to EMA|) over avgLen)
Long threshold = max(floorTEMA, kAvgLong × average(|Dist to TEMA|) over avgLen)
This adapts to the instrument’s typical stretch away from the rails.
These become minDistEMA_eff and minDistTEMA_eff and are re-computed each bar.
Fake-out / bounce logic (the “don’t get tricked” part)
A touch means the bar’s high/low overlapped the MA ± a small buffer % (touchBufPct).
A bounce is a touch plus a close on the “wrong” side (e.g., touch EMA and close above it on shorts = bullish bounce).
The script blocks entries if a bounce happened within bounceLookback bars (excluding the current signal bar).
Exits & risk
Take profit: when price touches the target MA:
Short TP = touch EMA(9)
Long TP = touch chosen TEMA
Stop loss: either
ATR stop: entry ± (atrMultStop × ATR) (default ON), or
Percent stop: entry × (1±stopPct%)
Time stop: if timeExitBars > 0, close after that many bars if still open.
Quality-of-life features
Backtest window (btFrom, btTo) so you can limit evaluation.
Labels on signal bars that show:
MACD bucket (Small/Moderate/HUGE/Violent — based on % separation on the bar),
the current absolute distance to the target MA,
and the effective minimum the engine used (plus which engine mode).
Data Window fields so you can audit:
abs distance to EMA/TEMA,
the effective min distance used on each side,
ATR%,
average absolute distances (for the AvgDist mode).
Alerts fire when a short/long signal is confirmed.
Optional debug panel to see the exact booleans & thresholds the bar had.
Quick mental model
Are we properly stretched away from the rail (by an adaptive threshold) and held on that side for a few bars?
Did MACD flip the way we want without price already tagging the rail that bar?
Have we avoided recent bounces off that rail (no fake-out)?
→ If yes, enter and aim for a tag back to the rail, with ATR/% stop and optional time stop.
If you want, I can add a simple on-chart “rating” (0–100) similar to your Python scorer (distance beyond min, MACD bucket, extension streak) so you can visually rank signals in TradingView too.
Script protetto
Questo script è pubblicato come codice protetto. Tuttavia, è possibile utilizzarlo liberamente e senza alcuna limitazione – per saperne di più clicca qui.
Declinazione di responsabilità
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.
Script protetto
Questo script è pubblicato come codice protetto. Tuttavia, è possibile utilizzarlo liberamente e senza alcuna limitazione – per saperne di più clicca qui.
Declinazione di responsabilità
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.