I am trying to count the number of consecutive bullish or bearish Fair Value Gaps for multiple time frames (count resets to zero when we flip from a bullish to bearish or vice versa).
Per the code below, the cumulative count is correct if my chart is set to the same time frame as the count, but if I change the chart time frame, the count updates incorrectly. Any suggestions on how I can count correctly for my desired time frames, even when the chart is set to a different time frame?
Thanks
//@version=5
indicator("FVG Count", overlay = true)
// Time frames
M1 = "1"
// Fetch data from multiple time frames
HighM1 = request.security(syminfo.tickerid, M1, high)
LowM1 = request.security(syminfo.tickerid, M1, low)
// FVG Definition
BullishFVGM1 = LowM1[1] > HighM1[3]
BearishFVGM1 = LowM1[3] > HighM1[1]
// Initialize counters
var int BullishFVGCountM1 = 0
var int BearishFVGCountM1 = 0
// Consecutive count - resets when we flip from bullish or bearish FVGs
if BullishFVGM1
BearishFVGCountM1 := 0
BullishFVGCountM1 += 1
if BearishFVGM1
BullishFVGCountM1 := 0
BearishFVGCountM1 -= 1
// table
var table myTable = table.new(position.top_right, 3, 3, border_width=1, border_color=color.gray)
table.cell(myTable, 0, 0, "Period", text_color=color.white, bgcolor=color.blue)
table.cell(myTable, 1, 0, "Bullish Count", text_color=color.white, bgcolor=color.blue)
table.cell(myTable, 2, 0, "Bearish Count", text_color=color.white, bgcolor=color.blue)
table.cell(myTable, 0, 1, "M1", text_color=color.white, bgcolor=color.blue)
table.cell(myTable, 1, 1, str.tostring(BullishFVGCountM1), text_color=color.white, bgcolor=color.blue)
table.cell(myTable, 2, 1, str.tostring(BearishFVGCountM1), text_color=color.white, bgcolor=color.blue)`
1