1

I'm working on something in Pine Script v5, but I'm stuck with something. I would like to store a value once, if conditions are met, and use this value later. But the value is still recalculated at each candle.

For example:

// initiate value
int go_long = 0

BullTrend = (close - lowest(low, 50)) / atr(5)

// Wait for condition 1
if BullTrend < 2 and ta.sma(close,15) > 15
    go_long := 1

// Wait for condition 2 later , but based on previous status of go_long
if go_long == 1 and open > close
    go_long := 2

plotshape(go_long == 2, style=shape.arrowup)`

This is just a nonsense example to highlight the problem I'm facing. In this case the go_long value changes at each candlestick, and that's not what I want.

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
EyFFix
  • 13
  • 2

1 Answers1

1

With

int go_long = 0

you set go_long to 0 on each candle.

To initialize a variable only at the beginning you should use var:

var int go_long = 0
CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
G.Lebret
  • 2,826
  • 2
  • 16
  • 27