请求编写Pine Script:持续展示除当前K线外的最近26根收盘价
Pine Script实现持续展示最近26个非当前K线收盘价的文本
以下是可直接运行的代码,实现你需要的功能:
//@version=5 indicator("最近26个非当前K线收盘价", overlay=false) // 持久化数组存储最近26个非当前K线的收盘价 var array<float> closePrices = array.new_float(26) // 新K线生成时更新数组:移除最早元素,加入前一根K线的收盘价(排除当前K线) if barstate.isnew if array.size(closePrices) >= 26 array.shift(closePrices) array.push(closePrices, close[1]) // 将数组元素格式化为"xxx.xxx;xxx.xxx;..."格式的字符串 priceString = str.join(";", array.map(closePrices, (price) => str.tostring(price, "0.000"))) // 创建持久化标签,在最新K线位置持续显示文本 var label displayLabel = label.new(bar_index, high, priceString, yloc=yloc.abovebar, color=color.blue, textcolor=color.white) if barstate.islast label.set_xy(displayLabel, bar_index, high) label.set_text(displayLabel, priceString)
代码说明:
- 数组存储逻辑:用
var定义数组确保其不会在每根K线重新初始化,barstate.isnew触发数组更新时,始终保留最近26个close[1](即前一根K线的收盘价,排除当前K线)。 - 字符串格式化:通过
array.map将每个价格转为三位小数的字符串,再用str.join用分号拼接成目标格式。 - 持续展示文本:用
var label创建一个持久化标签,仅在最新K线(barstate.islast)时更新标签的位置和文本,保证文本始终显示在当前可视区域的最新K线附近。
内容的提问来源于stack exchange,提问作者Teddol
相关产品推荐
相关产品推荐

