Pine Script v5无法返回linefills数组[Bug报告]
Pine Script 5编译错误:返回填充线数组触发Internal server pine compilation error
报错信息
研究脚本错误
Internal server pine compilation error
触发错误的代码
//@version=5 indicator("Test") test() => foo = line.new(bar_index, close, bar_index + 1, close, xloc.bar_index, color = color.red) bar = line.new(bar_index, open, bar_index + 1, open, xloc.bar_index, color = color.green) lf = linefill.new(foo, bar, color.new(color.red, 80)) array.from(lf) // 我们希望返回多个linefills lf = test()
问题原因
这并非Pine Script的bug,而是因为linefill属于Pine的图形对象引用类型,这类对象目前不支持被存入数组或从函数返回数组形式的引用。Pine Script对图形对象(包括line、label、linefill等)的处理有严格限制,它们的引用只能在创建它们的作用域内直接操作,无法通过数组打包后跨作用域传递。
解决方案
如果需要在函数外部操作创建的填充线,可采用以下两种方式:
- 直接在函数内部完成填充线的后续逻辑,无需返回引用
- 提前声明全局数组,在函数内部将linefill引用存入该全局数组,而非尝试返回数组
示例代码(全局数组方式):
//@version=5 indicator("Test") // 提前声明全局数组存储linefill引用 var linefill_array = array.new_linefill() test() => foo = line.new(bar_index, close, bar_index + 1, close, xloc.bar_index, color = color.red) bar = line.new(bar_index, open, bar_index + 1, open, xloc.bar_index, color = color.green) lf = linefill.new(foo, bar, color.new(color.red, 80)) // 将填充线引用存入全局数组 array.push(linefill_array, lf) // 调用函数创建填充线 test() // 示例:每10根K线删除最早的填充线 if bar_index % 10 == 0 if array.size(linefill_array) > 0 linefill.delete(array.shift(linefill_array))
内容的提问来源于stack exchange,提问作者elod008
相关产品推荐
相关产品推荐

