问答交流

回测引擎:每日数据处理函数,每天执行一次

由bqvyl6pt创建,最终由small_q 被浏览 23 用户

回测引擎:每日数据处理函数,每天执行一次

def m19_handle_data_bigquant_run(context, data):

# 按日期过滤得到今日的预测数据

ranker_prediction = context.ranker_prediction[

context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]

# 1. 资金分配

# 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金

# 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)

is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)

cash_avg = context.portfolio.portfolio_value / context.options['hold_days']

cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)

cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)

positions = {e.symbol: p.amount * p.last_sale_price

for e, p in context.portfolio.positions.items()}

# 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按机器学习算法预测的排序末位淘汰

if not is_staging and cash_for_sell > 0:

equities = {e.symbol: e for e, p in context.portfolio.positions.items()}

instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(

lambda x: x in equities)])))

for instrument in instruments:

context.order_target(context.symbol(instrument), 0)

cash_for_sell -= positions[instrument]

if cash_for_sell <= 0:

break

标签

回测函数
{link}