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.
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
🔵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.
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
🔵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.
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。