金融数据分析| 因子选股策略
因子选股策略
因子选股策略
因子:选择股票的某种标准
增长率、 市值、 市盈率、 ROE(净资产收益率)
选股策略:
对于某个因子,选取表现最好(因子最大或最小)的N支股票持仓
每隔一段时间调仓一次
小市值策略:选取股票池中市值最小的N只股票持仓
def initialize(context): set_benchmark('000300.XSHG') set_option('use_real_price', True) set_order_cost(OrderCost(open_tax=0, close_tax=0.001,open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') g.security = get_index_stocks('000300.XSHG') g.q = query(valuation).filter(valuation.code.in_(g.security)) g.N = 20 run_monthly(handle, 1) def handle(context): df = get_fundamentals(g.q)[['code', 'market_cap']] df = df.sort_values('market_cap').iloc[:g.N,:] #sort 'DataFrame' object has no attribute 'sort' #print(df) to_hold = df['code'].values for stock in context.portfolio.positions: if stock not in to_hold: order_target(stock, 0) to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions] if len(to_buy) > 0: cash_per_stock = context.portfolio.available_cash / len(to_buy) for stock in to_buy: order_value(stock, cash_per_stock)
点击运行回测查看详情:
多因子选股策略
def initialize(context): set_benchmark('000002.XSHG') set_option('use_real_price', True) set_order_cost(OrderCost(open_tax=0, close_tax=0.001,open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') g.security = get_index_stocks('000002.XSHG') g.q = query(valuation, indicator).filter(valuation.code.in_(g.security)) g.N = 20 run_monthly(handle, 1) def handle(context): df = get_fundamentals(g.q)[['code', 'market_cap', 'roe']] #print(df) df['market_cap'] = (df['market_cap'] - df['market_cap'].min()) / (df['market_cap'].max() - df['market_cap'].min()) df['roe'] = (df['roe'] - df['roe'].min()) / (df['roe'].max() - df['roe'].min()) #print(df) df['score'] = df['roe'] - df['market_cap'] df = df.sort_values('score').iloc[-g.N:,:] #sort AttributeError: 'DataFrame' object has no attribute 'sort' to_hold = df['code'].values for stock in context.portfolio.positions: if stock not in to_hold: order_target(stock, 0) to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions] if len(to_buy) > 0: cash_per_stock = context.portfolio.available_cash / len(to_buy) for stock in to_buy: order_value(stock, cash_per_stock)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2019-05-13 Fink| 实时热门商品
2019-05-13 Fink| CEP
2019-05-13 Fink| source| transform| sink
2018-05-13 JavaScript