问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

基于米筐量化平台的多因子量化选股策略实现

创作时间:
作者:
@小白创作中心

基于米筐量化平台的多因子量化选股策略实现

引用
CSDN
1.
https://m.blog.csdn.net/akansase/article/details/145066431

量化投资领域中,多因子选股策略是一种重要的投资策略。本文将详细介绍如何基于米筐量化平台实现多因子量化选股策略。文章将从米筐策略平台代码和本地化部署代码两个方面进行讲解,并提供完整的代码示例。

米筐策略平台代码

在米筐策略平台上实现多因子量化选股策略,需要编写一系列函数来完成初始化、选股、调仓等操作。以下是具体的代码实现:

import rqdatac 
import numpy as np
import pandas as pd
from pandas import DataFrame as df
from rqfactor.utils import rolling_window
from rqfactor.rolling import RollingWindowFactor
from rqdatac import get_factor
from datetime import datetime
from rqdatac import get_fundamentals

# 初始化函数
def init(context):
    context.stocks = index_components('000300.XSHG')
    scheduler.run_monthly(rebalance,1)

# 月度调仓函数
def rebalance(context,bar_dict):
    stocks = set(get_stocks(context))
    holdings = set(get_holdings(context))
    to_buy = stocks - holdings
    to_sell = holdings - stocks
    for stock in to_sell:
        order_target_percent(stock,0)
    if len(to_buy) == 0:
        return
    average_value = context.portfolio.portfolio_value/len(to_buy)
    for stock in to_buy:
        order_target_value(stock,average_value)

# 获取持仓股票函数
def get_holdings(context):   
    positions = context.portfolio.positions
    holdings = []
    for position in positions:
        if positions[position].quantity > 0:
           holdings.append(position)
    return holdings

# 获取待选股票函数
def get_stocks(context):
    # 获取净资产收益率
    df_return_on_equity = get_factor(context.stocks, 'return_on_equity')
    df_return_on_equity = df_return_on_equity.groupby(level = 'order_book_id').sum()
    # 获取每股收益EPS
    df_earnings_per_share = get_factor(context.stocks, 'basic_earnings_per_share')
    df_earnings_per_share = df_earnings_per_share.groupby(level = 'order_book_id').sum()
    # 获取净资产回报率
    df_return_on_invested_capital = get_factor(context.stocks, 'return_on_invested_capital')
    df_return_on_invested_capital = df_return_on_invested_capital.groupby(level = 'order_book_id').sum()
    # 组合正向因子数据
    df_fundamental_df_up = df_return_on_equity
    df_fundamental_df_up['basic_earnings_per_share'] = df_earnings_per_share['basic_earnings_per_share']
    df_fundamental_df_up['return_on_invested_capital'] = df_return_on_invested_capital['return_on_invested_capital']
    df_fundamental_df_up['up_level_fator'] = df_fundamental_df_up.sum(axis=1)
    df_fundamental_df_up.sort_values(by = 'up_level_fator',inplace = True) 
    df_fundamental_df_up['up_level_fator'] = np.linspace(1,len(df_fundamental_df_up),len(df_fundamental_df_up))
    df_fundamental_df_up =  df_fundamental_df_up['up_level_fator'].to_frame()
    
    # 获取资产负债率
    df_debt_to_asset_ratio = get_factor(context.stocks, 'debt_to_asset_ratio')
    df_debt_to_asset_ratio = df_debt_to_asset_ratio.groupby(level = 'order_book_id').sum()
    # 获取市净率
    df_pb_ratio = get_factor(context.stocks, 'pb_ratio')
    df_pb_ratio = df_pb_ratio.groupby(level = 'order_book_id').sum()
    # 获取市值
    df_market_cap = get_factor(context.stocks, 'market_cap')
    df_market_cap = df_market_cap.groupby(level = 'order_book_id').sum()
    # 组合反向因子数据
    df_fundamental_df_down = df_debt_to_asset_ratio
    df_fundamental_df_down['pb_ratio'] = df_pb_ratio['pb_ratio']
    df_fundamental_df_down['market_cap'] = df_market_cap['market_cap']
    df_fundamental_df_down['down_level_fator'] = df_fundamental_df_down.sum(axis=1)
    df_fundamental_df_down.sort_values(by = 'down_level_fator',inplace = True) 
    df_fundamental_df_down['down_level_fator'] = np.linspace(len(df_fundamental_df_down),1,len(df_fundamental_df_down))
    df_fundamental_df_down =  df_fundamental_df_down['down_level_fator'].to_frame()
    
    # 组合所有因子数据
    df_fundamental_df_join = pd.merge(df_fundamental_df_up, df_fundamental_df_down, left_index=True, right_index=True, how='inner')
    df_fundamental_df_join['sum_factor']=df_fundamental_df_join.sum(axis=1)
    df_fundamental_df_join.sort_values(by = 'sum_factor',inplace = True) 
    df_fundamental_df_join =  df_fundamental_df_join['sum_factor'].to_frame()
    
    good_stocks = df_fundamental_df_join.tail(10).index.tolist()
    return good_stocks

