Flag Pattern Breakout [Dots3Red]█ FLAG PATTERN BREAKOUT
This script detects bull and bear flag patterns using two structural components: the staff (the impulsive pole) and the edge (the consolidation channel). Rather than relying on generic pivot-to-pivot zigzag lines, both components are built from regression-fitted geometry so the drawn shapes reflect the actual price structure rather than an approximation.
█ THE STAFF
The staff is the sharp, near-straight-line impulsive move that starts a flag. Two checks work together to identify it:
Structural continuity — instead of checking candle color (green vs red), the script checks whether each bar's wick still overlaps the bar before it. For a rising staff, a bar whose high fails to reach the previous bar's low counts as a break in the move. This catches genuine gaps in the advance while tolerating a normal red pullback candle that still overlaps the prior bar.
Straightness (R²) — a linear regression is fit through the closing prices of the candidate window, and its R² (coefficient of determination) is required to clear a minimum threshold (default 0.85). An R² of 1.0 would mean the closes sit exactly on a straight line; lower values reflect real curvature. Since real price data rarely produces a perfectly straight move, the threshold is adjustable rather than fixed at 1.0.
The search checks the longest possible window first and works down to shorter ones. A long window is only accepted if it clears both the wick-overlap test and the R² threshold — so the result favors the longest staff that still qualifies as straight, rather than the first short segment that happens to pass.
The staff line itself is drawn using the actual price at the two boundary bars (not the highest/lowest price found anywhere inside the scanning window), so it sits flush against the real candle wicks at both ends.
█ THE EDGE
The edge is the flag itself — a channel that runs opposite to the staff's direction. It is built as follows:
Slope — a running linear regression is fit through the highs (for a bull flag's falling edge) or the lows (for a bear flag's rising edge) of every bar since the staff ended. This produces one slope value that updates each bar as more data arrives.
Direction and steepness constraints — the edge's slope must run opposite to the staff (negative for a bull flag, positive for a bear flag) and must be shallower than the staff's own slope by a configurable ratio (default 60%). A flag that slopes as steeply as its pole is not behaving like a consolidation.
Width cap — the vertical distance between the tracked high and low extremes of the edge cannot exceed a percentage of the staff's height (default 60%). This is measured in absolute price distance, not bar count, so a slow-forming edge and a fast-forming edge are held to the same physical size constraint.
Breakout confirmation — the pattern is not finalized as soon as it meets minimum criteria. It keeps extending, bar by bar, for as long as price stays inside the channel. Confirmation only happens when price closes beyond the channel boundary by a configurable ATR buffer, in the direction that continues the original staff move (upward for a bull flag, downward for a bear flag). This means the edge is drawn at its full, longest actual duration rather than being cut short at an arbitrary minimum.
Envelope construction — once a pattern confirms, the two boundary lines are built by taking the regression-fitted line and shifting it by the maximum deviation observed on each side across every bar in the edge. The upper boundary is shifted up by the largest high-to-line distance seen; the lower boundary is shifted down by the largest line-to-low distance seen. Both lines share the identical slope, so they are parallel by construction, and together they contain the full price range of the consolidation rather than only touching two points.
█ VISUALS
The staff is drawn with a glow layer behind a sharp core line, color-coded green for bull and red for bear. The edge channel is filled with a soft translucent tint between its two boundary lines. A label at the breakout point shows the staff's height in ATR units, the edge's duration in bars, and the channel's width in ATR units.
█ SETTINGS
Staff
• Min Staff Height (×ATR) — minimum impulsive move size relative to ATR
• Min/Max Staff Duration (bars) — bounds on how many bars the staff can span
• Max Opposite-Direction Bars — structural wick-overlap tolerance
• Min Straightness (R²) — how closely the staff must fit a straight line
Edge
• Min Bars Before Breakout Eligible — minimum edge duration before a breakout can confirm
• Max Edge Duration (bars) — safety cap; abandons the pattern if no breakout occurs in time
• Max Channel Width (% of Staff Height) — absolute-distance cap on the edge's vertical size
• Max Edge Slope (× staff slope) — how much shallower the edge must be than the staff
• Min Edge Slope (×ATR per bar) — minimum slope magnitude so the edge counts as genuinely sloped
• Breakout Buffer (×ATR) — margin required beyond the boundary to confirm a breakout
█EXAMPLE ( DAILY APPLE STOCK )
█ NOTES
Because confirmation only happens at breakout, the pattern appears on the chart once the move has already resumed — this script identifies completed flag-and-breakout structures for review and study, not an early-warning signal before the breakout occurs. Works on any timeframe; behavior depends on how the ATR-based thresholds interact with the instrument's typical volatility.
However (!), with the ATR Breakout setting, it is possible to set a lower value, and by doing so we might "anticipate" Flag Development and consequent Breakout.
█ DISCLAIMER
This is a pattern visualization tool. It does not generate trade signals and does not constitute financial advice. Historical pattern detection does not guarantee that similar structures will behave the same way in the future.
指標

