OPEN-SOURCE SCRIPT
Cup and Handle Pattern [DJYYLAB]

OVERVIEW
Finds cup and handle patterns with a fixed set of rules and keeps track of what happens after each one forms: a breakout, a quick drop back below the pivot, or invalidation. All thresholds are listed below, so the result can be checked by hand.
Everything is calculated on closed bars. A pattern shows up on the bar where it was first confirmed and is not redrawn later.
HOW IT WORKS
1. Left rim
- A swing high (not exceeded within 2 bars on either side) above the previous 20 highs.
- It is only used once a later close drops to 92% of that high or lower, i.e. price has pulled back at least 8%.
2. Cup
- 30 to 220 bars long, 8% to 50% deep (measured from the left rim).
- The lowest low has to be a confirmed swing low, somewhere between 30% and 85% of the cup length.
- No high inside the cup more than 1% above the left rim.
- Round bottom, not a V: at least 15% of the closes sit in the lower part of the cup (low + 35% of the depth). On the way back up, no single bar gains more than 35% of the depth and no gap up is larger than 15% of the depth.
3. Right rim
- The first bar after the low whose high gets back within 3% of the left rim (or within -6% / +3% if that never happens).
4. Handle (3 to 8 bars after the right rim)
- Handle high between 95% and 103% of the right rim.
- At most 10% deep, and no deeper than half the cup.
- The handle low stays in the upper half of the cup.
- Closes don't rise by more than 1% per bar.
- Average bar range no more than 1.25x the cup's.
- At least 2 bars with highs within 4% of the handle high.
Shorter handles are checked first; the pattern forms on the first bar where all of the above is true.
5. After it forms (pivot = handle high)
- Breakout: close above the pivot.
- Invalidated: close below 90% of the right rim, or no breakout within 30 bars.
- Back below in 5 bars: after a breakout, a close at or below the pivot within 5 bars.
- If price closes below the handle low after a breakout, the pattern turns grey and the date is noted.
ON THE CHART
- Cup arc, handle line and a dashed pivot line.
- Blue = waiting, green = broke out, orange = back below in 5 bars, grey = invalidated.
- Hover the "Cup & Handle" label to see the formation bar, cup dates, length, depth, pivot, result and the move 5, 10 and 20 bars after the breakout.
- The table in the top right lists the latest patterns on the chart with their result and the move 5, 10 and 20 bars after the breakout (from the breakout close). These are single past cases on this symbol, not a win rate.
- If cups overlap, only the longest is drawn. The table shows how many were left out ("+N overlapped").
- Alerts: add an alert with "Any alert() function call" to get notified when a pattern forms and when it breaks out.
SETTINGS AND TIMEFRAMES
- Works on any timeframe. Lengths are in bars, so a weekly chart finds bases of about 7 months to 4 years, and an hourly chart finds bases of a few days to a few weeks.
- Cup length, cup depth, handle length, handle depth and the waiting period can all be changed.
- On stocks, keep dividend adjustment (ADJ) on so ex-dividend gaps don't look like price moves.
LIMITATIONS
- The thresholds were set on daily bars of Chinese A-shares and haven't been tested on other markets or on weekly/intraday data. On intraday charts the percentage rules (like the 8% minimum depth) can be too strict, so you may see few or no patterns.
- The table and tooltips only describe the history loaded on the chart. They are past cases, not a forecast.
- TradingView limits drawings, so at most 40 patterns are drawn at a time.
- Charts with little history will show fewer patterns.
ORIGINALITY
Written from scratch, no code taken from other scripts. It doesn't fit a curve to price: the left rim and the cup low have to be confirmed before they are used, V-shaped bottoms are filtered out, the handle is measured against its own cup, and every pattern is followed after it forms.
日本語
概要
カップ・ウィズ・ハンドル(カップ&ハンドル)を固定ルールで検出し、形成後にどうなったか(ブレイク、すぐにピボット下へ戻った、失効)まで追いかけるインジケーターです。しきい値はすべて下に書いてあるので、結果を自分で確かめられます。
計算はすべて確定足で行います。パターンは確定した足に表示され、後から描き直されることはありません。
判定ルール
1. 左の縁
- 前後2本で上回られていない高値(スイングハイ)で、直前20本の高値より高いこと。
- その後の終値がその高値の92%以下まで下がってから使います(少なくとも8%の押し)。
2. カップ
- 長さ30〜220本、深さ8%〜50%(左の縁から測定)。
- 最安値は確定したスイングローで、カップの長さの30%〜85%の位置にあること。
- カップ内の高値が左の縁を1%より大きく上回らないこと。
- V字ではなく丸い底:終値の15%以上がカップの下の方(安値+深さの35%以内)にあること。戻りの途中で、1本で深さの35%を超える上昇や、深さの15%を超える上窓がないこと。
3. 右の縁
- 底のあと、高値が初めて左の縁の3%以内に戻った足(なければ−6%〜+3%以内)。
4. ハンドル(右の縁のあとの3〜8本)
- ハンドルの高値が右の縁の95%〜103%。
- 深さ10%以内、かつカップの深さの半分以内。
- ハンドルの安値がカップの上半分にあること。
- 終値の上昇が1本あたり1%以内。
- 平均値幅がカップの1.25倍以内。
- ハンドルの高値から4%以内の高値が2本以上あること。
短いハンドルから順に調べ、すべて満たした最初の足でパターンが形成されます。
5. 形成後(ピボット=ハンドルの高値)
- ブレイク:終値がピボットを上回る。
- 失効:終値が右の縁の90%を下回る、または30本以内にブレイクしない。
- 5本以内に戻る:ブレイク後5本以内に終値がピボット以下になる。
- ブレイク後に終値がハンドルの安値を下回ると、グレーになり日付が記録されます。
チャート表示
- カップの弧、ハンドルの線、ピボットの点線。
- 青=ブレイク待ち、緑=ブレイク、オレンジ=5本以内に戻る、グレー=失効。
- 「Cup & Handle」ラベルにカーソルを合わせると、形成足、カップの期間・長さ・深さ、ピボット、結果、ブレイク後5・10・20本の値動きが表示されます。
- 右上の表は、チャート上の最近のパターンと結果、ブレイク後5・10・20本の値動き(ブレイク足の終値から)です。この銘柄の過去の個別事例で、勝率ではありません。
- カップが重なる場合は一番長いものだけを描き、描かなかった数を表に「+N overlapped」と表示します。
- アラート:「Any alert() function call」でアラートを作ると、形成時とブレイク時に通知されます。
設定と時間足
- どの時間足でも使えます。長さは本数なので、週足ならおよそ7か月〜4年、1時間足なら数日〜数週間のベースが対象になります。
- カップの長さ・深さ、ハンドルの長さ・深さ、ブレイク待ちの期間は変更できます。
- 株式では配当調整(ADJ)をオンにしてください。権利落ちの窓が値動きに見えるのを防げます。
注意点
- しきい値は中国A株の日足で決めたもので、他の市場や週足・日中足では検証していません。日中足では割合のルール(最小深さ8%など)が厳しく、パターンがほとんど出ないことがあります。
- 表とツールチップは読み込まれた過去データの事例だけで、予測ではありません。
- TradingViewの描画上限のため、同時に描けるのは最大40個です。
- 履歴が短いチャートではパターンが少なくなります。
独自性
他のスクリプトのコードは使わず、一から書いています。価格に曲線を当てはめるのではなく、左の縁とカップの底が確定してから使い、V字の底を除き、ハンドルを自分のカップと比べて判定し、形成後の経過まで追いかけます。
Finds cup and handle patterns with a fixed set of rules and keeps track of what happens after each one forms: a breakout, a quick drop back below the pivot, or invalidation. All thresholds are listed below, so the result can be checked by hand.
Everything is calculated on closed bars. A pattern shows up on the bar where it was first confirmed and is not redrawn later.
HOW IT WORKS
1. Left rim
- A swing high (not exceeded within 2 bars on either side) above the previous 20 highs.
- It is only used once a later close drops to 92% of that high or lower, i.e. price has pulled back at least 8%.
2. Cup
- 30 to 220 bars long, 8% to 50% deep (measured from the left rim).
- The lowest low has to be a confirmed swing low, somewhere between 30% and 85% of the cup length.
- No high inside the cup more than 1% above the left rim.
- Round bottom, not a V: at least 15% of the closes sit in the lower part of the cup (low + 35% of the depth). On the way back up, no single bar gains more than 35% of the depth and no gap up is larger than 15% of the depth.
3. Right rim
- The first bar after the low whose high gets back within 3% of the left rim (or within -6% / +3% if that never happens).
4. Handle (3 to 8 bars after the right rim)
- Handle high between 95% and 103% of the right rim.
- At most 10% deep, and no deeper than half the cup.
- The handle low stays in the upper half of the cup.
- Closes don't rise by more than 1% per bar.
- Average bar range no more than 1.25x the cup's.
- At least 2 bars with highs within 4% of the handle high.
Shorter handles are checked first; the pattern forms on the first bar where all of the above is true.
5. After it forms (pivot = handle high)
- Breakout: close above the pivot.
- Invalidated: close below 90% of the right rim, or no breakout within 30 bars.
- Back below in 5 bars: after a breakout, a close at or below the pivot within 5 bars.
- If price closes below the handle low after a breakout, the pattern turns grey and the date is noted.
ON THE CHART
- Cup arc, handle line and a dashed pivot line.
- Blue = waiting, green = broke out, orange = back below in 5 bars, grey = invalidated.
- Hover the "Cup & Handle" label to see the formation bar, cup dates, length, depth, pivot, result and the move 5, 10 and 20 bars after the breakout.
- The table in the top right lists the latest patterns on the chart with their result and the move 5, 10 and 20 bars after the breakout (from the breakout close). These are single past cases on this symbol, not a win rate.
- If cups overlap, only the longest is drawn. The table shows how many were left out ("+N overlapped").
- Alerts: add an alert with "Any alert() function call" to get notified when a pattern forms and when it breaks out.
SETTINGS AND TIMEFRAMES
- Works on any timeframe. Lengths are in bars, so a weekly chart finds bases of about 7 months to 4 years, and an hourly chart finds bases of a few days to a few weeks.
- Cup length, cup depth, handle length, handle depth and the waiting period can all be changed.
- On stocks, keep dividend adjustment (ADJ) on so ex-dividend gaps don't look like price moves.
LIMITATIONS
- The thresholds were set on daily bars of Chinese A-shares and haven't been tested on other markets or on weekly/intraday data. On intraday charts the percentage rules (like the 8% minimum depth) can be too strict, so you may see few or no patterns.
- The table and tooltips only describe the history loaded on the chart. They are past cases, not a forecast.
- TradingView limits drawings, so at most 40 patterns are drawn at a time.
- Charts with little history will show fewer patterns.
ORIGINALITY
Written from scratch, no code taken from other scripts. It doesn't fit a curve to price: the left rim and the cup low have to be confirmed before they are used, V-shaped bottoms are filtered out, the handle is measured against its own cup, and every pattern is followed after it forms.
日本語
概要
カップ・ウィズ・ハンドル(カップ&ハンドル)を固定ルールで検出し、形成後にどうなったか(ブレイク、すぐにピボット下へ戻った、失効)まで追いかけるインジケーターです。しきい値はすべて下に書いてあるので、結果を自分で確かめられます。
計算はすべて確定足で行います。パターンは確定した足に表示され、後から描き直されることはありません。
判定ルール
1. 左の縁
- 前後2本で上回られていない高値(スイングハイ)で、直前20本の高値より高いこと。
- その後の終値がその高値の92%以下まで下がってから使います(少なくとも8%の押し)。
2. カップ
- 長さ30〜220本、深さ8%〜50%(左の縁から測定)。
- 最安値は確定したスイングローで、カップの長さの30%〜85%の位置にあること。
- カップ内の高値が左の縁を1%より大きく上回らないこと。
- V字ではなく丸い底:終値の15%以上がカップの下の方(安値+深さの35%以内)にあること。戻りの途中で、1本で深さの35%を超える上昇や、深さの15%を超える上窓がないこと。
3. 右の縁
- 底のあと、高値が初めて左の縁の3%以内に戻った足(なければ−6%〜+3%以内)。
4. ハンドル(右の縁のあとの3〜8本)
- ハンドルの高値が右の縁の95%〜103%。
- 深さ10%以内、かつカップの深さの半分以内。
- ハンドルの安値がカップの上半分にあること。
- 終値の上昇が1本あたり1%以内。
- 平均値幅がカップの1.25倍以内。
- ハンドルの高値から4%以内の高値が2本以上あること。
短いハンドルから順に調べ、すべて満たした最初の足でパターンが形成されます。
5. 形成後(ピボット=ハンドルの高値)
- ブレイク:終値がピボットを上回る。
- 失効:終値が右の縁の90%を下回る、または30本以内にブレイクしない。
- 5本以内に戻る:ブレイク後5本以内に終値がピボット以下になる。
- ブレイク後に終値がハンドルの安値を下回ると、グレーになり日付が記録されます。
チャート表示
- カップの弧、ハンドルの線、ピボットの点線。
- 青=ブレイク待ち、緑=ブレイク、オレンジ=5本以内に戻る、グレー=失効。
- 「Cup & Handle」ラベルにカーソルを合わせると、形成足、カップの期間・長さ・深さ、ピボット、結果、ブレイク後5・10・20本の値動きが表示されます。
- 右上の表は、チャート上の最近のパターンと結果、ブレイク後5・10・20本の値動き(ブレイク足の終値から)です。この銘柄の過去の個別事例で、勝率ではありません。
- カップが重なる場合は一番長いものだけを描き、描かなかった数を表に「+N overlapped」と表示します。
- アラート:「Any alert() function call」でアラートを作ると、形成時とブレイク時に通知されます。
設定と時間足
- どの時間足でも使えます。長さは本数なので、週足ならおよそ7か月〜4年、1時間足なら数日〜数週間のベースが対象になります。
- カップの長さ・深さ、ハンドルの長さ・深さ、ブレイク待ちの期間は変更できます。
- 株式では配当調整(ADJ)をオンにしてください。権利落ちの窓が値動きに見えるのを防げます。
注意点
- しきい値は中国A株の日足で決めたもので、他の市場や週足・日中足では検証していません。日中足では割合のルール(最小深さ8%など)が厳しく、パターンがほとんど出ないことがあります。
- 表とツールチップは読み込まれた過去データの事例だけで、予測ではありません。
- TradingViewの描画上限のため、同時に描けるのは最大40個です。
- 履歴が短いチャートではパターンが少なくなります。
独自性
他のスクリプトのコードは使わず、一から書いています。価格に曲線を当てはめるのではなく、左の縁とカップの底が確定してから使い、V字の底を除き、ハンドルを自分のカップと比べて判定し、形成後の経過まで追いかけます。
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.