Multi-Timeframe Previous-Candle Return Alert Scanner
A client's five-times-copied v5 alert script rewritten as one Pine v6 function with tick-level state across six selectable timeframes.
Alerts when price returns through the previous candle's high or low, deduped until a new extreme prints; one varip scan function instead of copy-paste, non-repainting level fetch, 167 historical events checked bar by bar with zero violations.

- One varip function replaces five copy-pasted blocks
- Dedupe re-arms only on a new extreme
- 167 events verified bar by bar, 0 violations
- Single alert covers all six timeframe slots
The client traded a simple but fiddly idea: alert when price trades below the previous candle's low and then comes back up through it (and the mirror on the high), never repeat for the same cross, and re-arm only after the current candle prints a new extreme. Their draft did this with five pasted copies of the same block and a dedupe rule that re-fired on the next tick.
The rewrite holds all tick-level state in a single function using varip variables, so each of six timeframe slots (15m, 1H, D, W, M, 3M by default, each with its own enable, timeframe and colour) gets an independent copy with no duplication. Previous-candle levels come from a non-repainting request on the prior bar. The dedupe fix arms from the candle's running extreme and requires a new extreme beyond the last alert before re-arming. One TradingView alert on "any alert() call" covers all slots with a readable message naming ticker, timeframe, event, level, price and time, and a status table shows each slot's levels, armed state and counts.
It was loaded on TradingView Desktop over the DevTools Protocol and 167 historical events across four slots were checked against the spec one bar at a time, including the dedupe rule, with zero violations; the 15-minute slot was verified by temporarily switching the chart timeframe and restoring it.
How it works
- Each slot fetches the previous candle's high and low for its timeframe
- Tick state tracks whether price has traded beyond a level
- Return through the level fires an alert and arms the dedupe
- New candle extreme beyond the last alert re-arms the slot
- Status table and markers show state on the chart
Software and automation are infrastructure, not financial advice. No performance guarantees. Every live system needs staged testing, risk limits, and owner approval.