基于米筐量化平台的多因子量化选股策略实现
创作时间:
作者:
@小白创作中心
基于米筐量化平台的多因子量化选股策略实现
引用
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)
回测结果
以下是部分模拟交易数据的回测结果:
需要注意的是,这些回测结果仅用于说明代码运行的正确性,不作为投资依据。
热门推荐
游泳对腰椎间盘突出有帮助吗
桃花运的提升与调整:如何优化爱情运势?
春节拜年短信,用藏头诗惊艳朋友圈!
海蜇的功效与作用 海蜇的药用价值
经典威海菜:酸爽开胃、清凉解暑的鲜海蜇汤
春节职场礼仪:掌握得体拜年短信技巧
AI助你春节拜年,秒变社交达人!
车辆悬挂系统维护,防患于未然!
桥梁施工黑科技:告别伸缩缝跳车烦恼
冬季行车必看:桥梁安全驾驶技巧
《和平精英》悦享卡省钱攻略大揭秘!
NBA球星教你如何避免篮球伤病
勒布朗·詹姆斯的篮球热身秘籍大公开!
从NBA伤情潮看职业球员如何科学预防运动伤害
新手收藏者必看:银手链鉴真秘籍
碧玺手链:从制作到佩戴,一文读懂这颗时尚新宠
双十一防坑指南:如何辨别银手镯真假?
手链+时尚服饰=完美搭配!
教你四招辨别银手链真伪,轻松避开假货陷阱
真实的后宫生活:每日请安禁睡懒觉,饮食服饰严格规定
秋冬养生必备:清蒸鲈鱼+豆腐&胡萝卜
如何快速走出负面情绪的小妙招(教你战胜负面情绪)
水晶手串市场乱象:如何避免被坑?
紫檀手串鉴别大揭秘:火眼金睛防踩坑!
吴彦祖与甄子丹:谁更适合演绎孙悟空?
北京遗产继承律师教你如何选合格遗嘱见证人
西京医院消化科:特色诊疗服务揭秘
西京医院樊代明院士团队破解胃癌耐药难题,发现牛磺酸关键作用
游戏基础知识——从“增辉龙”和“巴德”看玩家游戏技巧的三个阶段
椒盐富贵虾的制作方法和配料