OPEN-SOURCE SCRIPT
Triangle Pattern Scanner (Symmetric / Ascending / Descending)

Triangle Pattern Scanner automatically detects and draws Symmetric,
Ascending, and Descending triangle formations on the chart in real time,
using pivot-based geometry validation to ensure only structurally sound
patterns are displayed. A multi-timeframe table shows at a glance which
triangle types are currently forming across five user-defined timeframes.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BACKGROUND — WHY TRIANGLE PATTERNS?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Triangle patterns are among the most widely recognized continuation and
reversal formations in technical analysis. They form when price makes a
series of pivot highs and pivot lows that converge toward a single apex
point, compressing volatility before a potential breakout in either
direction.
Identifying them manually requires drawing trendlines across multiple
pivots, verifying that price stays within the boundaries throughout the
formation, checking that the lines actually converge ahead, and
distinguishing between the three types based on the angle of each
boundary line. This indicator automates all of those steps.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
THREE PATTERN TYPES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SYMMETRIC TRIANGLE
Both the upper trendline (connecting pivot highs) and the lower trendline
(connecting pivot lows) slope toward each other — the upper falling, the
lower rising. Neither side is horizontal. The pattern is considered neutral
until the breakout direction is confirmed. A symmetry ratio check ensures
the two slopes are reasonably balanced rather than one side being nearly
flat while the other is very steep.
ASCENDING TRIANGLE
The upper trendline is nearly horizontal (price repeatedly tests the same
resistance level) while the lower trendline slopes upward (higher lows).
The pattern is considered bullish by structure, with the expectation of
an upward breakout through the flat resistance. The angle tolerance for
the horizontal line is configurable, giving you control over how strictly
"flat" the upper line must be.
DESCENDING TRIANGLE
The lower trendline is nearly horizontal (price repeatedly tests the same
support level) while the upper trendline slopes downward (lower highs).
The pattern is considered bearish by structure, with the expectation of
a downward breakout through the flat support. The same configurable angle
tolerance applies to the lower line.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
HOW IT WORKS — TECHNICAL DETAIL
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
PIVOT DETECTION
The indicator uses Pine Script's ta.pivothigh() and ta.pivotlow() to
collect recent swing highs and swing lows. The Pivot Length input controls
the symmetric left-right lookback used by both functions — a pivot high
requires the highest high compared to an equal number of bars on both
sides. Up to 8 recent pivots of each type are stored in memory for
pattern scanning.
GEOMETRY VALIDATION
For each combination of two pivot highs and two pivot lows, the indicator
calculates the slopes and intercepts of the upper and lower trendlines,
then verifies:
— Convergence: the two lines must actually meet ahead of the current
bar (apex in the future), not behind it
— Containment: every bar between the formation's start and end must
have its high below the upper trendline and its low above the lower
trendline — no violations allowed
— Slope direction: upper falling + lower rising for symmetric; upper
flat + lower rising for ascending; lower flat + upper falling for
descending
— Alternating pivots: the four pivot points (2 highs, 2 lows) must
alternate in time order (H, L, H, L or L, H, L, H) — this prevents
false formations where all highs come before all lows or vice versa
— Symmetry ratio (symmetric only): the ratio of the two slopes must
exceed a minimum threshold so neither side dominates
ANGLE NORMALIZATION
The angle calculation normalizes price change against the formation's
own height and bar count rather than using raw price differences. This
makes the angle tolerance behave consistently across different symbols,
price ranges, and timeframes — the same degree setting works on a $1
stock and a $50,000 asset alike.
DUPLICATE FILTERING
Once a valid formation is found, its starting bar position is recorded.
Any subsequent formation of the same type starting within a short
distance of an existing one is treated as a duplicate and skipped,
preventing the same structure from being drawn multiple times with
slightly different pivot combinations.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
MULTI-TIMEFRAME TABLE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
A table in the bottom-right corner runs the same pattern detection
logic across five user-defined timeframes simultaneously and displays
whether each triangle type is currently detected on each timeframe.
A ✅ marks a detected pattern; — means none found.
This gives you a fast structural overview without switching charts —
if the 1H and 4H both show an ascending triangle, that confluence may
carry more weight than a pattern on a single timeframe alone.
The MTF scan uses a sampled bar validation (checking approximately 12
evenly-spaced points within the formation rather than every bar) to
keep performance reasonable across multiple timeframes.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
HOW TO USE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. SET PIVOT LENGTH
Controls how significant a swing high or low must be. Lower values
(2–5) detect more frequent, shorter-term pivots. Higher values
(10–20) require more surrounding bars and detect only major structure.
2. SET LOOKBACK
Maximum bar span a formation can cover. Increase this to allow
larger, longer-term formations; decrease it to focus on recent
structure only.
3. ADJUST FLAT LINE ANGLE TOLERANCE
For ascending and descending triangles, this controls how strictly
the flat boundary must be horizontal. At 0° only perfectly
horizontal lines qualify; at 10–15° a moderate slope is allowed.
Increase if you want more patterns detected; decrease for stricter
horizontal requirement.
4. CONTROL MAX PATTERNS
Each pattern type has a separate maximum count. Set to 1 to show
only the most recent valid formation per type, or increase to see
multiple simultaneous formations.
5. SET LINE EXTENSION
How many bars beyond the formation the trendlines extend, giving
a visual projection of where the boundaries will be in the future.
6. READ THE MTF TABLE
Use the bottom-right table to check whether the same pattern type
is forming on higher timeframes, and prioritize formations that
appear on multiple timeframes.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WHERE TO USE IT
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Works on any asset class (crypto, forex, equities, indices, commodities)
and any timeframe. Particularly useful for:
— Breakout traders who want to be positioned before a triangle resolves
— Swing traders looking for compression zones where volatility is
contracting ahead of an expansion move
— Multi-timeframe analysis where higher-timeframe triangle formations
provide context for lower-timeframe entries
— Market structure analysis where triangle patterns help define the
current phase of price action (consolidation vs. expansion)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SETTINGS REFERENCE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Pivot Length — symmetric lookback for swing high/low detection
Lookback — maximum bar span a formation may cover
Max Triangles (each) — maximum formations shown per pattern type
Show/Hide (each type) — toggle each pattern type independently
Line Width — visual thickness of the trendlines
Line Extension (Bars) — how far beyond the formation lines extend
Show Labels — enable/disable the formation labels on chart
Flat Line Angle Tolerance — horizontal strictness for ascending/descending
MTF Timeframes (TF 1–5) — the five timeframes scanned in the MTF table
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ORIGINALITY
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Triangle pattern detection is a known concept, but this indicator's
specific implementation combines several elements not commonly found
together: full bar-by-bar containment validation (not just endpoint
checking), alternating pivot order enforcement, normalized angle
calculation that works consistently across different assets and
timeframes, and a simultaneous five-timeframe detection table — all
within a single indicator. The flat-line angle tolerance using
formation-height normalization is the author's own approach to making
ascending and descending triangle detection robust across varied market
conditions.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DISCLAIMER
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
This indicator is intended for educational and analytical purposes only.
Triangle patterns do not guarantee a breakout in any particular
direction. Always use proper risk management and combine multiple forms
of analysis before making trading decisions.
Ascending, and Descending triangle formations on the chart in real time,
using pivot-based geometry validation to ensure only structurally sound
patterns are displayed. A multi-timeframe table shows at a glance which
triangle types are currently forming across five user-defined timeframes.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BACKGROUND — WHY TRIANGLE PATTERNS?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Triangle patterns are among the most widely recognized continuation and
reversal formations in technical analysis. They form when price makes a
series of pivot highs and pivot lows that converge toward a single apex
point, compressing volatility before a potential breakout in either
direction.
Identifying them manually requires drawing trendlines across multiple
pivots, verifying that price stays within the boundaries throughout the
formation, checking that the lines actually converge ahead, and
distinguishing between the three types based on the angle of each
boundary line. This indicator automates all of those steps.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
THREE PATTERN TYPES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SYMMETRIC TRIANGLE
Both the upper trendline (connecting pivot highs) and the lower trendline
(connecting pivot lows) slope toward each other — the upper falling, the
lower rising. Neither side is horizontal. The pattern is considered neutral
until the breakout direction is confirmed. A symmetry ratio check ensures
the two slopes are reasonably balanced rather than one side being nearly
flat while the other is very steep.
ASCENDING TRIANGLE
The upper trendline is nearly horizontal (price repeatedly tests the same
resistance level) while the lower trendline slopes upward (higher lows).
The pattern is considered bullish by structure, with the expectation of
an upward breakout through the flat resistance. The angle tolerance for
the horizontal line is configurable, giving you control over how strictly
"flat" the upper line must be.
DESCENDING TRIANGLE
The lower trendline is nearly horizontal (price repeatedly tests the same
support level) while the upper trendline slopes downward (lower highs).
The pattern is considered bearish by structure, with the expectation of
a downward breakout through the flat support. The same configurable angle
tolerance applies to the lower line.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
HOW IT WORKS — TECHNICAL DETAIL
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
PIVOT DETECTION
The indicator uses Pine Script's ta.pivothigh() and ta.pivotlow() to
collect recent swing highs and swing lows. The Pivot Length input controls
the symmetric left-right lookback used by both functions — a pivot high
requires the highest high compared to an equal number of bars on both
sides. Up to 8 recent pivots of each type are stored in memory for
pattern scanning.
GEOMETRY VALIDATION
For each combination of two pivot highs and two pivot lows, the indicator
calculates the slopes and intercepts of the upper and lower trendlines,
then verifies:
— Convergence: the two lines must actually meet ahead of the current
bar (apex in the future), not behind it
— Containment: every bar between the formation's start and end must
have its high below the upper trendline and its low above the lower
trendline — no violations allowed
— Slope direction: upper falling + lower rising for symmetric; upper
flat + lower rising for ascending; lower flat + upper falling for
descending
— Alternating pivots: the four pivot points (2 highs, 2 lows) must
alternate in time order (H, L, H, L or L, H, L, H) — this prevents
false formations where all highs come before all lows or vice versa
— Symmetry ratio (symmetric only): the ratio of the two slopes must
exceed a minimum threshold so neither side dominates
ANGLE NORMALIZATION
The angle calculation normalizes price change against the formation's
own height and bar count rather than using raw price differences. This
makes the angle tolerance behave consistently across different symbols,
price ranges, and timeframes — the same degree setting works on a $1
stock and a $50,000 asset alike.
DUPLICATE FILTERING
Once a valid formation is found, its starting bar position is recorded.
Any subsequent formation of the same type starting within a short
distance of an existing one is treated as a duplicate and skipped,
preventing the same structure from being drawn multiple times with
slightly different pivot combinations.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
MULTI-TIMEFRAME TABLE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
A table in the bottom-right corner runs the same pattern detection
logic across five user-defined timeframes simultaneously and displays
whether each triangle type is currently detected on each timeframe.
A ✅ marks a detected pattern; — means none found.
This gives you a fast structural overview without switching charts —
if the 1H and 4H both show an ascending triangle, that confluence may
carry more weight than a pattern on a single timeframe alone.
The MTF scan uses a sampled bar validation (checking approximately 12
evenly-spaced points within the formation rather than every bar) to
keep performance reasonable across multiple timeframes.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
HOW TO USE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. SET PIVOT LENGTH
Controls how significant a swing high or low must be. Lower values
(2–5) detect more frequent, shorter-term pivots. Higher values
(10–20) require more surrounding bars and detect only major structure.
2. SET LOOKBACK
Maximum bar span a formation can cover. Increase this to allow
larger, longer-term formations; decrease it to focus on recent
structure only.
3. ADJUST FLAT LINE ANGLE TOLERANCE
For ascending and descending triangles, this controls how strictly
the flat boundary must be horizontal. At 0° only perfectly
horizontal lines qualify; at 10–15° a moderate slope is allowed.
Increase if you want more patterns detected; decrease for stricter
horizontal requirement.
4. CONTROL MAX PATTERNS
Each pattern type has a separate maximum count. Set to 1 to show
only the most recent valid formation per type, or increase to see
multiple simultaneous formations.
5. SET LINE EXTENSION
How many bars beyond the formation the trendlines extend, giving
a visual projection of where the boundaries will be in the future.
6. READ THE MTF TABLE
Use the bottom-right table to check whether the same pattern type
is forming on higher timeframes, and prioritize formations that
appear on multiple timeframes.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WHERE TO USE IT
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Works on any asset class (crypto, forex, equities, indices, commodities)
and any timeframe. Particularly useful for:
— Breakout traders who want to be positioned before a triangle resolves
— Swing traders looking for compression zones where volatility is
contracting ahead of an expansion move
— Multi-timeframe analysis where higher-timeframe triangle formations
provide context for lower-timeframe entries
— Market structure analysis where triangle patterns help define the
current phase of price action (consolidation vs. expansion)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SETTINGS REFERENCE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Pivot Length — symmetric lookback for swing high/low detection
Lookback — maximum bar span a formation may cover
Max Triangles (each) — maximum formations shown per pattern type
Show/Hide (each type) — toggle each pattern type independently
Line Width — visual thickness of the trendlines
Line Extension (Bars) — how far beyond the formation lines extend
Show Labels — enable/disable the formation labels on chart
Flat Line Angle Tolerance — horizontal strictness for ascending/descending
MTF Timeframes (TF 1–5) — the five timeframes scanned in the MTF table
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ORIGINALITY
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Triangle pattern detection is a known concept, but this indicator's
specific implementation combines several elements not commonly found
together: full bar-by-bar containment validation (not just endpoint
checking), alternating pivot order enforcement, normalized angle
calculation that works consistently across different assets and
timeframes, and a simultaneous five-timeframe detection table — all
within a single indicator. The flat-line angle tolerance using
formation-height normalization is the author's own approach to making
ascending and descending triangle detection robust across varied market
conditions.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DISCLAIMER
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
This indicator is intended for educational and analytical purposes only.
Triangle patterns do not guarantee a breakout in any particular
direction. Always use proper risk management and combine multiple forms
of analysis before making trading decisions.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.