OPEN-SOURCE SCRIPT

ALISH WEEK LABELS

92
THE ALISH WEEK LABELS
Overview

This indicator programmatically delineates each trading week and encapsulates its realized price range in a live-updating, filled rectangle. A week is defined in America/Toronto time from Monday 00:00 to Friday 16:00. Weekly market open to market close, For every week, the script draws:

a vertical start line at the first bar of Monday 00:00,

a vertical end line at the first bar at/after Friday 16:00, and

a white, semi-transparent box whose top tracks the highest price and whose bottom tracks the lowest price observed between those two temporal boundaries.
The drawing is timeframe-agnostic (M1 → 1D): the box expands in real time while the week is open and freezes at the close boundary.

Time Reference and Session Boundaries

All scheduling decisions are computed with time functions called using the fixed timezone string "America/Toronto", ensuring correct behavior across DST transitions without relying on chart timezone. The start condition is met at the first bar where (dayofweek == Monday && hour == 0 && minute == 0); on higher timeframes where an exact 00:00 bar may not exist, a fallback checks for the first Monday bar using ta.change(dayofweek). The close condition is met on the first bar at or after Friday 16:00 (Toronto), which guarantees deterministic closure on intraday and higher timeframes.

State Model

The indicator maintains minimal persistent state using var globals:

week_open (bool): whether the current weekly session is active.

wk_hi / wk_lo (float): rolling extrema for the active week.

wk_box (box): the graphical rectangle spanning [left=start_bar, right=latest_bar] × [wk_lo, wk_hi].

wk_start_line and a transient wk_end_line (line): vertical delimiters at the week’s start and end.
Two dynamic arrays (boxes, vlines) store object handles to support bounded history and deterministic garbage collection.

Update Cycle (Per Bar)

On each bar the script executes the following pipeline:

Start Check: If no week is open and the start condition is satisfied, instantiate wk_box anchored at the current bar_index, prime wk_hi/wk_lo with the bar’s high/low, create the start line, and push both handles to their arrays.

Accrual (while week_open): Update wk_hi/wk_lo using math.max/min with current bar extremes. Propagate those values to the active wk_box via box.set_top/bottom and slide box.set_right to the current bar_index to keep the box flush with live price.

Close Check: If at/after Friday 16:00, finalize the week by freezing the right edge (box.set_right), drawing the end line, pushing its handle, and flipping week_open false.

Retention Pruning: Enforce a hard cap on historical elements by deleting the oldest objects when counts exceed configured limits.

Drawing Semantics

The range container is a filled white rectangle (bgcolor = color.new(color.white, 100 − opacity)), with a solid white border for clear contrast on dark or light themes. Start/end boundaries are full-height vertical white lines (y1=+1e10, y2=−1e10) to guarantee visibility across auto-scaled y-axes. This approach avoids reliance on price-dependent anchors for the lines and is robust to large volatility spikes.

Multi-Timeframe Behavior

Because session logic is driven by wall-clock time in the Toronto zone, the indicator remains consistent across chart resolutions. On coarse timeframes where an exact boundary bar might not exist, the script legally approximates by triggering on the first available bar within or immediately after the boundary (e.g., Friday 16:00 occurs between two 4-hour bars). The box therefore represents the true realized high/low of the bars present in that timeframe, which is the correct visual for that resolution.

Inputs and Defaults

Weeks to keep (show_weeks_back): integer, default 40. Controls retention of historical boxes/lines to avoid UI clutter and resource overhead.

Fill opacity (fill_opacity): integer 0–100, default 88. Controls how solid the white fill appears; border color is fixed pure white for crisp edges.
Time zone is intentionally fixed to "America/Toronto" to match the strategy definition and maintain consistent historical backtesting.

Performance and Limits

Objects are reused only within a week; upon closure, handles are stored and later purged when history limits are exceeded. The script sets generous but safe caps (max_boxes_count/max_lines_count) to accommodate 40 weeks while preserving Editor constraints. Per-bar work is O(1), and pruning loops are bounded by the configured history length, keeping runtime predictable on long histories.

Edge Cases and Guarantees

DST Transitions: Using a fixed IANA time zone ensures Friday 16:00 and Monday 00:00 boundaries shift correctly when DST changes in Toronto.

Weekend Gaps/Holidays: If the market lacks bars exactly at boundaries, the nearest subsequent bar triggers the start/close logic; range statistics still reflect observed prices.

Live vs Historical: During live sessions the box edge advances every bar; when replaying history or backtesting, the same rules apply deterministically.

Scope (Intentional Simplicity)

This tool is strictly a visual framing indicator. It does not compute labels, statistics, alerts, or extended S/R projections. Its single responsibility is to clearly present the week’s realized range in the Toronto session window so you can layer your own execution or analytics on top.

Exención de responsabilidad

La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.