获得超额收益 年化 49%
2. 从股票中选择市值小的股票- 选定财务数据筛选
- 进行每日调仓(多因子选股调仓 周期频率会小一些)
估值有关指标
# 在沪深300股票中,选出市值比较小的10只股票
# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。
def init(context):
# 沪深300组合
stocks = index_components('000300.XSHG') # 沪深300
context.stocks = stocks
# before_trading此函数会在每天策略交易开始前被调用,当天只会被调用一次
def before_trading(context):
# 获取因子
fund = get_factor(context.stocks, ['market_cap', 'pe_ratio'])
# 过滤
# fund = fund[(fund['pe_ratio'] > 50) & (fund['pe_ratio'] < 65)]
# 排序
fund = fund.sort_values(by='market_cap', ascending=True).head(20)
fund = fund.reset_index(1, drop=True)
# print(fund)
# 获得10只股票的代码
context.stock_list = fund.index.values
update_universe(context.stock_list)
print(context.stock_list)
# 你选择的证券的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新
def handle_bar(context, bar_dict):
# 获取仓位
position_keys = context.portfolio.positions.keys()
if len(position_keys) != 0:
for stock in position_keys:
# 如果旧的股票池,不在新的股票池中
if stock not in context.stock_list:
order_target_percent(stock, 0)
# 买入最新的更新的股票
# 等比率资金买入 投资组合总价值的百分比平分 20份
weight = 1.0 / len(context.stock_list)
for stock in context.stock_list:
order_target_percent(stock, weight)
# after_trading函数会在每天交易结束后被调用,当天只会被调用一次
def after_trading(context):
pass
初始运行信息
回测结果



