3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在量化选股策略中,企业的营运能力是衡量其管理效率与资金周转能力的核心维度。存货周转率和总资产周转率越高,说明企业资产利用效率越高,存货积压风险越低,通常是优质行业龙头的显著特征。
在 ptrade 量化交易平台中,我们可以通过调用 get_fundamentals 接口快速获取上市公司的营运能力财务指标并进行筛选。
get_fundamentals获取营运能力数据时,需要将 table 参数指定为 'operating_ability'(营运能力表)。
inventory_turnover_rate: 存货周转率(次)total_asset_turnover_rate: 总资产周转率(次)accounts_receivables_turnover_rate: 应收账款周转率(次)注意:
get_fundamentals接口支持按日期查询或按年份查询。为避免未来函数,建议在策略盘前阶段(before_trading_start)查询最新已发布的财报数据。
inventory_turnover_rate) > 行业均值或指定门槛(如 > 3 次)total_asset_turnover_rate) > 指定门槛(如 > 0.5 次)import pandas as pd
def initialize(context):
# 设置参考基准
set_benchmark('000300.SS')
# 每天盘前 09:15 执行选股逻辑
run_daily(context, select_stocks, time='09:15')
def select_stocks(context):
# 1. 获取全 A 股列表
all_stocks = get_Ashares()
# 2. 过滤 ST、停牌、退市股票
valid_stocks = filter_stock_by_status(all_stocks, filter_type=["ST", "HALT", "DELISTING"])
# 3. 获取营运能力数据 (指定表名 operating_ability)
# 查询字段:存货周转率、总资产周转率
fields = ['inventory_turnover_rate', 'total_asset_turnover_rate']
# 获取财务数据(以当前日期之前最近一份已发布财报为准)
df_fundamentals = get_fundamentals(
security=valid_stocks,
table='operating_ability',
fields=fields
)
if df_fundamentals is None or df_fundamentals.empty:
log.info("未获取到财务数据")
return
# 4. 筛选条件:存货周转率 > 3 次 且 总资产周转率 > 0.4 次
filtered_df = df_fundamentals[
(df_fundamentals['inventory_turnover_rate'] > 3.0) &
(df_fundamentals['total_asset_turnover_rate'] > 0.4)
]
# 5. 按存货周转率降序排序,取前 10 只龙头股票
target_df = filtered_df.sort_values(by='inventory_turnover_rate', ascending=False).head(10)
target_stocks = list(target_df.index)
log.info("筛选出的优质营运龙头股票列表: %s" % target_stocks)
# 更新策略操作股票池
set_universe(target_stocks)
g.target_stocks = target_stocks
def handle_data(context, data):
# 盘中交易逻辑(示例:简单调仓)
if not hasattr(g, 'target_stocks') or not g.target_stocks:
return
# 卖出不在目标池中的股票
for stock in context.portfolio.positions:
if stock not in g.target_stocks:
order_target(stock, 0)
# 等权重买入目标池股票
cash = context.portfolio.cash
if len(g.target_stocks) > 0 and cash > 0:
value_per_stock = cash / len(g.target_stocks)
for stock in g.target_stocks:
if get_position(stock).amount == 0:
order_value(stock, value_per_stock)
get_fundamentals 已在后台做分批处理,但建议在非高频盘中时段(如盘前 before_trading_start 或 run_daily 09:15)执行。get_industry_stocks 获取同行业股票后,在行业内部进行百分位或均值对比选股。date 参数时,默认使用当前回测日期的历史已发布数据,能有效规避未来函数风险。