基于米筐量化平台的多因子量化选股策略实现
创作时间:
作者:
@小白创作中心
基于米筐量化平台的多因子量化选股策略实现
引用
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)
回测结果
以下是部分模拟交易数据的回测结果:
需要注意的是,这些回测结果仅用于说明代码运行的正确性,不作为投资依据。
热门推荐
手机桌面壁纸设置全攻略:步骤详解与小贴士
诸葛亮七擒孟获:古代军事策略的巅峰之作
七擒孟获:诸葛亮的智慧与仁德
《哪吒2》陈塘关大战视效揭秘:技术与艺术的巅峰融合
秋冬月季修剪大法:轻松爆花不是梦!
秋日园艺:月季修剪法大揭秘!
被狗咬伤后的心理恐惧:从识别到克服
北京朝阳流浪犬咬伤8人后续:伤者已接种疫苗,警方全力搜寻伤人犬只
被狗咬伤后如何正确接种狂犬病疫苗?
左宗棠最大遗憾:白彦虎的军事才能解析
抑郁症患者如何应对手臂肌肉酸痛?
手臂酸痛?警惕这三大元凶!
手臂酸痛?这些按摩手法让你秒变"按摩大师"
120句经典月亮诗句,尽显中国传统文化之美
日月星天珠的象征意义:解读传统文化中的宇宙之美
中国十大白茶:从福鼎到景谷,品味千年茶韵
读懂女生的17种暗示:从眼神到行为的解码指南
表情包社交:用幽默打破沉默的艺术
双十一必囤:平价人参刺五加护心宝典
刺五加种植:一亩地年入10万,这个“绿色黄金”你值得拥有
平价人参刺五加,养生新宠了解一下?
刺五加:从传统中药到现代医学研究新宠
电子社保卡+博时基金:个人理财新趋势
投资管理公司如何应对市场波动?这些策略值得你学习
电子社保卡:爸妈也能玩转高科技!
洛杉矶Art District:从工业废墟到创意天堂
从《芭比》看洛杉矶潮流新趋势
心学问心理教育,成长的阶梯:设定合理期望,助力孩子稳步前行
你看到了什么,就会发生什么,心态决定命运
中风后遗症:中医疗法能有效舒缓吗?