🚀 AI 一键生成 ptrade 策略代码
立即体验

ptrade 财务选股:如何利用现金流量表筛选高自由现金流股票?(附 Python 源码)

ptrade | 发布时间: 2026/7/26
以下内容由 EasyQuant 生成。

问题描述

请根据以下 SEO 主题生成完整页面内容:

Title: ptrade 财务选股:利用现金流量表筛选高自由现金流股票

Question: 如何使用 ptrade 的 get_fundamentals 接口获取现金流量表数据并筛选自由现金流充沛的股票?

解决方案

ptrade 财务选股:利用现金流量表筛选高自由现金流股票

自由现金流(Free Cash Flow, FCF)是衡量企业真实盈利能力与财务健康度的核心指标。在 ptrade 量化交易平台中,我们可以通过调用 get_fundamentals 接口查询现金流量表(cashflow_statement)等财务数据,精准筛选出自由现金流充沛的优质标的。


一、ptrade 财务数据接口介绍

在 ptrade 中,获取财务数据的核心函数为 get_fundamentals。通过该接口,可以轻松调取资产负债表、利润表、现金流量表以及各种财务比率指标。

常用接口语法:

get_fundamentals(security, table, fields=None, date=None, start_year=None, end_year=None, report_types=None, date_type=None, merge_type=None)

获取现金流量表数据说明:

  • table 参数:传入 'cashflow_statement'
  • 核心字段
    • net_operate_cash_flow:经营活动产生的现金流量净额
    • fix_intan_other_asset_acqui_cash:购建固定资产、无形资产和其他长期资产支付的现金(即资本支出 Capital Expenditure, CapEx)

自由现金流简易计算公式
$$\text{自由现金流 (FCF)} = \text{经营活动现金流量净额 (net_operate_cash_flow)} - \text{资本支出 (fix_intan_other_asset_acqui_cash)}$$


二、核心代码实现:获取与计算自由现金流

下面的函数演示了如何提取全市场(或指定股票池)的现金流量表数据,并筛选出自由现金流大于 0 且排名前列的股票:

import pandas as pd
import numpy as np

def get_high_fcf_stocks(stocks, query_date):
    """
    筛选高自由现金流股票
    """
    fields = ['net_operate_cash_flow', 'fix_intan_other_asset_acqui_cash']
    
    # 调取现金流量表数据
    df_cash = get_fundamentals(
        security=stocks,
        table='cashflow_statement',
        fields=fields,
        date=query_date,
        report_types='4' # 获取最新年度财报,也可不传获取最近一期
    )
    
    if df_cash is None or df_cash.empty:
        return []
    
    # 替换缺失值
    df_cash = df_cash.fillna(0)
    
    # 计算自由现金流 (FCF = 经营现金流净额 - 购建固定资产/无形资产等支付的现金)
    df_cash['fcf'] = df_cash['net_operate_cash_flow'] - df_cash['fix_intan_other_asset_acqui_cash']
    
    # 过滤自由现金流大于0的股票
    valid_df = df_cash[df_cash['fcf'] > 0]
    
    # 按 FCF 降序排序,取前20只
    top_fcf_stocks = valid_df.sort_values(by='fcf', ascending=False).index.tolist()
    
    return top_fcf_stocks[:20]

三、完整的 ptrade 选股策略源码

以下是一个完整的 ptrade 定期调仓策略示例,每周一开盘前根据上末最新财务数据重新筛选高自由现金流股票并进行建仓调仓。

import time
import datetime

def initialize(context):
    # 设置参考基准为沪深300
    set_benchmark('000300.SS')
    # 设置调仓周期:每周一运行
    run_daily(context, rebalance, time='09:31')
    g.max_hold_count = 10  # 持仓目标数量

def rebalance(context):
    # 仅每周一执行调仓
    current_dt = context.blotter.current_dt
    if current_dt.weekday() != 0:
        return
        
    log.info("开始执行高自由现金流选股调仓...")
    
    # 1. 获取全A股股票池
    trade_date = current_dt.strftime('%Y%m%d')
    all_stocks = get_Ashares(date=trade_date)
    
    # 2. 过滤剔除 ST、停牌、退市股票
    valid_stocks = filter_stock_by_status(all_stocks, filter_type=["ST", "HALT", "DELISTING"])
    
    # 3. 提取现金流量表并计算 FCF
    fields = ['net_operate_cash_flow', 'fix_intan_other_asset_acqui_cash']
    df_cash = get_fundamentals(
        security=valid_stocks,
        table='cashflow_statement',
        fields=fields,
        date=trade_date
    )
    
    if df_cash is None or df_cash.empty:
        log.warning("未能获取到财务数据")
        return
        
    df_cash = df_cash.fillna(0)
    df_cash['fcf'] = df_cash['net_operate_cash_flow'] - df_cash['fix_intan_other_asset_acqui_cash']
    
    # 按自由现金流从大到小排序,选取 Top N
    selected_stocks = df_cash[df_cash['fcf'] > 0].sort_values(by='fcf', ascending=False).index.tolist()[:g.max_hold_count]
    log.info("筛选出的高 FCF 股票列表: %s" % selected_stocks)
    
    # 4. 执行调仓:卖出不在目标池中的股票
    positions = context.portfolio.positions
    for stock in list(positions.keys()):
        if stock not in selected_stocks:
            order_target(stock, 0)
            log.info("卖出股票: %s" % stock)
            
    # 5. 执行调仓:买入目标池中的股票(等权重分配)
    if len(selected_stocks) > 0:
        target_value = context.portfolio.portfolio_value / len(selected_stocks)
        for stock in selected_stocks:
            order_target_value(stock, target_value)
            log.info("买入/调整股票: %s, 目标价值: %.2f" % (stock, target_value))

def handle_data(context, data):
    pass

四、注意事项与优化建议

  1. 流控限制get_fundamentals 接口属于 HTTP 在线获取,平台有流量限制(每秒不建议超过100次调用,单次最大返回量为5万条单元格数据)。如需对全市场股票批量查询,建议一次性传入股票列表,避免在循环中逐个查询。
  2. 相对估值结合:仅看自由现金流绝对值可能会偏向大市值公司,实际应用中可以结合 valuation 表中的市值字段,计算 自由现金流收益率(FCF / 总市值),以选出估值性价比更高的标的。
  3. 避免未来函数:在回测场景下,date 参数默认不传会取当前回测日日期。建议明确传入上一交易日或指定公报发布时间,确保回测符合实际交易场景。