|
|
@@ -204,7 +204,7 @@ def initialize(context):
|
|
|
|
|
|
# 策略品种选择策略配置
|
|
|
# 方案1:全品种策略 - 考虑所有配置的期货品种
|
|
|
- g.strategy_focus_symbols = [] # 空列表表示考虑所有品种
|
|
|
+ g.strategy_focus_symbols = ['AU'] # 空列表表示考虑所有品种
|
|
|
|
|
|
# 方案2:精选品种策略 - 只交易流动性较好的特定品种(如需使用请取消下行注释)
|
|
|
# g.strategy_focus_symbols = ['RM', 'CJ', 'CY', 'JD', 'L', 'LC', 'SF', 'SI']
|
|
|
@@ -1016,6 +1016,7 @@ def check_ma5_distribution_filter(data, lookback_days, direction, min_ratio):
|
|
|
ma5_series = data['close'].rolling(window=5).mean().iloc[-lookback_days:]
|
|
|
|
|
|
for close_price, ma5_value in zip(recent_closes, ma5_series):
|
|
|
+ log.debug(f"close_price: {close_price}, ma5_value: {ma5_value}")
|
|
|
if pd.isna(ma5_value):
|
|
|
continue
|
|
|
stats['valid_days'] += 1
|
|
|
@@ -1462,8 +1463,32 @@ def position_auto_switch(context, pindex=0, switch_func=None, callback=None):
|
|
|
switch_result.append({"before": symbol, "after": dominant, "side": "long"})
|
|
|
# 换月成功,更新交易记录
|
|
|
if symbol in g.trade_history:
|
|
|
- g.trade_history[dominant] = g.trade_history[symbol]
|
|
|
+ # 复制旧的交易记录作为基础
|
|
|
+ old_entry_price = g.trade_history[symbol]['entry_price']
|
|
|
+ g.trade_history[dominant] = g.trade_history[symbol].copy()
|
|
|
+
|
|
|
+ # 更新成本价为新合约的实际开仓价
|
|
|
+ new_entry_price = None
|
|
|
+ if order_new.avg_cost and order_new.avg_cost > 0:
|
|
|
+ new_entry_price = order_new.avg_cost
|
|
|
+ g.trade_history[dominant]['entry_price'] = order_new.avg_cost
|
|
|
+ elif order_new.price and order_new.price > 0:
|
|
|
+ new_entry_price = order_new.price
|
|
|
+ g.trade_history[dominant]['entry_price'] = order_new.price
|
|
|
+ else:
|
|
|
+ # 如果订单价格无效,使用当前价格作为成本价
|
|
|
+ new_entry_price = dominant_last_price
|
|
|
+ g.trade_history[dominant]['entry_price'] = dominant_last_price
|
|
|
+
|
|
|
+ # 更新入场时间
|
|
|
+ g.trade_history[dominant]['entry_time'] = context.current_dt
|
|
|
+ # 更新入场交易日
|
|
|
+ g.trade_history[dominant]['entry_trading_day'] = get_current_trading_day(context.current_dt)
|
|
|
+ # 删除旧合约的交易记录
|
|
|
del g.trade_history[symbol]
|
|
|
+
|
|
|
+ log.info(f"移仓换月成本价更新: {symbol} -> {dominant}, "
|
|
|
+ f"旧成本价: {old_entry_price:.2f}, 新成本价: {new_entry_price:.2f}")
|
|
|
else:
|
|
|
log.warning("标的{}交易失败,无法开仓。移仓换月失败。".format(dominant))
|
|
|
if p.side == "short":
|
|
|
@@ -1484,8 +1509,32 @@ def position_auto_switch(context, pindex=0, switch_func=None, callback=None):
|
|
|
switch_result.append({"before": symbol, "after": dominant, "side": "short"})
|
|
|
# 换月成功,更新交易记录
|
|
|
if symbol in g.trade_history:
|
|
|
- g.trade_history[dominant] = g.trade_history[symbol]
|
|
|
+ # 复制旧的交易记录作为基础
|
|
|
+ old_entry_price = g.trade_history[symbol]['entry_price']
|
|
|
+ g.trade_history[dominant] = g.trade_history[symbol].copy()
|
|
|
+
|
|
|
+ # 更新成本价为新合约的实际开仓价
|
|
|
+ new_entry_price = None
|
|
|
+ if order_new.avg_cost and order_new.avg_cost > 0:
|
|
|
+ new_entry_price = order_new.avg_cost
|
|
|
+ g.trade_history[dominant]['entry_price'] = order_new.avg_cost
|
|
|
+ elif order_new.price and order_new.price > 0:
|
|
|
+ new_entry_price = order_new.price
|
|
|
+ g.trade_history[dominant]['entry_price'] = order_new.price
|
|
|
+ else:
|
|
|
+ # 如果订单价格无效,使用当前价格作为成本价
|
|
|
+ new_entry_price = dominant_last_price
|
|
|
+ g.trade_history[dominant]['entry_price'] = dominant_last_price
|
|
|
+
|
|
|
+ # 更新入场时间
|
|
|
+ g.trade_history[dominant]['entry_time'] = context.current_dt
|
|
|
+ # 更新入场交易日
|
|
|
+ g.trade_history[dominant]['entry_trading_day'] = get_current_trading_day(context.current_dt)
|
|
|
+ # 删除旧合约的交易记录
|
|
|
del g.trade_history[symbol]
|
|
|
+
|
|
|
+ log.info(f"移仓换月成本价更新: {symbol} -> {dominant}, "
|
|
|
+ f"旧成本价: {old_entry_price:.2f}, 新成本价: {new_entry_price:.2f}")
|
|
|
else:
|
|
|
log.warning("标的{}交易失败,无法开仓。移仓换月失败。".format(dominant))
|
|
|
if callback:
|