Pine Script数组仅push一次却长度异常激增问题求助
Pine Script数组异常问题分析与解决
问题现象
在TradingView中测试Pine Script的array功能时遇到两个异常:
- 创建float类型数组后仅执行一次
array.push()添加数值5,但打印数组长度显示为7776 - 尝试直接打印数组时触发报错:
Study error: Error on bar 7775: String is too long 23328, max allowed length is 4096.
用户测试代码:
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=5 indicator("test indicator", overlay=false) var test_array = array.new_float() var tt = 5 array.push(id = test_array, value = 5) var groupPanel = 'test table' string table_vertical_pos = input.string('bottom', 'Panel position', inline='11', options=['top', 'middle', 'bottom'], group=groupPanel) string table_horizontal_pos = input.string('right', '', inline='11', options=['left', 'center', 'right'], group=groupPanel) text_color = color.black text_color_white = color.white fn_fill_header(_id, _row) => table.cell(table_id=_id, column=0, row=_row, text='Criteria', width=0, height=0, text_color=text_color_white, text_size=size.small, bgcolor=color.rgb(92, 92, 94)) table.cell(table_id=_id, column=1, row=_row, text='', width=0, height=0, text_color=text_color_white, text_size=size.small, bgcolor= color.rgb(92, 92, 94)) fn_fill_value(_id, _row, criteria, value, _color_value) => table.cell(table_id=_id, column=0, row=_row, text=criteria, width=0, height=0, text_color=text_color, text_size=size.small, bgcolor=color.white) table.cell(table_id=_id, column=1, row=_row, text=value, width=0, height=0, text_color=text_color, text_size=size.small, bgcolor=_color_value) var T = table.new( position= table_vertical_pos + "_" + table_horizontal_pos, columns=2, rows=10, bgcolor=color.white, frame_color=color.gray, frame_width=2, border_color=color.gray, border_width=1) if barstate.islastconfirmedhistory fn_fill_header(T, 0) fn_fill_value(T, 1, 'test', str.tostring(test_array), color.rgb(255, 255, 255))
问题根源
- Pine Script的代码默认会在每一根K线周期都运行一次,你写的
array.push(id = test_array, value = 5)没有添加执行限制,导致从第一根K线到最后一根K线(共7776根),每根K线都执行一次push操作,数组被重复添加了7776个5,所以长度显示为7776。 - 当用
str.tostring(test_array)将数组转为字符串时,7776个数值的字符串总长度远超TradingView允许的4096字符上限,因此触发字符串过长的报错。
修复方案
需要把array.push()的执行限制为仅运行一次,有两种常用实现方式:
方案1:用var关键字包裹push操作(推荐)
var修饰的代码只会在脚本初始化时执行一次,不会随K线重复运行:
// 替换原数组初始化与push代码 var test_array = array.new_float() var _ = array.push(id = test_array, value = 5)
方案2:用barstate.isfirst控制仅在第一根K线执行
通过判断当前是否为第一根K线,限制push操作仅执行一次:
// 替换原数组初始化与push代码 var test_array = array.new_float() if barstate.isfirst array.push(id = test_array, value = 5)
修复后完整代码
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=5 indicator("test indicator", overlay=false) var test_array = array.new_float() var _ = array.push(id = test_array, value = 5) // 仅初始化时执行一次push var groupPanel = 'test table' string table_vertical_pos = input.string('bottom', 'Panel position', inline='11', options=['top', 'middle', 'bottom'], group=groupPanel) string table_horizontal_pos = input.string('right', '', inline='11', options=['left', 'center', 'right'], group=groupPanel) text_color = color.black text_color_white = color.white fn_fill_header(_id, _row) => table.cell(table_id=_id, column=0, row=_row, text='Criteria', width=0, height=0, text_color=text_color_white, text_size=size.small, bgcolor=color.rgb(92, 92, 94)) table.cell(table_id=_id, column=1, row=_row, text='', width=0, height=0, text_color=text_color_white, text_size=size.small, bgcolor= color.rgb(92, 92, 94)) fn_fill_value(_id, _row, criteria, value, _color_value) => table.cell(table_id=_id, column=0, row=_row, text=criteria, width=0, height=0, text_color=text_color, text_size=size.small, bgcolor=color.white) table.cell(table_id=_id, column=1, row=_row, text=value, width=0, height=0, text_color=text_color, text_size=size.small, bgcolor=_color_value) var T = table.new( position= table_vertical_pos + "_" + table_horizontal_pos, columns=2, rows=10, bgcolor=color.white, frame_color=color.gray, frame_width=2, border_color=color.gray, border_width=1) if barstate.islastconfirmedhistory fn_fill_header(T, 0) fn_fill_value(T, 1, 'test', str.tostring(test_array), color.rgb(255, 255, 255))
内容的提问来源于stack exchange,提问作者Archits
相关产品推荐
相关产品推荐

