Miro Krsjak
Miro Krsjak

Reputation: 363

Pine script store own data with each bar and access its history

is there any way to store boolean data for each bar? I seem to be missing the "bigger" idea how to do this in Pine script v5. For each bar, based on calculations, I want to assign a variable, and each iteration, go back in time and check this information and access also that bars OHLC. thanks for pointing to a direction

Upvotes: 0

Views: 181

Answers (1)

vitruvius
vitruvius

Reputation: 21342

That's how series work. You just do your calculations and assign it to a value. Then you can access its historical values with the [ ] history-referencing operator.

//@version=5
indicator("My script", overlay=true)

is_green = (close >= open)  // Your calculation

plotchar(is_green, "is_green", "")
plotchar(is_green[1], "is_green[1]", "")
plotchar(is_green[2], "is_green[2]", "")

Or if you want to save some value when a specific event happens, you can use var variables for that. They will keep their values between iterations unless you overwrite them.

var int green_idx = na
var float green_close = na
var float green_open = na

// Save data at specific events
if (is_green)
    green_idx := bar_index
    green_close := close
    green_open := open

Upvotes: 0

Related Questions