OPEN-SOURCE SCRIPT

FVG HTF

108
# FVG HTF — Higher‑Timeframe Fair Value Gaps

## Summary

- Plots higher‑timeframe Fair Value Gap (FVG) zones directly on your current chart.
- Tracks fill progress using four methods: Any Touch, Midpoint Reached, Wick Sweep, Body Beyond.
- Shows optional labels with timeframe source and live fill percentage; label text color is configurable.
- Designed for clean overlays and efficient rendering with limits on graphics and bars processed.

## What It Does

- Detects bullish and bearish FVGs from a chosen timeframe (or the chart timeframe) and renders:
- Zone Top/Bottom lines and a dotted midpoint line
- Semi‑transparent area fill between the edges
- Optional label at the midpoint with a tooltip showing zone prices
- Continuously updates zones forward and removes them when the selected fill condition is met.

## Inputs

- `Enable FVG` (`fvgSH2`): Toggle detection/plotting on/off.
- `Timeframe` (`fvgTF2`): Choose `Chart` or HTFs (`5 Minutes`, `15 Minutes`, `1 Hour`, `4 Hours`, `1 Day`, `1 Week`, `1 Month`).
- `Fill Method` (`fvgFill2`):
- Any Touch — wick or body touches any part of the zone
- Midpoint Reached — price reaches at least the 50% of the zone
- Wick Sweep — wick fully travels past the far edge and back inside (conceptually stricter than touch)
- Body Beyond — candle body closes beyond the opposite edge (strong confirmation)
- `Zones` colors (`fvgCb2`, `fvgCs2`): Bullish/Bearish zone colors.
- `Labels` (`fvgLB2`): Show/Hide on‑chart labels.
- `Label Color` (`fvgLBc2`): Color picker for label text (default: white).
- `Max Bars Back` (`maxBars2`): Limits processing to recent bars for performance.

## Timeframe Rules

- The helper `htfTF` prevents selecting a timeframe lower than the chart. If an invalid lower TF is chosen, it falls back to `timeframe.period`.
- Supports minute, daily, weekly, and monthly aggregations that are safe for intraday/daily/weekly charts.

## Detection Logic

- Uses rolling higher‑timeframe bars constructed on the fly and checks 3‑bar displacement patterns:
- Bullish FVG: current HTF low above the high two bars ago AND previous HTF close above that high, with no direct gap condition.
- Bearish FVG: current HTF high below the low two bars ago AND previous HTF close below that low, with no direct gap condition.
- On detection, the script creates an FVG object with:
- Top/Bottom lines (`lnTop`, `lnBtm`) and midpoint line (`lnAvg`)
- Midpoint label (`lbTxt`) showing source timeframe and updating fill percentage
- Semi‑transparent fill (`linefill`) for visual clarity

## Fill Tracking

- Fill threshold depends on selected method:
- Any Touch: opposite edge
- Midpoint Reached: zone’s midpoint
- Wick Sweep: stricter condition conceptually (implemented as an opposite‑edge threshold)
- Body Beyond: requires close beyond the opposite edge
- Each bar updates label x‑position and line endpoints forward; the label text shows the best fill ratio achieved.
- When the threshold is reached, the FVG (label, lines, fill) is removed from the chart.

## Best Practices

- Start with `Any Touch` to visualize broad repairs; switch to `Body Beyond` for conservative confirmations.
- Use `1 Hour` or `4 Hours` overlays on 5m–15m charts for context; `1 Day` on 1H charts; `1 Week` on daily charts.
- Keep labels on when monitoring fills intraday; hide labels for clean higher‑level context.
- Adjust `Max Bars Back` if performance is impacted by many zones.

## Repainting Notes

- HTF zones are computed on `timeframe.change(tf)` and therefore confirm on HTF bar closes.
- Label endpoints extend each bar; detection itself avoids lookahead bias. For strict confirmation, align entries with HTF closes.

## Limitations

- “Wick Sweep” is treated as a stricter touch to the far edge; it does not enforce a separate “return inside” bar state.
- Label text color applies uniformly to bull/bear labels. If you need separate colors per side, contact the author.

## Credits & Version

- Pine Script v6; © rithsilanew2020

## Quick Start

1. Enable FVG and choose your HTF (e.g., `1 Hour`).
2. Pick a Fill Method (start with `Any Touch`).
3. Select zone colors and label text color.
4. Set `Max Bars Back` as needed for performance.
5. Use labels/tooltip values (Top/Mid/Bottom) to plan entries and manage risk.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.