首页 > 解决方案 > Pinescript - 全局变量的历史参考在函数中返回意外结果

问题描述

当涉及到未在每个柱上执行的函数内部的历史引用时,用户定义的全局变量是否与内置变量的处理方式不同?以下两个示例产生不同的结果,其中第一个示例在标签中打印“NaN”,而第二个示例(使用内置关闭变量的示例)打印前一个柱的收盘值(正确的行为):

//@version=4
study("My Script", overlay=true)

_close = close

f()=>
    label l1= label.new(bar_index, high, text=tostring(_close[1]), style=label.style_circle)     

if(barstate.islast)
    f() 
//@version=4
study("My Script", overlay=true)

f()=>
    label l1= label.new(bar_index, high, text=tostring(close[1]), style=label.style_circle)     

if(barstate.islast)
    f() 

这让我非常困惑,因为它暗示内置变量的处理方式与全局用户定义的变量不同?

需要明确的是,我确实理解为什么以下示例不起作用,但我认为上述情况的行为应该有所不同:

//@version=4
study("My Script", overlay=true)

f(_close)=>
    label l1= label.new(bar_index, high, text=tostring(_close[1]), style=label.style_circle)     

if(barstate.islast)
    f(close) 

谁能解释这里发生了什么?谢谢!

标签: pine-script

解决方案


推荐阅读