Fork me on GitHub

金融数据分析| 因子选股策略

 

  因子选股策略

因子选股策略
因子:选择股票的某种标准
    增长率、 市值、 市盈率、 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)
         
复制代码

 

 

posted @   kris12  阅读(438)  评论(0编辑  收藏  举报
编辑推荐:
· 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
levels of contents
点击右上角即可分享
微信分享提示