问答交流

持仓比例问题

由aimarhu创建,最终由small_q 被浏览 45 用户

{w:100}上图为买入twap1 卖出为twap8时候的持仓比率

{w:100}下图为买入open 卖出close时候的持仓比率 请问这是哪里的问题?

{w:100} {w:100}

交易函数如下

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
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: 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: 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

# 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
buy_cash_weights = context.stock_weights
buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
for i, instrument in enumerate(buy_instruments):
    cash = cash_for_buy * buy_cash_weights[i]
    if cash > max_cash_per_instrument - positions.get(instrument, 0):
        # 确保股票持仓量不会超过每次股票最大的占用资金量
        cash = max_cash_per_instrument - positions.get(instrument, 0)
    if cash > 0:
        context.order_value(context.symbol(instrument), cash)

\

标签

交易策略风险管理投资决策交易成本持仓
{link}