Sure! I'd be happy to help you create a TradingView indicator that draws Fair Value Gaps (FVGs), extends them until they are filled, and deletes them if they are older than 10 days. Below is the Pine Script code for the indicator, along with an explanation of how it works.
Fair Value Gap (FVG) Detector Indicator
//@version=5
indicator("Fair Value Gap (FVG) Detector", overlay=true)
// Initialize arrays to store Bullish FVG data
var float[] bullish_fvg_top = array.new_float()
var float[] bullish_fvg_bottom = array.new_float()
var int[] bullish_fvg_time = array.new_int()
var box[] bullish_fvg_box = array.new_box()
var bool[] bullish_fvg_is_filled = array.new_bool()
// Initialize arrays to store Bearish FVG data
var float[] bearish_fvg_top = array.new_float()
var float[] bearish_fvg_bottom = array.new_float()
var int[] bearish_fvg_time = array.new_int()
var box[] bearish_fvg_box = array.new_box()
var bool[] bearish_fvg_is_filled = array.new_bool()
// Detect Bullish FVG
if low > high[2]
// New Bullish FVG detected
fvg_top = high[2]
fvg_bottom = low
fvg_time = time
// Create a box to represent the FVG
fvg_box = box.new(bar_index[2], fvg_top, bar_index, fvg_bottom, border_color=color.new(color.green, 0), bgcolor=color.new(color.green, 80))
// Store in arrays
array.push(bullish_fvg_top, fvg_top)
array.push(bullish_fvg_bottom, fvg_bottom)
array.push(bullish_fvg_time, fvg_time)
array.push(bullish_fvg_box, fvg_box)
array.push(bullish_fvg_is_filled, false)
// Detect Bearish FVG
if high < low[2]
// New Bearish FVG detected
fvg_top = high
fvg_bottom = low[2]
fvg_time = time
// Create a box to represent the FVG
fvg_box = box.new(bar_index[2], fvg_top, bar_index, fvg_bottom, border_color=color.new(color.red, 0), bgcolor=color.new(color.red, 80))
// Store in arrays
array.push(bearish_fvg_top, fvg_top)
array.push(bearish_fvg_bottom, fvg_bottom)
array.push(bearish_fvg_time, fvg_time)
array.push(bearish_fvg_box, fvg_box)
array.push(bearish_fvg_is_filled, false)
// Function to process FVGs
f_process_fvg(_fvg_top_array, _fvg_bottom_array, _fvg_time_array, _fvg_box_array, _fvg_is_filled_array, _is_bullish) =>
// Iterate over the FVGs in reverse order to handle array removals
for i = array.size(_fvg_top_array) - 1 to 0 by -1
fvg_is_filled = array.get(_fvg_is_filled_array, i)
fvg_box = array.get(_fvg_box_array, i)
// Extend the box to the current bar
box.set_right(fvg_box, bar_index)
// Get stored FVG levels
fvg_top = array.get(_fvg_top_array, i)
fvg_bottom = array.get(_fvg_bottom_array, i)
// Check if FVG is filled
is_filled = _is_bullish ? (high >= fvg_top and low <= fvg_bottom) : (high >= fvg_bottom and low <= fvg_top)
if not fvg_is_filled and is_filled
array.set(_fvg_is_filled_array, i, true)
// Change the color to indicate it's filled
box.set_bgcolor(fvg_box, color.new(color.gray, 80))
box.set_border_color(fvg_box, color.new(color.gray, 0))
// Check if FVG is older than 10 days (in milliseconds)
fvg_time = array.get(_fvg_time_array, i)
if time - fvg_time > 10 * 24 * 60 * 60 * 1000
// Delete the box and remove from arrays
box.delete(fvg_box)
array.remove(_fvg_top_array, i)
array.remove(_fvg_bottom_array, i)
array.remove(_fvg_time_array, i)
array.remove(_fvg_box_array, i)
array.remove(_fvg_is_filled_array, i)
// Process Bullish FVGs
f_process_fvg(bullish_fvg_top, bullish_fvg_bottom, bullish_fvg_time, bullish_fvg_box, bullish_fvg_is_filled, true)
// Process Bearish FVGs
f_process_fvg(bearish_fvg_top, bearish_fvg_bottom, bearish_fvg_time, bearish_fvg_box, bearish_fvg_is_filled, false)
Explanation:
Indicator Overview:
- Purpose: This indicator identifies Fair Value Gaps (FVGs) on the chart and visualizes them using boxes that extend until they are filled. It also removes FVGs that are older than 10 days.
- Types of FVGs:
- Bullish FVG: Occurs when the current candle's low is higher than the high from two candles ago.
- Bearish FVG: Occurs when the current candle's high is lower than the low from two candles ago.
How It Works:
-
Initialization:
- We initialize several arrays to store data about the FVGs:
bullish_fvg_top, bullish_fvg_bottom, bullish_fvg_time, bullish_fvg_box, bullish_fvg_is_filled for bullish FVGs.
bearish_fvg_top, bearish_fvg_bottom, bearish_fvg_time, bearish_fvg_box, bearish_fvg_is_filled for bearish FVGs.
-
Detection of FVGs:
- Bullish FVG:
- If
low > high[2], it means there's an upward gap between the current low and the high from two candles ago.
- We record the
fvg_top (high[2]) and fvg_bottom (current low).
- A box is drawn from
bar_index[2] to the current bar_index, spanning from fvg_top to fvg_bottom.
- All relevant data is stored in the arrays.
- Bearish FVG:
- If
high < low[2], it indicates a downward gap.
- We record the
fvg_top (current high) and fvg_bottom (low[2]).
- A box is drawn similarly and data is stored.
-
Processing FVGs:
- We use a custom function
f_process_fvg to handle both bullish and bearish FVGs.
- Extending Boxes:
- For each FVG, we extend the right side of the box to the current
bar_index to keep it extending forward until the condition changes.
- Checking for Fill:
- For bullish FVGs, the gap is considered filled if the price action (high and low of current candle) intersects the gap area between
fvg_top and fvg_bottom.
- For bearish FVGs, similarly, we check if the price action intersects the gap area.
- If filled, we change the box color to gray to indicate it's been filled.
- Deleting Old FVGs:
- If an FVG is older than 10 days, we delete the box and remove its data from the arrays.
-
Visualization:
How to Use the Indicator:
-
Adding the Indicator to Your Chart:
- Open TradingView and navigate to a chart.
- Click on the "Pine Editor" tab at the bottom of the page.
- Copy and paste the above code into the editor.
- Click "Add to Chart" to apply the indicator.
-
Interpreting the Indicator:
- Green Boxes: Represent bullish Fair Value Gaps where price may potentially return to fill the gap.
- Red Boxes: Represent bearish Fair Value Gaps.
- Gray Boxes: Indicate that the FVG has been filled.
-
Customization:
- You can adjust the colors or transparency by modifying the
color.new and bgcolor parameters in the box.new function.
- If you wish to change the time frame for deleting old FVGs, adjust the
10 * 24 * 60 * 60 * 1000 calculation (which currently represents 10 days in milliseconds).
Notes:
- Time Calculations: In Pine Script, time is measured in UNIX milliseconds since 1970. To calculate 10 days, we multiply 10 days * 24 hours * 60 minutes * 60 seconds * 1000 milliseconds.
- Looping Through Arrays: We iterate through the arrays in reverse order to prevent issues with changing array sizes when removing elements.
- Box Adjustments: The boxes are dynamically adjusted to extend with each new bar until the gap is filled or the FVG is deleted after 10 days.
Let me know if you need any further customization or assistance with the indicator!