Create TradingView indicator that Draw Higher Timer Frame FVG in Lower Time Frame
- User
Create TradingView indicator that Draw Higher Timer Frame FVG in Lower Time Frame
- Assistant
Create TradingView indicator that Draw Higher Timer Frame FVG in Lower Time Frame
Certainly! Below is a TradingView indicator written in Pine Script that draws Higher Time Frame Fair Value Gaps (FVG) on a Lower Time Frame chart. This indicator identifies bullish and bearish FVGs on a higher timeframe specified by the user and plots them on the current chart. The FVGs are represented as shaded areas that extend until they are filled by price action.
//@version=5
indicator(title="Higher Time Frame Fair Value Gaps (FVG)", overlay=true)
// User input for higher timeframe
htf = input.timeframe("60", "Higher Timeframe") // Default is 60-minute timeframe
// Fetch higher timeframe high and low prices for current and previous bars
htf_high_0 = request.security(syminfo.tickerid, htf, high[0], barmerge.gaps_off, barmerge.lookahead_on)
htf_low_0 = request.security(syminfo.tickerid, htf, low[0], barmerge.gaps_off, barmerge.lookahead_on)
htf_high_1 = request.security(syminfo.tickerid, htf, high[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_low_1 = request.security(syminfo.tickerid, htf, low[1], barmerge.gaps_off, barmerge.lookahead_on)
htf_high_2 = request.security(syminfo.tickerid, htf, high[2], barmerge.gaps_off, barmerge.lookahead_on)
htf_low_2 = request.security(syminfo.tickerid, htf, low[2], barmerge.gaps_off, barmerge.lookahead_on)
// Detect Bullish Fair Value Gap (FVG)
// Occurs when the low of bar 2 is higher than the high of bar 3
bullFVG = (htf_low_1 > htf_high_2)
// Detect Bearish Fair Value Gap (FVG)
// Occurs when the high of bar 2 is lower than the low of bar 3
bearFVG = (htf_high_1 < htf_low_2)
// Initialize arrays to store lines representing FVGs
var line[] bullTopLines = array.new_line()
var line[] bullBottomLines = array.new_line()
var line[] bearTopLines = array.new_line()
var line[] bearBottomLines = array.new_line()
// Plot Bullish FVGs
if bullFVG
// Create lines at the top and bottom of the FVG and extend them to the right
bullTopLine = line.new(x1=bar_index, y1=htf_low_1, x2=bar_index+1, y2=htf_low_1, extend=extend.right, color=color.green)
bullBottomLine = line.new(x1=bar_index, y1=htf_high_2, x2=bar_index+1, y2=htf_high_2, extend=extend.right, color=color.green)
// Add lines to arrays
array.push(bullTopLines, bullTopLine)
array.push(bullBottomLines, bullBottomLine)
// Shade the area between the lines
fill(bullTopLine, bullBottomLine, color=color.new(color.green, 90))
// Plot Bearish FVGs
if bearFVG
// Create lines at the top and bottom of the FVG and extend them to the right
bearTopLine = line.new(x1=bar_index, y1=htf_high_2, x2=bar_index+1, y2=htf_high_2, extend=extend.right, color=color.red)
bearBottomLine = line.new(x1=bar_index, y1=htf_low_1, x2=bar_index+1, y2=htf_low_1, extend=extend.right, color=color.red)
// Add lines to arrays
array.push(bearTopLines, bearTopLine)
array.push(bearBottomLines, bearBottomLine)
// Shade the area between the lines
fill(bearTopLine, bearBottomLine, color=color.new(color.red, 90))
// Function to check if a bullish FVG has been filled
f_check_bull_fvg_fill() =>
for i = 0 to array.size(bullTopLines) - 1 by 1
var line topLine_i = array.get(bullTopLines, i)
var line bottomLine_i = array.get(bullBottomLines, i)
var float fvg_top = line.get_y1(topLine_i)
var float fvg_bottom = line.get_y1(bottomLine_i)
// Check if the current price has filled the FVG
if low <= fvg_top
// Delete lines and remove them from arrays
line.delete(topLine_i)
line.delete(bottomLine_i)
array.remove(bullTopLines, i)
array.remove(bullBottomLines, i)
// Adjust the index after removal
i := i - 1
// Function to check if a bearish FVG has been filled
f_check_bear_fvg_fill() =>
for i = 0 to array.size(bearTopLines) - 1 by 1
var line topLine_i = array.get(bearTopLines, i)
var line bottomLine_i = array.get(bearBottomLines, i)
var float fvg_top = line.get_y1(topLine_i)
var float fvg_bottom = line.get_y1(bottomLine_i)
// Check if the current price has filled the FVG
if high >= fvg_bottom
// Delete lines and remove them from arrays
line.delete(topLine_i)
line.delete(bottomLine_i)
array.remove(bearTopLines, i)
array.remove(bearBottomLines, i)
// Adjust the index after removal
i := i - 1
// Call the functions on each bar
f_check_bull_fvg_fill()
f_check_bear_fvg_fill()
Higher Timeframe Selection: The indicator allows you to select a higher timeframe (htf) using the input at the top. The default higher timeframe is set to 60 minutes.
Fetching Higher Timeframe Data: It retrieves the high and low prices of the current and previous two bars on the higher timeframe using the request.security() function.
Detecting Fair Value Gaps:
htf_low_1) is higher than the high of the third previous bar (htf_high_2). This indicates a gap in price that may act as support in the future.htf_high_1) is lower than the low of the third previous bar (htf_low_2). This indicates a gap in price that may act as resistance in the future.Plotting the FVGs:
Managing FVGs:
Adding the Indicator to Your Chart:
Configuring the Higher Timeframe:
240 for 4-hour, D for daily).Interpreting the FVGs:
Customizing the Indicator (Optional):
color and fill functions within the script.60), 240 minutes (240), daily (D), etc., as your higher timeframe.Suppose you're analyzing a 15-minute chart and want to see where the 1-hour FVGs are located to identify potential support and resistance areas:
60 (for 60 minutes).Feel free to customize and adjust the indicator according to your trading needs. Always ensure you understand how an indicator works before using it in live trading.
Not yet included in a published release.