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

如何在Pine Script中为request.financial()获取的财报数据匹配对应季度?

匹配财报数据对应季度的实现方案

完全可以在脚本中为财报数据匹配对应季度,核心是利用Pine Script的时间函数提取每个财务数据点对应的年份与季度信息,生成格式清晰的季度标签后加入表格。以下是具体实现步骤和示例代码:

实现思路

  1. 通过request.financial()获取季度财报数据时,返回的系列数据会与对应季度结束后的bar时间对齐。
  2. 用ta.year()和ta.quarter()函数提取当前bar的年份与季度编号,拼接成YYYY QX格式的季度标签。
  3. 过滤重复的财报数据(仅在有新数据的bar上更新表格),避免表格中出现重复的季度条目。

示例代码

//@version=5
indicator("带季度标签的财报数据表格", overlay=false)

// 获取季度EBITDA数据
ebitda = request.financial(syminfo.tickerid, "EBITDA", "FQ", gaps=barmerge.gaps_off)

// 生成季度标签:格式为「YYYY QX」
get_quarter_label() =>
    year = ta.year(time)
    quarter_num = ta.quarter(time)
    str.tostring(year) + " Q" + str.tostring(quarter_num)

quarter_label = get_quarter_label()

// 判断当前bar是否有新的财报数据(过滤重复值)
is_new_quarter = ta.change(ebitda) != 0

// 初始化表格
var table financial_table = table.new(position.bottom_right, 2, 1, border_width=1)

// 有新季度数据时插入表格行
if is_new_quarter
    table.insert_row(financial_table, 0)
    // 填充季度列
    table.cell(financial_table, 0, 0, quarter_label, text_halign=text.align_center)
    // 填充EBITDA数据列
    table.cell(financial_table, 1, 0, str.tostring(ebitda), text_halign=text.align_center)

// 设置表格表头(仅在最后一根bar执行)
if barstate.islast
    table.cell(financial_table, 0, 0, "季度", text_color=color.white, bg_color=color.blue)
    table.cell(financial_table, 1, 0, "EBITDA", text_color=color.white, bg_color=color.blue)

关键说明

  • 如果标的财季与自然季度不一致(部分公司财年非自然年),ta.quarter()返回的是自然季度,但request.financial()返回的财季数据仍会与对应财季结束后的bar对齐,此时标签仍能准确对应财报所属财季。
  • is_new_quarter的判断逻辑确保表格仅添加新的季度数据,避免重复填充相同内容。

内容的提问来源于stack exchange,提问作者Om Prakash Das

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 23:06:05