OPEN-SOURCE SCRIPT

Bollinger Bands HTF Hardcoded (Len 20 / Dev 2) [CHE]

67
Bollinger Bands HTF Hardcoded (Len 20 / Dev 2) — Higher-timeframe BB emulation with bucket-based length scaling and on-chart diagnostics

Summary

This indicator emulates higher-timeframe Bollinger Bands directly on the current chart by scaling a fixed base length (20) via a timeframe-to-bucket multiplier map. It avoids cross-timeframe requests and instead applies the “HTF feel” by using a longer effective lookback on lower timeframes. Bands use the classic deviation of 2 and the original color scheme (Basis blue, Upper red, Lower green, blue fill). An on-chart table reports the resolved bucket, multiplier, and effective length.

Pine version: v6
Overlay: true
Primary outputs: Basis (SMA), Upper/Lower bands, background fill, optional info table

Motivation: Why this design?

Cross-timeframe Bollinger Bands typically rely on `request.security`, which can introduce complexity, mixed-bar alignment issues, and potential repaint paths depending on how users consume signals intrabar. This design offers a deterministic alternative: a single-series calculation on the chart timeframe, with a hardcoded “HTF emulation” achieved by scaling the BB length according to coarse higher-timeframe buckets. The result is a smoother, slower band structure on low timeframes without external timeframe calls.

What’s different vs. standard approaches?

Baseline: Standard Bollinger Bands with a fixed user length on the current timeframe, or true HTF bands via `request.security`.

Architecture differences:

Fixed base parameters: Length = 20, Deviation = 2.
Bucket mapping derived from the chart timeframe (or manually overridden).
No `request.security`; all computations occur on the current series.
Effective length is “20 × multiplier”, where multiplier approximates aggregation into the chosen bucket.
Diagnostics table for transparency (bucket, multiplier, resolved length, bandwidth).

Practical effect: On lower timeframes, the effective length becomes much larger, behaving like a higher-timeframe Bollinger structure (smoother basis and wider stability), while remaining purely local to the chart series.

How it works (technical)

The script first resolves a target bucket (“Auto” or a manual selection such as 60/240/1D/…/12M). It then computes a multiplier that approximates how many current bars fit into that bucket (e.g., 1m→60m uses mult≈60, 5m→60m uses mult≈12). The effective Bollinger length becomes:

`bb_len = 20 mult` (clamped to at least 1)

Using the effective length, it calculates:

`basis = ta.sma(src, bb_len)`
`dev = 2 ta.stdev(src, bb_len)`
`upper = basis + dev`
`lower = basis - dev`

A “bandwidth” diagnostic is also computed as `(upper-lower) / basis` (guarded against division by zero) and shown in the table as a percentage. A persistent table object is created/deleted based on the visibility toggle and updated only on the last bar for performance.

Parameter Guide

Source — Input series for the bands — Default: Close
Use close for classic behavior; smoother sources reduce responsiveness.

Bucket — HTF bucket selection — Default: Auto
Auto derives a bucket from the chart timeframe; manual selection forces the intended target bucket.

Offset — Plot offset — Default: 0
Shifts plots forward/back for visual alignment, displayed in the data window.

Table X / Table Y — Table anchor — Default: Right / Top
Places the diagnostics table in one of nine anchor points.

Table Size — Table text size — Default: Normal
Use small on dense charts, large for presentations.

Dark Mode — Table theme — Default: Enabled
Switches table palette for readability against chart background.

Show Table — Toggle diagnostics table — Default: Enabled
Disable for a cleaner chart.

Reading & Interpretation

Basis (blue): The moving average centerline of the bands (SMA of effective length).
Upper (red) / Lower (green): ±2 standard deviations around the basis using the same effective length.
Fill (blue tint): Visual band zone to quickly see compression/expansion.

Interpretation staples:

Price riding the upper band suggests strong bullish pressure; riding the lower band suggests strong bearish pressure.
Band expansion indicates rising volatility; contraction indicates volatility compression.
Mean reversion setups often key off the basis and re-entries from outside bands, while breakout/trend setups often key off sustained band rides.

Diagnostics table:

HTF Tag: Human-readable label showing the current timeframe → bucket mapping.
Bucket: The resolved target bucket (Auto result or manual selection).
Multiplier: The integer factor applied to the base length.
Len/Dev: Shows base length (20) and the effective length result plus deviation (2).
Bandwidth: Normalized width of the band (percent), useful for spotting squeezes.

Practical Workflows & Combinations

HTF context on LTF charts: Use this as “slow structure” bands on 1m–15m charts without requesting HTF data.
Squeeze detection: Watch bandwidth shrink to historically low levels, then look for break/hold outside bands.
Trend filtering: Favor long bias when price stays above the basis and repeatedly respects it; favor short bias when below.
Confluence: Combine with market structure (swing highs/lows), volume tools, or a trend filter (e.g., a longer MA) for confirmation.

Behavior, Constraints & Performance

Repaint/confirmation: No cross-timeframe requests. Values can still evolve intrabar and settle on close, as with any indicator computed on live bars.
History requirements: Very large effective lengths need sufficient historical bars; expect a warm-up period after loading or switching symbols/timeframes.
Known limits: Because the method approximates HTF behavior by scaling lookback, it is not identical to true HTF Bollinger Bands computed on aggregated candles. In particular, volatility and mean can differ slightly versus a real HTF series.

Sensible Defaults & Quick Tuning

Default workflow:

Bucket: Auto
Source: Close
Table: On (until you trust the mapping), then optionally off

If bands feel too slow on your timeframe: choose a smaller bucket (e.g., 60 instead of 240).
If bands feel too reactive/noisy: choose a larger bucket (e.g., 1D or 3D).
If chart looks cluttered: hide the table; keep only the bands and fill.

What this indicator is—and isn’t

This is a Bollinger Band visualization layer that emulates higher-timeframe “slowness” via deterministic length scaling. It is not a complete trading system and does not include entries, exits, sizing, or risk management. Use it as context alongside your execution rules and protective stops.

Disclaimer

The content provided, including all code and materials, is strictly for educational and informational purposes only. It is not intended as, and should not be interpreted as, financial advice, a recommendation to buy or sell any financial instrument, or an offer of any financial product or service. All strategies, tools, and examples discussed are provided for illustrative purposes to demonstrate coding techniques and the functionality of Pine Script within a trading context.

Any results from strategies or tools provided are hypothetical, and past performance is not indicative of future results. Trading and investing involve high risk, including the potential loss of principal, and may not be suitable for all individuals. Before making any trading decisions, please consult with a qualified financial professional to understand the risks involved.

By using this script, you acknowledge and agree that any trading decisions are made solely at your discretion and risk.

Do not use this indicator on Heikin-Ashi, Renko, Kagi, Point-and-Figure, or Range charts, as these chart types can produce unrealistic results for signal markers and alerts.

Best regards and happy trading

Chervolino.

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.