I have a requirement to detect candlestick patterns in historical candles on each live candlestick bar formation and fetch their bar index.
For example, Suppose In Live market trading, I want to know if any candle of size 10 points is present in today's session historical candles.
I tried using for loop to backtrack but cant make it work as I am very new to pinescript.
Thanks in advance.
No need for a loop. Your script will be executed on each bar. So, you can keep track of that while new bars form. You can have a var
array for the bar indeces. That way, its value won't be lost during the next iterations.
var idx_arr = array.new_int()
if is_new_session // First bar of the new session
array.clear(idx_arr) // Clear the array so the previous session values are gone
if (is_in_session) // Only update your array when you are in a session
if (your condition) // Check for your condition
array.push(idx_arr. bar_index) // Push the bar index to your array whenever your condition is satisfied