OPEN-SOURCE SCRIPT
DTW Template Tracker [forexobroker]

DTW Template Tracker uses simplified Dynamic Time Warping with a Sakoe-Chiba band (width 2) to align recent z-scored closes against monotone bull (linear rise) and bear (linear fall) templates. DTW allows time stretching that cosine similarity does not. Lower distance = closer match; signal fires when one template's distance is materially lower than the other and below an absolute threshold.
🔶 ALGORITHM
1. Z-score the recent N closes using rolling mean and stdev.
2. Build templates: bull = linspace(-1, +1) over N points; bear = -bull.
3. DTW with Sakoe-Chiba band W = 2: cost matrix D[i,j] is filled only within |i - j| <= W. D[i,j] = |x - t[j]| + min(D[i-1,j-1], D[i-1,j], D[i,j-1]).
4. Bull and bear distances = D[N-1, N-1] under each template.
5. Match when one distance < other AND <= threshold.
🔶 SIGNAL LOGIC
- Buy: bull match edge AND not already long AND cooldown elapsed AND barstate.isconfirmed.
- Sell: bear match edge.
- Position-lock state machine.
🔶 INPUTS
- Template Length (default 15)
- DTW Distance Threshold (default 2.0)
- Cooldown Bars (default 4)
- Visual: dashboard, glow (flag with "match"), template overlay, buy / sell colors
🔶 ALERTS
DTT Buy, DTT Sell, DTT Any Signal, DTT Bull Match, DTT Bear Match, DTT Bull Tight, DTT Bear Tight, DTT Strong, DTT Webhook JSON.
🔶 LIMITATIONS
- DTW cost grows ~ N * W per bar; defaults (N = 15, W = 2) keep compute reasonable. Larger N exceeds Pine compute budget.
- Sakoe-Chiba band W = 2 limits stretch to +/- 2 bars; useful for 1:1 matching with minor jitter.
- Monotone templates only — V-shaped or W-shaped patterns require additional template definitions.
- Z-scoring removes scale; the indicator is invariant to absolute price level.
🔶 ALGORITHM
1. Z-score the recent N closes using rolling mean and stdev.
2. Build templates: bull = linspace(-1, +1) over N points; bear = -bull.
3. DTW with Sakoe-Chiba band W = 2: cost matrix D[i,j] is filled only within |i - j| <= W. D[i,j] = |x - t[j]| + min(D[i-1,j-1], D[i-1,j], D[i,j-1]).
4. Bull and bear distances = D[N-1, N-1] under each template.
5. Match when one distance < other AND <= threshold.
🔶 SIGNAL LOGIC
- Buy: bull match edge AND not already long AND cooldown elapsed AND barstate.isconfirmed.
- Sell: bear match edge.
- Position-lock state machine.
🔶 INPUTS
- Template Length (default 15)
- DTW Distance Threshold (default 2.0)
- Cooldown Bars (default 4)
- Visual: dashboard, glow (flag with "match"), template overlay, buy / sell colors
🔶 ALERTS
DTT Buy, DTT Sell, DTT Any Signal, DTT Bull Match, DTT Bear Match, DTT Bull Tight, DTT Bear Tight, DTT Strong, DTT Webhook JSON.
🔶 LIMITATIONS
- DTW cost grows ~ N * W per bar; defaults (N = 15, W = 2) keep compute reasonable. Larger N exceeds Pine compute budget.
- Sakoe-Chiba band W = 2 limits stretch to +/- 2 bars; useful for 1:1 matching with minor jitter.
- Monotone templates only — V-shaped or W-shaped patterns require additional template definitions.
- Z-scoring removes scale; the indicator is invariant to absolute price level.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
🔵Gain access to our powerful tools : kestutisbalciunas.com/tradingview/
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
🔵Gain access to our powerful tools : kestutisbalciunas.com/tradingview/
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
🔵Join our free telegram for updates : t.me/algotradingedge
All scripts & content provided by Forexobroker are for informational & educational purposes only.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.