# 其他函数略

本地化部署代码

如果使用PyCharm进行本地化部署,可以参考以下代码示例:

import rqdatac 
import numpy as np
import pandas as pd
from pandas import DataFrame as df
from rqfactor.utils import rolling_window
from rqfactor.rolling import RollingWindowFactor
from rqdatac import get_factor
from datetime import datetime
from rqdatac import get_fundamentals

stocks = index_components('000300.XSHG')

# 获取正向因子数据
df_return_on_equity = get_factor(stocks, 'return_on_equity', '20240801', '20241231')
df_return_on_equity = df_return_on_equity.groupby(level = 'order_book_id').sum()
df_earnings_per_share = get_factor(stocks, 'basic_earnings_per_share', '20240801', '20241231')
df_earnings_per_share = df_earnings_per_share.groupby(level = 'order_book_id').sum()
df_return_on_invested_capital = get_factor(stocks, 'return_on_invested_capital', '20240801', '20241231')
df_return_on_invested_capital = df_return_on_invested_capital.groupby(level = 'order_book_id').sum()

# 组合正向因子数据
df_fundamental_df_up = df_return_on_equity
df_fundamental_df_up['basic_earnings_per_share'] = df_earnings_per_share['basic_earnings_per_share']
df_fundamental_df_up['return_on_invested_capital'] = df_return_on_invested_capital['return_on_invested_capital']
df_fundamental_df_up['up_level_fator'] = df_fundamental_df_up.sum(axis=1)
df_fundamental_df_up.sort_values(by = 'up_level_fator',inplace = True) 
df_fundamental_df_up['up_level_fator'] = np.linspace(1,len(df_fundamental_df_up),len(df_fundamental_df_up))
df_fundamental_df_up =  df_fundamental_df_up['up_level_fator'].to_frame()

# 获取反向因子数据
df_debt_to_asset_ratio = get_factor(stocks, 'debt_to_asset_ratio', '20240801', '20241231')
df_debt_to_asset_ratio = df_debt_to_asset_ratio.groupby(level = 'order_book_id').sum()
df_pb_ratio = get_factor(stocks, 'pb_ratio', '20240801', '20241231')
df_pb_ratio = df_pb_ratio.groupby(level = 'order_book_id').sum()
df_market_cap = get_factor(stocks, 'market_cap', '20240801', '20241231')
df_market_cap = df_market_cap.groupby(level = 'order_book_id').sum()

# 组合反向因子数据
df_fundamental_df_down = df_debt_to_asset_ratio
df_fundamental_df_down['pb_ratio'] = df_pb_ratio['pb_ratio']
df_fundamental_df_down['market_cap'] = df_market_cap['market_cap']
df_fundamental_df_down['down_level_fator'] = df_fundamental_df_down.sum(axis=1)
df_fundamental_df_down.sort_values(by = 'down_level_fator',inplace = True) 
df_fundamental_df_down['down_level_fator'] = np.linspace(len(df_fundamental_df_down),1,len(df_fundamental_df_down))
df_fundamental_df_down =  df_fundamental_df_down['down_level_fator'].to_frame()

# 组合所有因子数据
df_fundamental_df_join = pd.merge(df_fundamental_df_up, df_fundamental_df_down, left_index=True, right_index=True, how='inner')
df_fundamental_df_join['sum_factor']=df_fundamental_df_join.sum(axis=1)
df_fundamental_df_join.sort_values(by = 'sum_factor',inplace = True) 
df_fundamental_df_join =  df_fundamental_df_join['sum_factor'].to_frame()

# 获取最终选股结果
good_stocks = df_fundamental_df_join.tail(10).index.tolist()
print(good_stocks)

回测结果

以下是部分模拟交易数据的回测结果:

需要注意的是,这些回测结果仅用于说明代码运行的正确性,不作为投资依据。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号