Want to extend FVG to certain time

22 views Asked by At

I am very new to pine script and am working on some code. I am plotting FVGs for the past 5 days in a certain time range. I have the plotting of the boxes down but I have two problems.

  1. I want to fvgs/boxes to only extend to 10:10(end of macro) on the day they are plotted instead of to the current day.

  2. I want to only plot the first FVG of the macro instead of the first of each(aka only one FVG per time slot). IGNORE THIS I FIGURED IT OUT

This is what I am working with so far and thank you so much for all the help! (PS if anyone has anywhere I can go to learn more about using the time functions since that is where I am tripping up a lot that would be great)

//@version=5
indicator("Fair Value Gap", overlay=true)

// Function to check if the current time is between 9:50 AM and 10:10 AM New York time on the past 5 days
isWithinTimeFrame() =>
    // Define New York time zone offset
    nyOffset = 5 * 60 * 60 // New York is UTC-5 in Standard Time (EST)
    
    // Get current time in New York time zone
    nyTime = time + nyOffset
    
    // Extract date, hour, and minute components of current time in New York time zone
    nyDate = dayofweek(nyTime)
    nyHour = hour(nyTime)
    nyMinute = minute(nyTime)
    
    // Check if current time is between 9:50 AM and 10:10 AM New York time on the past 5 days
    nyDate <= dayofweek(timenow) and nyDate >= dayofweek(timenow) - 4 and ((nyHour == 9 and nyMinute >= 50) or (nyHour == 10 and nyMinute <= 10))

// Function to find the bar index of the last bar before 10:10 AM New York time for a given date
getLastBarIndex(date) =>
    // Define New York time zone offset
    nyOffset = 5 * 60 * 60 // New York is UTC-5 in Standard Time (EST)
    
    // Calculate the timestamp corresponding to 10:10 AM New York time for the given date
    nyTime = timestamp("GMT-5", year(date), month(date), dayofmonth(date), 10, 10)
    
    // Convert New York time to local time
    localTime = nyTime - nyOffset
    
    // Find the bar index of the last bar before 10:10 AM New York time for the given date
    lastBar = bar_index[nz(localTime[1])]
    
    // If the last bar is ahead of the current time, return the last bar, otherwise return na
    lastBar >= nz(bar_index[1]) ? lastBar : na

var bool[] bullishBoxPlotted = array.new_bool(7)
var bool[] bearishBoxPlotted = array.new_bool(7)

// Function to check for the bullish FVG pattern
bullishFVG() =>
    // Check if the first candle has a high lower than the third candle's low
    low > high[2]

// Function to check for the bearish FVG pattern
bearishFVG() =>
    // Check if the first candle has a low higher than the third candle's high
    high < low[2]

// Plot rectangles for bullish and bearish FVG patterns if within the specified time frame
if isWithinTimeFrame()
    lastBarIndex = getLastBarIndex(timestamp(year, month, dayofmonth))
    
    if bullishFVG() and not array.get(bullishBoxPlotted, dayofweek)
        box.new(bar_index, high[2], lastBarIndex, low, color.black, 0, line.style_solid, extend.right, xloc.bar_index, color.new(color.blue, 80))
        array.set(bullishBoxPlotted, dayofweek, true)

    if bearishFVG() and not array.get(bearishBoxPlotted, dayofweek)
        box.new(bar_index, low[2], lastBarIndex, high, color.black, 0, line.style_solid, extend.right, xloc.bar_index, color.new(color.red, 80))
        array.set(bearishBoxPlotted, dayofweek, true)

Ive tried basically everything I have found and most things just end up either plotting the box all the way to the current date which is what I have right now or it doesnt plot the box at all. Like I said I am having trouble with the time functions.

0

There are 0 answers