OPEN-SOURCE SCRIPT
MirPapa:ICT:HTF: FVG OB Threeple

# MirPapa:ICT:HTF: FVG OB (Fair Value Gap Order Block)
**Version:** Pine Script® v6
**Author:** © goodia
**License:** MPL-2.0 (Mozilla Public License 2.0)
---
## Overview
“FVG OB” (Fair Value Gap Order Block) identifies higher-timeframe candle ranges where a gap (imbalance) exists between two non-consecutive candles, signaling potential institutional order blocks. This module draws bullish or bearish FVG OB boxes on your lower-timeframe chart, extends them until price interacts a specified number of times, and then finalizes (recolors) the box.
---
## Inputs
- **Enable FVG OB Boxes** (`bool`)
Toggle drawing of HTF FVG OB boxes on the chart.
- **Enable FVG OB Midlines** (`bool`)
Toggle drawing of a midpoint line inside each FVG OB box.
- **FVG OB Close Count** (`int` 1–10)
Number of HTF closes beyond the FVG range required to finalize (recolor) the box.
- **FVG OB Bull Color** (`color`)
Fill & border color for bullish FVG OB boxes.
- **FVG OB Bear Color** (`color`)
Fill & border color for bearish FVG OB boxes.
- **FVG OB Box Transparency** (`int` 1–100)
Opacity level for FVG OB box fills (higher = more transparent).
---
## How It Works
1. **HTF Data Retrieval**
- The script uses `request.security()` (via `GetHTFrevised()`) to fetch HTF OHLC and historical values:
- `_htfHigh3` (high three bars ago) and `_htfLow1` (low one bar ago) for bullish FVG OB.
- `_htfLow3` (low three bars ago) and `_htfHigh1` (high one bar ago) for bearish FVG OB.
- It also tracks the HTF `bar_index` on the lower timeframe to align drawing.
2. **FVG OB Detection**
- **Bullish FVG OB**: Occurs when the HTF low of the previous bar (`low[1]`) is strictly above the HTF high of three bars ago (`high[3]`), creating a gap.
- **Bearish FVG OB**: Occurs when the HTF high of the previous bar (`high[1]`) is strictly below the HTF low of three bars ago (`low[3]`), creating a gap.
3. **Box Creation**
- On each new HTF bar (`ta.change(time(HTF)) != 0`), if a bullish or bearish FVG OB condition is met, the script calls `CreateBoxData()` with:
- **Bullish**: `bottom = HTF low[1]`, `top = HTF high[3]`, `_isBull = true`.
- **Bearish**: `bottom = HTF low[3]`, `top = HTF high[1]`, `_isBull = false`.
- Midline toggled by input.
- A `BoxData` struct is created and stored in either the Bull or Bear array.
4. **Box Extension & Finalization**
- On **every LTF bar**, `ProcessBoxDatas(...)` iterates over all active FVG OB boxes:
1. **Extend Right Edge**: `box.set_right(bar_index)` ensures the box follows the latest bar.
2. **Record Volume Delta**: Tracks buy/sell volume inside the box.
3. **Touch Stage Update**: `modBoxUpdateStage()` increments `_stage` when price touches its “basePoint” (for FVG OB, the basePrice is one side of the gap).
4. **Finalize**: `setBoxFinalize()` checks if the configured number of closes beyond the FVG gap (`FVG OB Close Count`) has occurred. If so:
- `_isActive := false`
- Border and background colors are changed to the “Box Close Color” (input).
- Finalized boxes remain on screen semi-transparent, indicating that the FVG OB zone has been tested.
5. **Midline (Optional)**
- If “Enable FVG OB Midlines” is checked, `ProcessBoxDatas()` also extends a horizontal midpoint line inside the box with `line.set_x2(bar_index)`.
---
## Usage Instructions
1. **Installation**
- Copy the FVG OB section of the Pine Script into TradingView’s Pine Editor (ensure the library import is included).
- Click “Add to Chart.”
2. **Configure Inputs**
- Choose a Higher Time Frame via the dropdown (e.g., “4시간” maps to a 4H timeframe).
- Toggle “Enable FVG OB Boxes” and “Enable FVG OB Midlines.”
- Select colors for bullish and bearish boxes and set transparency.
- Adjust “FVG OB Close Count” to control how many closes beyond the gap finalize the box.
3. **Interpretation**
- **Active FVG OB Boxes** extend to the right until price closes beyond the gap range the specified number of times.
- When finalized, each box changes to the “Box Close Color,” signaling that institutional orders in that gap have likely been filled.
Enjoy precise visualization of higher-timeframe Fair Value Gap Order Blocks on your lower-timeframe chart!
**Version:** Pine Script® v6
**Author:** © goodia
**License:** MPL-2.0 (Mozilla Public License 2.0)
---
## Overview
“FVG OB” (Fair Value Gap Order Block) identifies higher-timeframe candle ranges where a gap (imbalance) exists between two non-consecutive candles, signaling potential institutional order blocks. This module draws bullish or bearish FVG OB boxes on your lower-timeframe chart, extends them until price interacts a specified number of times, and then finalizes (recolors) the box.
---
## Inputs
- **Enable FVG OB Boxes** (`bool`)
Toggle drawing of HTF FVG OB boxes on the chart.
- **Enable FVG OB Midlines** (`bool`)
Toggle drawing of a midpoint line inside each FVG OB box.
- **FVG OB Close Count** (`int` 1–10)
Number of HTF closes beyond the FVG range required to finalize (recolor) the box.
- **FVG OB Bull Color** (`color`)
Fill & border color for bullish FVG OB boxes.
- **FVG OB Bear Color** (`color`)
Fill & border color for bearish FVG OB boxes.
- **FVG OB Box Transparency** (`int` 1–100)
Opacity level for FVG OB box fills (higher = more transparent).
---
## How It Works
1. **HTF Data Retrieval**
- The script uses `request.security()` (via `GetHTFrevised()`) to fetch HTF OHLC and historical values:
- `_htfHigh3` (high three bars ago) and `_htfLow1` (low one bar ago) for bullish FVG OB.
- `_htfLow3` (low three bars ago) and `_htfHigh1` (high one bar ago) for bearish FVG OB.
- It also tracks the HTF `bar_index` on the lower timeframe to align drawing.
2. **FVG OB Detection**
- **Bullish FVG OB**: Occurs when the HTF low of the previous bar (`low[1]`) is strictly above the HTF high of three bars ago (`high[3]`), creating a gap.
- **Bearish FVG OB**: Occurs when the HTF high of the previous bar (`high[1]`) is strictly below the HTF low of three bars ago (`low[3]`), creating a gap.
3. **Box Creation**
- On each new HTF bar (`ta.change(time(HTF)) != 0`), if a bullish or bearish FVG OB condition is met, the script calls `CreateBoxData()` with:
- **Bullish**: `bottom = HTF low[1]`, `top = HTF high[3]`, `_isBull = true`.
- **Bearish**: `bottom = HTF low[3]`, `top = HTF high[1]`, `_isBull = false`.
- Midline toggled by input.
- A `BoxData` struct is created and stored in either the Bull or Bear array.
4. **Box Extension & Finalization**
- On **every LTF bar**, `ProcessBoxDatas(...)` iterates over all active FVG OB boxes:
1. **Extend Right Edge**: `box.set_right(bar_index)` ensures the box follows the latest bar.
2. **Record Volume Delta**: Tracks buy/sell volume inside the box.
3. **Touch Stage Update**: `modBoxUpdateStage()` increments `_stage` when price touches its “basePoint” (for FVG OB, the basePrice is one side of the gap).
4. **Finalize**: `setBoxFinalize()` checks if the configured number of closes beyond the FVG gap (`FVG OB Close Count`) has occurred. If so:
- `_isActive := false`
- Border and background colors are changed to the “Box Close Color” (input).
- Finalized boxes remain on screen semi-transparent, indicating that the FVG OB zone has been tested.
5. **Midline (Optional)**
- If “Enable FVG OB Midlines” is checked, `ProcessBoxDatas()` also extends a horizontal midpoint line inside the box with `line.set_x2(bar_index)`.
---
## Usage Instructions
1. **Installation**
- Copy the FVG OB section of the Pine Script into TradingView’s Pine Editor (ensure the library import is included).
- Click “Add to Chart.”
2. **Configure Inputs**
- Choose a Higher Time Frame via the dropdown (e.g., “4시간” maps to a 4H timeframe).
- Toggle “Enable FVG OB Boxes” and “Enable FVG OB Midlines.”
- Select colors for bullish and bearish boxes and set transparency.
- Adjust “FVG OB Close Count” to control how many closes beyond the gap finalize the box.
3. **Interpretation**
- **Active FVG OB Boxes** extend to the right until price closes beyond the gap range the specified number of times.
- When finalized, each box changes to the “Box Close Color,” signaling that institutional orders in that gap have likely been filled.
Enjoy precise visualization of higher-timeframe Fair Value Gap Order Blocks on your lower-timeframe chart!
Açık kaynak kodlu komut dosyası
Gerçek TradingView ruhuna uygun olarak, bu komut dosyasının oluşturucusu bunu açık kaynaklı hale getirmiştir, böylece yatırımcılar betiğin işlevselliğini inceleyip doğrulayabilir. Yazara saygı! Ücretsiz olarak kullanabilirsiniz, ancak kodu yeniden yayınlamanın Site Kurallarımıza tabi olduğunu unutmayın.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.
Açık kaynak kodlu komut dosyası
Gerçek TradingView ruhuna uygun olarak, bu komut dosyasının oluşturucusu bunu açık kaynaklı hale getirmiştir, böylece yatırımcılar betiğin işlevselliğini inceleyip doğrulayabilir. Yazara saygı! Ücretsiz olarak kullanabilirsiniz, ancak kodu yeniden yayınlamanın Site Kurallarımıza tabi olduğunu unutmayın.
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.