Max
Max

Reputation: 37

Pinescript - Realtime close value in the past

When using my indicator, I would like to write the realtime price of a realtime candle in a var and use it later for price calculations. The problem is, however, if I write the price like this:

varip float myvar = close

in a realtime candle, the value is filled with the actual last close of the candle as soon as the candle is no longer a realtime candle, so that later, when I want to access the price, I always get the candle close price and not the close price that was the price in the realtime candle when my indicator reacted. How can I write the realtime candle price in the middle of the candle in a var so that I can calculate exactly this price later without just getting the end close of the candle.

Upvotes: 0

Views: 57

Answers (1)

vitruvius
vitruvius

Reputation: 21342

You need to conditoinally save the value. Something like below:

varip bool is_written = false
varip flaot price = na

if (your_condition and not is_written)
    price := close
    is_written := true

While in theory this would work, if you trigger any re-execution of the script (refresh your page, change a user input, change timeframe, change ticker etc.), you will lose this data.

Upvotes: 0

Related Questions