【聚宽 JoinQuant】聚宽如何用基本面数据选股?get_fundamentals()市盈率与市值选股示例
本方案由EasyQuant AI量化助手提供。问题背景基本面选股常见指标包括市盈率、市净率、市值、营业收入和净利润等。聚宽可通过query()构造查询条件再使用get_fundamentals()返回 DataFrame 格式的财务数据。聚宽基本面数据文档基础选股示例下面的代码从沪深 300 成分股中筛选市盈率为正、总市值较小的股票def initialize(context): g.index_code 000300.XSHG g.stock_num 10 run_monthly(select_stocks, 1, time09:35) def select_stocks(context): stock_pool get_index_stocks( g.index_code, datecontext.current_dt ) q query( valuation.code, valuation.market_cap, valuation.pe_ratio, indicator.roe ).filter( valuation.code.in_(stock_pool), valuation.pe_ratio 0, valuation.pe_ratio 50, indicator.roe 5 ).order_by( valuation.market_cap.asc() ).limit( g.stock_num ) df get_fundamentals( q, datecontext.current_dt ) g.target_stocks list(df[code]) log.info(本期选股结果%s % str(g.target_stocks))加入 ST 和停牌过滤def filter_tradeable_stocks(stock_list): current_data get_current_data() result [] for stock in stock_list: if current_data[stock].paused: continue if current_data[stock].is_st: continue if 退 in current_data[stock].name: continue result.append(stock) return result在查询前过滤股票池stock_pool get_index_stocks( 000300.XSHG, datecontext.current_dt ) stock_pool filter_tradeable_stocks(stock_pool)注意事项get_fundamentals()返回的是 DataFrame可通过字段名继续筛选、排序和计算。查询多只股票时应使用valuation.code.in_(stock_list)而不是 Python 的in。财务数据有披露时点回测时要注意避免使用当前时点不可获得的数据。聚宽对未来数据接口进行了限制策略应使用当前回测日期或更早日期查询数据。聚宽避免未来数据说明基本面指标不代表收益仍需结合行业、估值、趋势和风险控制进行回测。总结聚宽基本面选股的核心流程是“股票池—财务查询—条件过滤—排序—调仓”。get_fundamentals()适合构建市值、估值、盈利能力等多因子选股策略。