Max
Max

Reputation: 37

Save intermediate price of realtime candle in var

I want to write the candle price in a var for every true of my indicator. I also want to output every candle price that was achieved by the indicator over the day in a total. Therefore, I want to summarize all profits that were made using the indicator over a session day in a total. Since I work with realtime candles, I don't want to have the close, open, high or low price because if I specify var myprice = close for realtime candles, I won't see the current price when the indicator = true, but the last close price of the entire realtime candle. It would be important for me to save the realtime price for the indicator true in a var, which is available when the indicator goes to true

Is there a Pinescript solution for this problem?

I tried this solution but it doesn't work here either:

varip myprice := ta.valuewhen(myindcator == true, close,0)

Upvotes: 0

Views: 63

Answers (1)

vgladkov
vgladkov

Reputation: 554

This can be done using varip, you initialize the variable as na, and then at each tick you check whether the condition of your indicator is met and the variable doesn't has a value, if it is true, then assign a new value, if not true, just leave the old value. Then on each new bar on first update before checking your condition drop the variable to na state

Example:

//@version=5
indicator("My scipt")
myCond = close > open and barstate.isrealtime
varip myPrice = float (na)
if not na(myPrice[1]) and barstate.isnew
    myPrice := na
myPrice := myCond and na(myPrice) ? close : myPrice
plot(myPrice)

Upvotes: 0

Related Questions