OPEN-SOURCE SCRIPT

D1 Support/Resistance Guard

486
D1 S/R GUARD — HIGHER-TIMEFRAME ZONES WITH AN R-BASED ENTRY FILTER


Most support/resistance indicators draw lines. The problem is that by the time you are focused on a 5-minute entry trigger, you are no longer looking at the daily chart — and lines on a busy intraday chart become wallpaper. You end up entering three ticks under a level that has rejected price four times in the last year, and the trade turns against you immediately.

This script does two things about that. It builds daily support and resistance zones ranked by how many times price actually reacted at them, and it converts the distance to the nearest opposing level into R — multiples of your own stop size — so the question stops being the vague "am I near a level?" and becomes the concrete "does this trade have room to pay?"


WHAT IT PLOTS

- Clustered higher-timeframe S/R zones, drawn as boxes, labelled with a touch count
- Prior day high, low and close
- Prior week high and low
- Daily 50, 100 and 200 simple moving averages
- A dashboard showing the nearest level above and below price, the distance to each in price and in R, and a clear LONG / SHORT verdict


HOW THE ZONES ARE BUILT

Step 1 — Pivot detection. The script scans a rolling window of closed higher-timeframe bars (250 by default, roughly a year of daily data) and marks every swing high and swing low. Pivot strength is adjustable: a pivot high at strength 3 must be the highest of its own bar plus the three bars either side of it.

Step 2 — Clustering. Raw pivots are noisy and rarely land on exactly the same price twice. Any pivots falling within a tolerance of each other — expressed as a fraction of higher-timeframe ATR, so it scales across instruments and volatility regimes — are merged into a single zone. Each merge increments that zone's touch count and widens its boundaries to span the pivots inside it. The result is a zone with a real reaction history rather than a line through one arbitrary wick.

Step 3 — Ranking. Zones are scored on touch count plus a recency bonus, so a level that was defended four times including recently outranks one that was defended four times two years ago. Only the highest-scoring zones within a configurable distance of current price are drawn, which keeps the chart readable.

Step 4 — Filtering. A minimum touch threshold (default 2) discards one-off swings entirely.


THE ENTRY GUARD

This is the part that does the work. On every bar the script identifies the near edge of the closest level above price and the closest level below price, drawing from both the clustered zones and the discrete key levels. It then evaluates three blocking conditions:

1. Price is currently inside a zone.
2. The nearest opposing level is inside the danger band — a hard proximity threshold set as a fraction of higher-timeframe ATR.
3. There is less than a minimum number of R between price and that level.

The third condition is the useful one. You supply your stop size, either as a multiple of intraday ATR or as a fixed number of points, and the script divides the distance to the nearest level by it. If your stop is 1R and the nearest resistance is 0.8R above, that long cannot pay even if your read on direction is correct — the level will cap you before your target. The dashboard reports this as a number rather than a feeling.

When a direction is blocked, the dashboard cell turns red and reads STAND DOWN. The background can optionally tint. Alerts fire on the transition into and out of a blocked state, so you can be warned before you are staring at a setup rather than after.

Important: a blocked reading is not a signal to trade the other way. It means this particular entry, at this particular price, does not have the runway to justify the risk. Waiting for a better price or a break of the level are both valid responses.


DAILY MOVING AVERAGES

The 50, 100 and 200 daily SMAs are included as levels and feed the guard on equal footing with everything else. They are pulled as last-closed daily values, so they hold flat through the session instead of drifting as the current day's close moves — which is the correct behaviour for a level you intend to trade against.

They behave differently from pivot zones in one respect worth understanding. Pivot zones carry a touch history; the moving averages do not, so they are judged purely on distance. A rising 50 SMA that price is grinding above during a trend day will therefore block longs repeatedly. If that is too restrictive for how you trade, either narrow the danger band or switch the 50 off and keep the 100 and 200, which tend to be the more consequential levels.


HOW TO USE IT

Add it to your execution timeframe — it is designed for intraday charts, 1m through 15m. Set the stop sizing input to match how you actually size positions; every R figure the script produces depends on it. Then read the dashboard rather than the boxes. The boxes are context; the LONG and SHORT row is the decision.

The minimum-room default of 2R is a starting point, not a rule. Pull your own trade history, measure the distance from each entry to the nearest daily level, and find where your win rate falls off. That number is your threshold, and it will not be the same as anyone else's.


REPAINTING

All higher-timeframe data is requested using the expression[1] with lookahead_on construction, meaning every value comes from a closed higher-timeframe bar. Zones rebuild once per closed bar, not tick by tick. What you see on a historical bar is what you would have seen live. The drawn boxes are refreshed on the last bar for display purposes only; the underlying calculations and alert conditions evaluate on every bar from persistent data.


LIMITATIONS AND NOTES

- Requires sufficient history. The 200 SMA needs 200 daily bars, and the zone engine needs at least a few dozen. Newly listed symbols will show partial output.
- Zones are derived from price structure alone. Volume profile, options positioning and session boundaries are not considered.
- High touch counts cut both ways. A level defended four times is a high-probability reaction point right up until it breaks, and when it breaks the move through it is often larger for the same reason. A decisive close through a heavily-touched zone is information, not an indicator failure.
- The recency bonus is deliberately small relative to touch count. If you trade a fast-rotating instrument you may want structure weighted more toward recent action; adjust the lookback window down rather than the scoring.


All settings are documented with tooltips in the settings panel. Source is open — read it, modify it, tell me what you improve.

This script is a decision-support tool, not a trading system. It produces no buy or sell signals and makes no claim about future performance. Nothing here is financial advice. Test any configuration on your own instrument and timeframe before risking capital.

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.