To add a function that draws higher timeframe Fair Value Gaps (FVGs) on lower timeframe charts, you can modify your script as follows:
-
Add Inputs for Higher Timeframe Settings:
htf_enabled = input.bool(defval = true, title = "Enable HTF FVGs", group = "HTF Settings")
htf_tf = input.timeframe("1D", "Higher Timeframe for FVGs", group = "HTF Settings")
-
Define Function to Get Higher Timeframe FVGs:
f_get_htf_fvg() =>
var float fvg_top = na
var float fvg_bottom = na
var bool is_bull = na
var int fvg_state = 0 // 0 - no FVG, 1 - new FVG, 2 - existing FVG
bull_fvg_htf = low > high[2] and close[1] > high[2]
bear_fvg_htf = high < low[2] and close[1] < low[2]
if bull_fvg_htf
fvg_top := high[2]
fvg_bottom := low
is_bull := true
fvg_state := 1 // New FVG detected
else if bear_fvg_htf
fvg_top := high
fvg_bottom := low[2]
is_bull := false
fvg_state := 1 // New FVG detected
else if not na(fvg_top)
fvg_state := 2 // Existing FVG continues
[fvg_state, is_bull, fvg_top, fvg_bottom]
-
Request Higher Timeframe Data and Handle FVGs:
[htf_fvg_state, htf_is_bull, htf_fvg_top, htf_fvg_bottom, htf_time] = request.security(syminfo.tickerid, htf_tf, [f_get_htf_fvg(), time])
var int htf_fvg_start_time = na
var line htf_fvg_line = na
var box htf_fvg_box = na
if htf_enabled
// When a new FVG is detected
if htf_fvg_state == 1
// Store the htf_time
htf_fvg_start_time := htf_time
// Delete previous FVGs
if not na(htf_fvg_line)
line.delete(htf_fvg_line)
htf_fvg_line := na
if not na(htf_fvg_box)
box.delete(htf_fvg_box)
htf_fvg_box := na
// Create new FVG line and box
htf_fvg_line := line.new(x1 = htf_fvg_start_time, y1 = htf_fvg_top, x2 = time, y2 = htf_fvg_top,
extend=extend.right,
color=htf_is_bull ? bullCss : bearCss,
style = line.style_dashed)
htf_fvg_box := box.new(left = htf_fvg_start_time, top = htf_fvg_top, right = time, bottom = htf_fvg_bottom,
bgcolor = htf_is_bull ? bullAreaCss : bearAreaCss,
border_width = 0)
// Else if FVG is continuing
else if htf_fvg_state == 2
// Update the FVG line and box right coordinates
if not na(htf_fvg_line)
line.set_x2(htf_fvg_line, time)
if not na(htf_fvg_box)
box.set_right(htf_fvg_box, time)
// Else if no FVG
else if htf_fvg_state == 0
// No FVG, delete any existing lines and boxes
if not na(htf_fvg_line)
line.delete(htf_fvg_line)
htf_fvg_line := na
if not na(htf_fvg_box)
box.delete(htf_fvg_box)
htf_fvg_box := na
htf_fvg_start_time := na
// Check for mitigation
// For bullish FVG, mitigated when price goes below bottom
if not na(htf_is_bull) and htf_fvg_state != 0
if htf_is_bull and low < htf_fvg_bottom
// Mitigated
if not na(htf_fvg_box)
box.set_bgcolor(htf_fvg_box, bullMitigatedCss)
elif not htf_is_bull and high > htf_fvg_top
// Mitigated
if not na(htf_fvg_box)
box.set_bgcolor(htf_fvg_box, bearMitigatedCss)
-
Integrate the Above into Your Existing Script:
Place the above code appropriately within your script. Ensure you maintain the structure and variable definitions to avoid any conflicts.
Here's how the full modified script would look with the added functionality:
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
indicator("FVG Sessions [LuxAlgo]", overlay = true, max_lines_count = 500, max_boxes_count = 500)
//------------------------------------------------------------------------------
// Inputs
//------------------------------------------------------------------------------
bullCss = input.color(color.teal, 'FVG Level' , inline = 'bull')
bullAreaCss = input.color(color.new(color.teal, 50), 'Area' , inline = 'bull')
bullMitigatedCss = input.color(color.new(color.teal, 80), 'Mitigated', inline = 'bull')
bearCss = input.color(color.red, 'FVG Level' , inline = 'bear')
bearAreaCss = input.color(color.new(color.red, 50), 'Area' , inline = 'bear')
bearMitigatedCss = input.color(color.new(color.red, 80), 'Mitigated' , inline = 'bear')
htf_enabled = input.bool(defval = true, title = "Enable HTF FVGs", group = "HTF Settings")
htf_tf = input.timeframe("1D", "Higher Timeframe for FVGs", group = "HTF Settings")
//------------------------------------------------------------------------------
// UDT's
//------------------------------------------------------------------------------
type fvg
float top
float btm
bool mitigated
bool isnew
bool isbull
line lvl
box area
type session_range
line max
line min
//------------------------------------------------------------------------------
// Methods
//------------------------------------------------------------------------------
n = bar_index
//Method for setting fair value gaps
method set_fvg(fvg id, offset, bg_css, l_css)=>
avg = math.avg(id.top, id.btm)
area = box.new(n - offset, id.top, n, id.btm, na, bgcolor = bg_css)
avg_l = line.new(n - offset, avg, n, avg, color = l_css, style = line.style_dashed)
id.lvl := avg_l
id.area := area
//Method for setting session range maximum/minimum
method set_range(session_range id)=>
max = math.max(high, id.max.get_y2())
min = math.min(low, id.min.get_y2())
id.max.set_xy2(n, max)
id.max.set_y1(max)
id.min.set_xy2(n, min)
id.min.set_y1(min)
//------------------------------------------------------------------------------
// Variables
//------------------------------------------------------------------------------
var chartCss = color.new(chart.fg_color, 50)
var fvg sfvg = fvg.new(na, na, na, true, na)
var session_range sesr = na
var box area = na
var line avg = na
bull_fvg = low > high[2] and close[1] > high[2]
bear_fvg = high < low[2] and close[1] < low[2]
//Alert conditions
bull_isnew = false
bear_isnew = false
bull_mitigated = false
bear_mitigated = false
within_bull_fvg = false
within_bear_fvg = false
//------------------------------------------------------------------------------
// New session
//------------------------------------------------------------------------------
dtf = timeframe.change('D')
//On new session
if dtf
//Set delimiter
line.new(n, high + syminfo.mintick
, n, low - syminfo.mintick
, color = chartCss
, style = line.style_dashed
, extend = extend.both)
//Set new range
sesr := session_range.new(
line.new(n, high, n, high, color = chartCss)
, line.new(n, low, n, low, color = chartCss))
sfvg.isnew := true
//Set prior session fvg right coordinates
if not na(sfvg.lvl)
sfvg.lvl.set_x2(n-2)
sfvg.area.set_right(n-2)
//Set range
else if not na(sesr)
sesr.set_range()
//Set range lines color
sesr.max.set_color(sfvg.isbull ? bullCss : bearCss)
sesr.min.set_color(sfvg.isbull ? bullCss : bearCss)
//------------------------------------------------------------------------------
// Set FVG
//------------------------------------------------------------------------------
if bull_fvg and sfvg.isnew
sfvg := fvg.new(low, high[2], false, false, true)
sfvg.set_fvg(2, bullAreaCss, bullCss)
bull_isnew := true
else if bear_fvg and sfvg.isnew
sfvg := fvg.new(low[2], high, false, false, false)
sfvg.set_fvg(2, bearAreaCss, bearCss)
bear_isnew := true
if not sfvg.mitigated
if sfvg.isbull and close < sfvg.btm
sfvg.set_fvg(1, bullMitigatedCss, bullCss)
sfvg.mitigated := true
bull_mitigated := true
else if not sfvg.isbull and close > sfvg.top
sfvg.set_fvg(1, bearMitigatedCss, bearCss)
sfvg.mitigated := true
bear_mitigated := true
if not sfvg.isnew
sfvg.lvl.set_x2(n)
sfvg.area.set_right(n)
//------------------------------------------------------------------------------
// Alerts
//------------------------------------------------------------------------------
alertcondition(bull_isnew, 'Bullish FVG', 'New session bullish fvg')
alertcondition(bear_isnew, 'Bearish FVG', 'New session bearish fvg')
alertcondition(bull_mitigated, 'Mitigated Bullish FVG', 'Session bullish fvg has been mitigated')
alertcondition(bear_mitigated, 'Mitigated Bearish FVG', 'Session bearish fvg has been mitigated')
alertcondition(close >= sfvg.btm and close <= sfvg.top and sfvg.isbull and not sfvg.isnew
, 'Price Within Bullish FVG'
, 'Price is within bullish fvg')
alertcondition(close >= sfvg.btm and close <= sfvg.top and not sfvg.isbull and not sfvg.isnew
, 'Price Within Bearish FVG'
, 'Price is within bearish fvg')
alertcondition(ta.cross(close, math.avg(sfvg.top, sfvg.btm)) and sfvg.isbull and not sfvg.isnew
, 'Bullish FVG AVG Cross'
, 'Price crossed bullish fvg average')
alertcondition(ta.cross(close, math.avg(sfvg.top, sfvg.btm)) and not sfvg.isbull and not sfvg.isnew
, 'Bearish FVG AVG Cross'
, 'Price crossed bearish fvg average')
//------------------------------------------------------------------------------
// Higher Timeframe FVGs
//------------------------------------------------------------------------------
f_get_htf_fvg() =>
var float fvg_top = na
var float fvg_bottom = na
var bool is_bull = na
var int fvg_state = 0 // 0 - no FVG, 1 - new FVG, 2 - existing FVG
bull_fvg_htf = low > high[2] and close[1] > high[2]
bear_fvg_htf = high < low[2] and close[1] < low[2]
if bull_fvg_htf
fvg_top := high[2]
fvg_bottom := low
is_bull := true
fvg_state := 1 // New FVG detected
else if bear_fvg_htf
fvg_top := high
fvg_bottom := low[2]
is_bull := false
fvg_state := 1 // New FVG detected
else if not na(fvg_top)
fvg_state := 2 // Existing FVG continues
[fvg_state, is_bull, fvg_top, fvg_bottom]
[htf_fvg_state, htf_is_bull, htf_fvg_top, htf_fvg_bottom, htf_time] = request.security(syminfo.tickerid, htf_tf, [f_get_htf_fvg(), time])
var int htf_fvg_start_time = na
var line htf_fvg_line = na
var box htf_fvg_box = na
if htf_enabled
// When a new FVG is detected
if htf_fvg_state == 1
// Store the htf_time
htf_fvg_start_time := htf_time
// Delete previous FVGs
if not na(htf_fvg_line)
line.delete(htf_fvg_line)
htf_fvg_line := na
if not na(htf_fvg_box)
box.delete(htf_fvg_box)
htf_fvg_box := na
// Create new FVG line and box
htf_fvg_line := line.new(x1 = htf_fvg_start_time, y1 = htf_fvg_top, x2 = time, y2 = htf_fvg_top,
extend=extend.right,
color=htf_is_bull ? bullCss : bearCss,
style = line.style_dashed)
htf_fvg_box := box.new(left = htf_fvg_start_time, top = htf_fvg_top, right = time, bottom = htf_fvg_bottom,
bgcolor = htf_is_bull ? bullAreaCss : bearAreaCss,
border_width = 0)
// Else if FVG is continuing
else if htf_fvg_state == 2
// Update the FVG line and box right coordinates
if not na(htf_fvg_line)
line.set_x2(htf_fvg_line, time)
if not na(htf_fvg_box)
box.set_right(htf_fvg_box, time)
// Else if no FVG
else if htf_fvg_state == 0
// No FVG, delete any existing lines and boxes
if not na(htf_fvg_line)
line.delete(htf_fvg_line)
htf_fvg_line := na
if not na(htf_fvg_box)
box.delete(htf_fvg_box)
htf_fvg_box := na
htf_fvg_start_time := na
// Check for mitigation
// For bullish FVG, mitigated when price goes below bottom
if not na(htf_is_bull) and htf_fvg_state != 0
if htf_is_bull and low < htf_fvg_bottom
// Mitigated
if not na(htf_fvg_box)
box.set_bgcolor(htf_fvg_box, bullMitigatedCss)
elif not htf_is_bull and high > htf_fvg_top
// Mitigated
if not na(htf_fvg_box)
box.set_bgcolor(htf_fvg_box, bearMitigatedCss)
Explanation:
- Inputs: Introduced new inputs
htf_enabled and htf_tf to enable and set the higher timeframe for FVGs.
- Function
f_get_htf_fvg(): This function calculates the FVGs on the higher timeframe and returns the state (fvg_state), whether it's bullish or bearish (is_bull), and the top and bottom levels (fvg_top, fvg_bottom).
- Requesting Data: Used
request.security() to get higher timeframe data and the FVG information.
- Plotting: Created lines and boxes to represent the higher timeframe FVGs on the lower timeframe chart.
- Mitigation Check: Added logic to check if the higher timeframe FVG has been mitigated and update the colors accordingly.
By integrating this code into your existing script, you'll be able to visualize higher timeframe Fair Value Gaps on your lower timeframe charts, providing a more comprehensive view of market structures across different timeframes.