Identify the box before assigning a direction
StockCharts describes rectangles using at least two comparable reaction highs and two comparable reaction lows. Its discussion allows continuation and reversal outcomes. TradingView also describes the rectangle as directionally uncertain. This page concerns recognition and breakout classification; buying and selling repeatedly inside the range requires a separate entry and exit model.
| Feature | Rectangle requirement | Reason to reject or relabel |
|---|
| Upper and lower edges | Two horizontal price levels, R > S | Parallel sloping edges describe a channel; converging edges describe a different shape |
| Independent touches | At least two highs and two lows from distinct swings | Adjacent candles during one visit are not separate swings |
| Tolerance | Every selected high is near R and every selected low is near S | Increasing tolerance after a failed touch changes the detector |
| Containment | A stated closing-price rule holds until the candidate is available | A breakout already occurred before the last anchor could be known |
A four-touch rule with measurable tolerance
The following is an illustrative research specification, not a TradingView default or an optimized setting. Use standard candles on one fixed symbol, timeframe and session. A high must be strictly higher than the highs of three bars on each side; a low must be strictly lower than the lows of three bars on each side. Reject ties. Confirm the swing only after the third right-side bar closes.
- Use the latest four alternating swings when no candidate is active. Merge consecutive same-type pivots by retaining the more extreme one; for equal prices retain the earlier one. Freeze all anchors once the candidate passes.
- A later high of 109.50 passes the 0.50 touch band, while 109.49 fails. A zero-height box is invalid. Reject overlapping touch bands by requiring twice the tolerance to be less than H.
- Do not widen the box when subsequent prices leave it. Additional touches do not restart its clock. An unbroken candidate expires after bar 51; save that outcome and require a later final anchor before creating another candidate.
- These bar counts and price bands define a testable sample. Changing the timeframe or tolerance creates a different sample and requires a fresh comparison.
| Rule | Exact definition | Hypothetical example |
|---|
| Swing sequence | Four alternating confirmed swings, starting with either type; neighboring anchors 4 to 12 bars apart | High 110 at bar 10, low 100 at 16, high 109.8 at 22, low 100.2 at 28 |
| Frozen boundaries | R = first high; S = first low; height H = R - S must be positive | R = 110; S = 100; H = 10 |
| Touch band | Maximum absolute distance from the relevant boundary is 5% of H, inclusive | Tolerance = 0.50; later touch errors are 0.20 and 0.20 |
| Parallelism | Both saved boundaries have zero slope; do not join drifting highs or lows with sloped lines | R(t) = 110 and S(t) = 100 on every active bar |
| Formation closes | All closes from the first anchor through confirmation stay in [S - 0.20, R + 0.20] | Every close from bars 10 to 31 is assumed within [99.80, 110.20] |
| Availability and expiry | Start on the bar after the final pivot confirms; allow 20 completed bars for a break | Final pivot confirms at 31; eligible breakout bars are 32 through 51 |
Separate a wick, a closing break and a failed break
For this example, the first completed close strictly above R + 0.20 or strictly below S - 0.20 is the breakout event. The 0.20 buffer is 2% of H and is separate from touch tolerance. Start checking at bar 32, regardless of earlier intrabar excursions. Consume the candidate after its first event so repeated outside closes cannot create repeated breakout signals.
- Track early failure over the five bars after the event: an inside close or a close beyond the opposite buffered edge qualifies. Apply the mirror rule after a downside event. A later return is logged separately, not silently discarded.
- A successful retest can touch the broken edge intrabar and still close outside it. Specify a retest entry separately if needed; a breakout need not retest at all.
- A signal can gap past the threshold. The threshold is not an execution price. This event model has no trade orders, stops or profit calculation.
| Alternative path after bar 31 | Test | Recorded result |
|---|
| High reaches 110.70; close is 109.90 | Close does not exceed 110.20 | Wick excursion; no upside event |
| Close is exactly 110.20 | Equality fails the strict comparison | No event |
| Bar 32 closes at 110.40 | 110.40 > 110.20 | Upside breakout |
| Bar 32 closes at 99.60 | 99.60 < 99.80 | Downside breakout |
| Upside break at 32; close at 34 is 109.60 | Close returns to [100, 110] within the next five bars | Early failed upside break under this custom rule |
| Upside break at 32; close at 34 is 99.60 | Close crosses the opposite buffered edge, below 99.80 | Opposite-side failure, even though no close landed inside the box |
| No qualifying close through bar 51 | Twenty eligible bars have elapsed | Expired rectangle |
Continuation and reversal describe context, not geometry
Save the preceding-trend label before the breakout. For a reproducible example, compare the close immediately before the first anchor with the close 20 bars earlier: label a positive difference up, a negative difference down, and equality flat. This simple label is a chosen context proxy and does not measure trend quality. Missing history is unknown, not flat.
- The same 100-to-110 box has height 10. A conventional height projection gives an upside reference of R + H = 120 or a downside reference of S - H = 90. It does not use the eventual entry fill.
- Neither reference is a forecast, a guaranteed fill or a measured success rate. Review events that never reach it, including the early failures above. Record commission, slippage and exit rules before turning this study into a strategy.
- Keep volume as a separate recorded field or an explicit optional filter. StockCharts notes that rectangle formation volume is not uniform; volume contraction is not a universal recognition requirement.
| Prior context | Upside break | Downside break |
|---|
| Up | Continuation candidate | Reversal candidate |
| Down | Reversal candidate | Continuation candidate |
| Flat or unknown | Upward range resolution | Downward range resolution |
Distinguish TradingView drawings from automatic detection
The Rectangle drawing tool lets you mark a chart region and set coordinates. The automatic Chart Pattern Rectangle indicator uses 5/5 pivots, confirms breaks by closing price, and expresses permissible deviation as a percentage of rectangle height. Its In Progress mode can use unconfirmed final points. These are different mechanisms from the custom three-right-bar specification above.
- TradingView defines its Failed status using a move beyond the last opposite rectangle point before reaching the target. The five-bar return-inside label on this page is a separate research definition; do not compare those labels as if they were identical.
- A pivot drawn on bar 28 is only available on bar 31 under this example. Keep anchor time, confirmation time and breakout time in separate fields. Backdating a marker does not make the signal available earlier.
- Closing-bar evaluation avoids reacting to an unfinished candle that later moves back. It does not remove every repainting cause, including revised source data. Save events as they occur rather than counting only shapes still visible later.
Review the anchors, then draft the objective alerts
Pineify AI Chart Analysis accepts a chart screenshot and returns analysis that includes support and resistance. Keep the symbol, timeframe and price axis visible. Compare the output with your manually recorded four anchors, tolerance and invalidation rules. An image interpretation cannot prove exact touch counts or reconstruct missing candles.
- Record each proposed anchor as a bar time, price and confirmation time. Mark uncertain readings explicitly; verify prices against chart data before accepting the candidate.
- Give Pine Script AI Coding Agent the frozen levels, swing definition, tolerance, breakout buffer, expiry and failure window. Ask it to draft closing-bar alerts and distinct events for creation, breakout, failure and expiry.
- Inspect and syntax-check the resulting script, then replay the worked paths above on TradingView. The agent can draft code; agreement with your intended detector still needs review. This page provides a recognition specification, not a ready-to-run script or live scanner.