Converting Pine Script to V5

Viewed 24

I am trying to convert an old script to V5 and get the error below.

zigzag() =>
    _isUp = close >= open
    _isDown = close <= open
    _direction = _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz (_direction[1]) 
    _zigzag = _isUp[1] and _isDown and _direction[1] != -1 ? ta.highest(2) : _isDown[1] and _isUp and _direction[1] != 1 ? ta.lowest(2) : na

Undeclared identifier '_direction'

Variable '_direction' is not found in scope 'zigzag', cannot register side effect

What is the problem here? Do you have any recommendations?

1 Answers

Self referencing is not allowed from v3 and above.

You should first define the variable and then use it.

zigzag() =>
    _isUp = close >= open
    _isDown = close <= open
    _direction = 0
    _direction := _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz (_direction[1]) 
    _zigzag = _isUp[1] and _isDown and _direction[1] != -1 ? ta.highest(2) : _isDown[1] and _isUp and _direction[1] != 1 ? ta.lowest(2) : na
Related