在PineScript v5中使用request.security_lower_tf能否仅获取最新值?
获取更低时间框架数据的最新值(Pine Script)
当使用request.security_lower_tf()获取子周期数据时,返回的是当前主周期内所有子周期的数值数组。要获取数组的最新/最后一个值,有两种简单实现方式:
方法1:使用ta.last()函数
ta.last()是Pine Script内置函数,专门用于提取数组的最后一个元素,代码更简洁:
// 获取1秒级high的最新值 secLastHigh = ta.last(request.security_lower_tf(syminfo.tickerid, "1S", high)) // 创建表格并显示数值 aTable = table.new(position.top_center, 2, 2) table.cell(aTable, 1, 1, str.tostring(secLastHigh))
方法2:通过数组索引获取
利用数组的array.size()方法获取长度,再通过array.get()提取最后一个元素(数组索引从0开始,最后一个元素的索引为长度减1):
// 获取1秒级high的完整数组 secHighArr = request.security_lower_tf(syminfo.tickerid, "1S", high) // 提取数组最后一个元素 secLastHigh = array.get(secHighArr, array.size(secHighArr) - 1) // 创建表格并显示数值 aTable = table.new(position.top_center, 2, 2) table.cell(aTable, 1, 1, str.tostring(secLastHigh))
两种方法都能实现需求,ta.last()更适合快速取值,数组索引法则便于后续对数组其他元素进行操作。
内容的提问来源于stack exchange,提问作者Lew2234
相关产品推荐
相关产品推荐

