变量意外更改值(Pine脚本)



这里有一个简单的脚本代码:

//@version=4
study("test", overlay=false, precision=8)
var last_price = 0.0
var move = 0.0
if barstate.isrealtime
move := move + abs(last_price - close)
last_price := close
plot(move)

正如我所理解的,变量move一直都必须变得越来越大,但事实并非如此
为什么会出现这种情况?

这是预期的行为,也是由于回滚过程。当脚本到达barstate.islast变为true的最后一个栏时,该栏开头的move的值为0.0,这是在实时栏中脚本每次迭代之前move重置为的值。

我几乎要放弃了。

但自2021年3月以来,varip被引入:

varip - is similar to the var keyword, but variables declared with varip retain their values between the updates **of a real-time bar**.

https://github.com/tradingview/pine_script_docs/blob/master/source/Release_notes.rst

更重要的是:它有效。:(

最新更新