Pine Script中分离低时间框架元组值并显示的问题求助
解决Pine Script中低时间框架收盘价拆分与显示问题
问题核心
request.security_lower_tf返回的是数组类型,并非可直接解构的元组,直接用[fir, sec, thi]赋值会触发错误- 原代码存在变量名不匹配问题:解构的变量是
fir/sec/thi,但表格调用时用的是a/b/c - 需要精准获取当前15分钟K线内的3根5分钟K线收盘价,而非历史低时间框架的全部数据
修正后的完整代码
//@version=5 indicator("当前15分钟K线内的5分钟收盘价", overlay=false) // 获取当前大周期K线内的所有5分钟收盘价数组 lower_tf_closes = request.security_lower_tf(syminfo.tickerid, "5", close) // 初始化变量,避免无值时报错 fir = na sec = na thi = na // 确保数组长度足够(15分钟=3根5分钟K线),再提取对应收盘价 if array.size(lower_tf_closes) >= 3 // 取数组最后3个元素,对应当前15分钟K线内的3根5分钟K线 fir = array.get(lower_tf_closes, array.size(lower_tf_closes) - 3) sec = array.get(lower_tf_closes, array.size(lower_tf_closes) - 2) thi = array.get(lower_tf_closes, array.size(lower_tf_closes) - 1) // 创建表格并展示数据 var table price_table = table.new(position.bottom_center, 3, 1) table.cell(price_table, 0, 0, text=str.tostring(fir, "#.####"), bgcolor=color.new(color.blue, 0), text_color=color.white, width=8, height=8) table.cell(price_table, 1, 0, text=str.tostring(sec, "#.####"), bgcolor=color.new(color.red, 0), text_color=color.white, width=8, height=8) table.cell(price_table, 2, 0, text=str.tostring(thi, "#.####"), bgcolor=color.new(color.yellow, 0), text_color=color.white, width=8, height=8)
关键调整说明
- 用
array.get()从返回数组中提取元素:数组末尾3个值就是当前15分钟K线内的5分钟收盘价 - 增加数组长度判断,避免行情启动初期数据不足导致的报错
- 统一变量名,修复原代码中变量不匹配的问题
- 给
str.tostring()添加格式参数#.####,让价格显示更规整 - 调整表格列数为3,避免原代码中列重复覆盖的问题
内容的提问来源于stack exchange,提问作者paulcab
相关产品推荐
相关产品推荐

