PROTECTED SOURCE SCRIPT
FVG + Manip (optimized)

This indicator detects **Fair Value Gaps (FVGs)** using a **3-candle confirmation rule**, draws each FVG as a **boxed zone** on the chart (optionally with a **50% midpoint dashed line**), then monitors price action to:
1. flag a **“reaction”** when price touches the zone **and the candle body closes completely outside the zone**, and
2. **delete** the zone once it has been **fully filled** (either by wick or by body, depending on user settings).
Additionally, it colors the candle **green or red** only when a **manipulative candle** occurs *and* a matching **FVG reaction** is detected (bullish or bearish).
---
## Inputs and User Settings
### 1) FVG fill (“close”) method
**`closeMethod`** can be:
* **BODY**: the FVG is considered filled only when the **candle body** fully fills it.
* **WICK**: the FVG is considered filled when the **wick** fully fills it.
This setting affects when zones are deleted.
### 2) Manipulative candle detection mode
**`manipMode`** can be:
* **BASE**
* **BASE + BREAK**
* **DISPLACEMENT**
If **DISPLACEMENT** is selected, it also uses:
* **`dispPct`** = minimum body size as a % of candle range (0–1).
Example: `0.5` means the candle body must be at least **50%** of its full range.
### 3) Visual styling
You can set:
* Bull and bear FVG fill/border colors
* Fill transparency
* Border thickness
* Whether to show the **50% midpoint line**
* Midline colors and thickness
---
## Candle Measurements (for displacement logic)
For each candle it computes:
* **bodyHigh** = max(open, close)
* **bodyLow** = min(open, close)
* **bodySize** = abs(close − open)
* **rangeSize** = max(high − low, minimum tick)
* **hasDisp** = true if `bodySize >= rangeSize * dispPct`
So in DISPLACEMENT mode, a candle qualifies only if its body is “big enough” relative to its range.
---
## Manipulative Candle Logic
The script defines “manipulative” candles separately for bullish and bearish directions.
### BASE mode
* **Bullish (c1Green):**
The candle makes an equal/lower low vs the previous candle (`low <= low[1]`) and closes bullish (`close > open`).
* **Bearish (c1Red):**
The candle makes an equal/higher high vs the previous candle (`high >= high[1]`) and closes bearish (`close < open`).
### BASE + BREAK mode
* **Bullish (c2Green):**
It makes a lower low (`low < low[1]`) and closes back above the previous low (`close > low[1]`), and is bullish (`close > open`).
* **Bearish (c2Red):**
It makes a higher high (`high > high[1]`) and closes back below the previous high (`close < high[1]`), and is bearish (`close < open`).
### DISPLACEMENT mode
Same as BASE + BREAK, but also requires **hasDisp**:
* **Bullish (c3Green):** `c2Green and hasDisp`
* **Bearish (c3Red):** `c2Red and hasDisp`
Finally:
* **manipGreen** is true if the selected mode’s bullish condition is true
* **manipRed** is true if the selected mode’s bearish condition is true
---
## FVG Detection (3-candle confirmed)
It defines an FVG using candles `0`, `1`, and `2` (current candle = 0):
### Bullish FVG confirmed
```pine
bullFvgConfirmed = low > high[2]
```
Meaning the **current candle’s low** is above the **high of two candles ago** → an “upward gap” across 3 candles.
### Bearish FVG confirmed
```pine
bearFvgConfirmed = high < low[2]
```
Meaning the **current candle’s high** is below the **low of two candles ago** → a “downward gap”.
---
## Zone Creation and Drawing
When an FVG is confirmed, the script creates:
* a **box** representing the zone
* an optional **dashed midpoint line** at 50%
### Bullish zone geometry
* **Top = current low**
* **Bottom = high[2]**
The box starts at the current bar and extends right by:
* **`extendBars = 500`**
### Bearish zone geometry
* **Top = low[2]**
* **Bottom = current high**
### Midline (50%)
Midpoint is:
```pine
mid = (zTop + zBot) / 2
```
A dashed line is drawn across the same 500-bar extension.
If `showMidline` is false, the line is made effectively invisible.
---
## Storage / Object Management (maxKeep)
The script stores:
* bull boxes + their midlines
* bear boxes + their midlines
It keeps at most:
* **`maxKeep = 120`** zones per direction
When exceeded, it deletes the oldest box and its line to stay within limits.
---
## Zone Monitoring: Reaction + Deletion
Every bar, it loops through all stored zones and checks:
### A) “Touch” condition (common)
```pine
touches = (high >= zBot) and (low <= zTop)
```
This means the candle range overlaps the zone at least partially.
---
### B) Reaction rules (strict: body must be outside)
The script’s comment says:
**Reaction requires body OUTSIDE zone (never inside).**
#### Bullish reaction
```pine
if touches and (bodyLow > zTop)
bullReactNow := true
```
So price touched the zone, but the **entire candle body is above the zone** (bodyLow is above the zone top).
This is a “tap + rejection upward” style reaction.
#### Bearish reaction
```pine
if touches and (bodyHigh < zBot)
bearReactNow := true
```
Touched the zone, but the **entire candle body is below the zone** (bodyHigh is below zone bottom).
This is a “tap + rejection downward” reaction.
---
### C) Deletion rules (zone “filled”)
#### Bullish FVG fill
* Wick fill:
```pine
filledW = (low <= zBot)
```
* Body fill:
```pine
filledB = (bodyLow <= zBot)
```
Delete if:
* `closeMethod == WICK` and `filledW`
* OR `closeMethod == BODY` and `filledB`
#### Bearish FVG fill
* Wick fill:
```pine
filledW = (high >= zTop)
```
* Body fill:
```pine
filledB = (bodyHigh >= zTop)
```
Delete if:
* `closeMethod == WICK` and `filledW`
* OR `closeMethod == BODY` and `filledB`
When deleting, it removes:
* the box
* its corresponding midpoint line
* the entries in the arrays
---
## Final Candle Coloring (no overlap)
At the end, it colors candles only if:
* there is a **manipulative candle**, and
* there is a **reaction** in the same direction, and
* the opposite reaction is not simultaneously active
### Bullish candle coloring
```pine
greenFinal = manipGreen and bullReactNow and not bearReactNow
```
→ candle becomes **lime**
### Bearish candle coloring
```pine
redFinal = manipRed and bearReactNow and not bullReactNow
```
→ candle becomes **red**
If neither condition is met, `barcolor(na)` leaves candles unchanged.
---
## In short (one-liner)
This script draws 3-candle FVG zones, keeps them extended forward, deletes them only when fully filled (by wick/body setting), and highlights candles only when a chosen “manipulative” candle pattern happens at the same time as a strict “touch + body rejection” reaction from a bull/bear FVG.
If you want, I can also write a clean “user manual” style description (what signals mean, how to use settings, typical setups for scalping vs swing) in English.
نص برمجي محمي
تم نشر هذا النص البرمجي كمصدر مغلق. ومع ذلك، يمكنك استخدامه بحرية ودون أي قيود - تعرف على المزيد هنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي محمي
تم نشر هذا النص البرمجي كمصدر مغلق. ومع ذلك، يمكنك استخدامه بحرية ودون أي قيود - تعرف على المزيد هنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.