You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script中ETF板块相对强度表格自动排名实现咨询

ETF板块相对强度自动排名与排序实现方案

完全可以实现你需要的按板块强弱升/降序排列和1-11排名编号功能,下面是重构后的完整代码,针对零基础做了详细注释:

indicator("Sector Relative Strength", overlay = true)

// 1. 定义需要监控的ETF板块列表
sector_symbols = ["XLP", "XLB", "XLE", "XLU", "XLF", "XLI", "XLK", "XLV", "XLY", "XLRE", "XLC"]

// 2. 创建数组存储板块数据:每个元素包含(板块代码, 12个月涨跌幅数值, 显示文本, 文本颜色)
var sector_data = array.new_tuple()

// 3. 批量计算每个板块的12个月涨跌幅,并存入数组
for sym in sector_symbols
    // 获取周线级别最新收盘价和52周前收盘价
    last_close = request.security(sym, "W", close)
    twelve_month_close = request.security(sym, "W", close[52])
    // 计算涨跌幅百分比数值
    pct_change = (last_close - twelve_month_close) / twelve_month_close * 100
    // 生成表格显示文本
    display_text = sym + " M12: " + str.tostring(pct_change, format.percent)
    // 保留你原来的颜色逻辑:涨跌幅为负时红色,正的绿色
    text_color = (twelve_month_close > last_close) ? color.red : color.green
    // 将数据存入数组
    array.push(sector_data, tuple.new(sym, pct_change, display_text, text_color))

// 4. 对板块按涨跌幅排序:order.descending=降序(最强在前),改成order.ascending就是升序
array.sort(sector_data, (a, b) => tuple.get(a, 1) - tuple.get(b, 1), order.descending)

// 5. 创建表格,新增排名列
var _t = table.new(position.middle_left, 3, 12, frame_width = 5, border_width = 1)
// 设置表头
table.cell(_t, 0, 0, "排名", text_size = size.small)
table.cell(_t, 1, 0, "板块", text_size = size.small)
table.cell(_t, 2, 0, "12月涨跌幅", text_size = size.small)

// 6. 循环填充表格,自动生成排名
for i = 0 to array.size(sector_data) - 1
    current_data = array.get(sector_data, i)
    // 排名编号:循环从0开始,加1得到1-11的连续编号
    rank_num = str.tostring(i + 1)
    sector_name = tuple.get(current_data, 0)
    display_text = tuple.get(current_data, 2)
    text_color = tuple.get(current_data, 3)
    
    table.cell(_t, 0, i + 1, rank_num, text_size = size.small)
    table.cell(_t, 1, i + 1, sector_name, text_size = size.small)
    table.cell(_t, 2, i + 1, display_text, text_color = text_color, text_size = size.small)

// 保留你原来的主标的6/12月动量显示(不需要可直接删除)
var _t1 = table.new(position.bottom_right, 5, 2, frame_width = 2, border_width = 2)
lastMonthmomo = request.security(syminfo.tickerid, "W", close)
sixMonthsclosemomo = request.security(syminfo.tickerid, "W", close[26])
indTextM6momo = "M6: " + str.tostring((lastMonthmomo - sixMonthsclosemomo)/sixMonthsclosemomo *100, format.percent)
twelveMonthsclosemomo = request.security(syminfo.tickerid, "W", close[52])
indTextM12momo = "M12: " + str.tostring((lastMonthmomo - twelveMonthsclosemomo)/twelveMonthsclosemomo *100, format.percent)
table.cell(_t1, 0, 0, indTextM6momo, text_size = size.small)
table.cell(_t1, 0, 1, indTextM12momo, text_size = size.small)

核心逻辑说明

  • 简化重复代码:用数组存储所有板块,循环批量计算数据,后续新增板块只需在sector_symbols里添加代码即可
  • 排序切换:修改array.sort()中的order.descending为order.ascending,就能切换成升序排列(最弱板块在前)
  • 自动排名:利用循环索引i生成1-11的连续排名,自动匹配排序后的板块顺序
  • 保留原有规则:完全沿用了你原来的颜色显示逻辑,确保和之前的视觉效果一致

内容的提问来源于stack exchange,提问作者SeaCon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 14:10:46