|
@@ -107,7 +107,7 @@ def initialize(context):
|
|
|
'PB': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 5, 'trading_start_time': '21:00'},
|
|
'PB': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 5, 'trading_start_time': '21:00'},
|
|
|
'NI': {'has_night_session': True, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 1, 'trading_start_time': '21:00'},
|
|
'NI': {'has_night_session': True, 'margin_rate': {'long': 0.16, 'short': 0.16}, 'multiplier': 1, 'trading_start_time': '21:00'},
|
|
|
'SN': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1, 'trading_start_time': '21:00'},
|
|
'SN': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1, 'trading_start_time': '21:00'},
|
|
|
- 'SS': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 5, 'trading_start_time': '21:00'},
|
|
|
|
|
|
|
+ 'SS': {'has_night_session': True, 'margin_rate': {'long': 0.07, 'short': 0.07}, 'multiplier': 5, 'trading_start_time': '21:00'},
|
|
|
|
|
|
|
|
# 黑色系
|
|
# 黑色系
|
|
|
'RB': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
|
|
'RB': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '21:00'},
|
|
@@ -171,26 +171,26 @@ def initialize(context):
|
|
|
'MO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 100, 'trading_start_time': '21:00'},
|
|
'MO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 100, 'trading_start_time': '21:00'},
|
|
|
# 'LF': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1, 'trading_start_time': '09:30'},
|
|
# 'LF': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 1, 'trading_start_time': '09:30'},
|
|
|
'HO': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 100, 'trading_start_time': '09:30'},
|
|
'HO': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 100, 'trading_start_time': '09:30'},
|
|
|
- 'LR': {'has_night_session': True, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '21:00'},
|
|
|
|
|
|
|
+ # 'LR': {'has_night_session': True, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '21:00'},
|
|
|
'LG': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 90, 'trading_start_time': '21:00'},
|
|
'LG': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 90, 'trading_start_time': '21:00'},
|
|
|
# 'FB': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 10, 'trading_start_time': '21:00'},
|
|
# 'FB': {'has_night_session': True, 'margin_rate': {'long': 0.14, 'short': 0.14}, 'multiplier': 10, 'trading_start_time': '21:00'},
|
|
|
- 'PM': {'has_night_session': True, 'margin_rate': {'long': 0.2, 'short': 0.2}, 'multiplier': 50, 'trading_start_time': '21:00'},
|
|
|
|
|
|
|
+ # 'PM': {'has_night_session': True, 'margin_rate': {'long': 0.2, 'short': 0.2}, 'multiplier': 50, 'trading_start_time': '21:00'},
|
|
|
'EC': {'has_night_session': False, 'margin_rate': {'long': 0.23, 'short': 0.23}, 'multiplier': 50, 'trading_start_time': '09:00'},
|
|
'EC': {'has_night_session': False, 'margin_rate': {'long': 0.23, 'short': 0.23}, 'multiplier': 50, 'trading_start_time': '09:00'},
|
|
|
# 'RR': {'has_night_session': True, 'margin_rate': {'long': 0.11, 'short': 0.11}, 'multiplier': 10, 'trading_start_time': '21:00'},
|
|
# 'RR': {'has_night_session': True, 'margin_rate': {'long': 0.11, 'short': 0.11}, 'multiplier': 10, 'trading_start_time': '21:00'},
|
|
|
'OP': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 40, 'trading_start_time': '09:00'},
|
|
'OP': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 40, 'trading_start_time': '09:00'},
|
|
|
# 'IO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1, 'trading_start_time': '21:00'},
|
|
# 'IO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 1, 'trading_start_time': '21:00'},
|
|
|
'BC': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
|
|
'BC': {'has_night_session': True, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '21:00'},
|
|
|
- 'WH': {'has_night_session': False, 'margin_rate': {'long': 0.2, 'short': 0.2}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
|
|
|
|
+ # 'WH': {'has_night_session': False, 'margin_rate': {'long': 0.2, 'short': 0.2}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
'SH': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 30, 'trading_start_time': '21:00'},
|
|
'SH': {'has_night_session': True, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 30, 'trading_start_time': '21:00'},
|
|
|
- 'RI': {'has_night_session': False, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
|
|
|
|
+ # 'RI': {'has_night_session': False, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
'TS': {'has_night_session': False, 'margin_rate': {'long': 0.015, 'short': 0.015}, 'multiplier': 2000000, 'trading_start_time': '09:30'},
|
|
'TS': {'has_night_session': False, 'margin_rate': {'long': 0.015, 'short': 0.015}, 'multiplier': 2000000, 'trading_start_time': '09:30'},
|
|
|
- 'JR': {'has_night_session': False, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
|
|
|
|
+ # 'JR': {'has_night_session': False, 'margin_rate': {'long': 0.21, 'short': 0.21}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
'AD': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '09:00'},
|
|
'AD': {'has_night_session': False, 'margin_rate': {'long': 0.13, 'short': 0.13}, 'multiplier': 10, 'trading_start_time': '09:00'},
|
|
|
# 'BB': {'has_night_session': False, 'margin_rate': {'long': 0.19, 'short': 0.19}, 'multiplier': 500, 'trading_start_time': '09:00'},
|
|
# 'BB': {'has_night_session': False, 'margin_rate': {'long': 0.19, 'short': 0.19}, 'multiplier': 500, 'trading_start_time': '09:00'},
|
|
|
'PL': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
'PL': {'has_night_session': False, 'margin_rate': {'long': 0.12, 'short': 0.12}, 'multiplier': 20, 'trading_start_time': '09:00'},
|
|
|
# 'RS': {'has_night_session': False, 'margin_rate': {'long': 0.26, 'short': 0.26}, 'multiplier': 10, 'trading_start_time': '09:00'},
|
|
# 'RS': {'has_night_session': False, 'margin_rate': {'long': 0.26, 'short': 0.26}, 'multiplier': 10, 'trading_start_time': '09:00'},
|
|
|
'SI': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '09:00'},
|
|
'SI': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '09:00'},
|
|
|
- 'ZC': {'has_night_session': True, 'margin_rate': {'long': 0.56, 'short': 0.56}, 'multiplier': 100, 'trading_start_time': '21:00'},
|
|
|
|
|
|
|
+ # 'ZC': {'has_night_session': True, 'margin_rate': {'long': 0.56, 'short': 0.56}, 'multiplier': 100, 'trading_start_time': '21:00'},
|
|
|
'SM': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '09:00'},
|
|
'SM': {'has_night_session': False, 'margin_rate': {'long': 0.15, 'short': 0.15}, 'multiplier': 5, 'trading_start_time': '09:00'},
|
|
|
'AO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 20, 'trading_start_time': '21:00'},
|
|
'AO': {'has_night_session': True, 'margin_rate': {'long': 0.17, 'short': 0.17}, 'multiplier': 20, 'trading_start_time': '21:00'},
|
|
|
'TL': {'has_night_session': False, 'margin_rate': {'long': 0.045, 'short': 0.045}, 'multiplier': 1000000, 'trading_start_time': '09:00'},
|
|
'TL': {'has_night_session': False, 'margin_rate': {'long': 0.045, 'short': 0.045}, 'multiplier': 1000000, 'trading_start_time': '09:00'},
|
|
@@ -204,7 +204,7 @@ def initialize(context):
|
|
|
|
|
|
|
|
# 策略品种选择策略配置
|
|
# 策略品种选择策略配置
|
|
|
# 方案1:全品种策略 - 考虑所有配置的期货品种
|
|
# 方案1:全品种策略 - 考虑所有配置的期货品种
|
|
|
- g.strategy_focus_symbols = [] # 空列表表示考虑所有品种
|
|
|
|
|
|
|
+ g.strategy_focus_symbols = ['SS'] # 空列表表示考虑所有品种
|
|
|
|
|
|
|
|
# 方案2:精选品种策略 - 只交易流动性较好的特定品种(如需使用请取消下行注释)
|
|
# 方案2:精选品种策略 - 只交易流动性较好的特定品种(如需使用请取消下行注释)
|
|
|
# g.strategy_focus_symbols = ['RM', 'CJ', 'CY', 'JD', 'L', 'LC', 'SF', 'SI']
|
|
# g.strategy_focus_symbols = ['RM', 'CJ', 'CY', 'JD', 'L', 'LC', 'SF', 'SI']
|
|
@@ -651,10 +651,10 @@ def check_open_and_stop(context):
|
|
|
continue
|
|
continue
|
|
|
# 执行开仓
|
|
# 执行开仓
|
|
|
log.info(f" 准备开仓: {symbol} {direction}")
|
|
log.info(f" 准备开仓: {symbol} {direction}")
|
|
|
- target_hands = calculate_target_hands(context, dominant_future, direction)
|
|
|
|
|
|
|
+ target_hands, single_hand_margin = calculate_target_hands(context, dominant_future, direction)
|
|
|
|
|
|
|
|
if target_hands > 0:
|
|
if target_hands > 0:
|
|
|
- success = open_position(context, dominant_future, target_hands, direction,
|
|
|
|
|
|
|
+ success = open_position(context, dominant_future, target_hands, direction, single_hand_margin,
|
|
|
f'均线形态开仓')
|
|
f'均线形态开仓')
|
|
|
if success:
|
|
if success:
|
|
|
log.info(f" ✓✓ {symbol} 开仓成功,从候选列表移除")
|
|
log.info(f" ✓✓ {symbol} 开仓成功,从候选列表移除")
|
|
@@ -1095,7 +1095,7 @@ def check_historical_ma_pattern_consistency(historical_data, direction, lookback
|
|
|
|
|
|
|
|
############################ 交易执行函数 ###################################
|
|
############################ 交易执行函数 ###################################
|
|
|
|
|
|
|
|
-def open_position(context, security, target_hands, direction, reason=''):
|
|
|
|
|
|
|
+def open_position(context, security, target_hands, direction, single_hand_margin, reason=''):
|
|
|
"""开仓"""
|
|
"""开仓"""
|
|
|
try:
|
|
try:
|
|
|
# 记录交易前的可用资金
|
|
# 记录交易前的可用资金
|
|
@@ -1110,6 +1110,9 @@ def open_position(context, security, target_hands, direction, reason=''):
|
|
|
|
|
|
|
|
# 计算实际资金变化
|
|
# 计算实际资金变化
|
|
|
cash_change = cash_before - cash_after
|
|
cash_change = cash_before - cash_after
|
|
|
|
|
+
|
|
|
|
|
+ # 计算保证金变化
|
|
|
|
|
+ margin_change = single_hand_margin * target_hands
|
|
|
|
|
|
|
|
# 获取订单价格和数量
|
|
# 获取订单价格和数量
|
|
|
order_price = order.avg_cost if order.avg_cost else order.price
|
|
order_price = order.avg_cost if order.avg_cost else order.price
|
|
@@ -1118,13 +1121,14 @@ def open_position(context, security, target_hands, direction, reason=''):
|
|
|
# 记录当日交易
|
|
# 记录当日交易
|
|
|
underlying_symbol = security.split('.')[0][:-4]
|
|
underlying_symbol = security.split('.')[0][:-4]
|
|
|
g.today_trades.append({
|
|
g.today_trades.append({
|
|
|
- 'security': security,
|
|
|
|
|
- 'underlying_symbol': underlying_symbol,
|
|
|
|
|
- 'direction': direction,
|
|
|
|
|
- 'order_amount': order_amount,
|
|
|
|
|
- 'order_price': order_price,
|
|
|
|
|
- 'cash_change': cash_change,
|
|
|
|
|
- 'time': context.current_dt
|
|
|
|
|
|
|
+ 'security': security, # 合约代码
|
|
|
|
|
+ 'underlying_symbol': underlying_symbol, # 标的代码
|
|
|
|
|
+ 'direction': direction, # 方向
|
|
|
|
|
+ 'order_amount': order_amount, # 订单数量
|
|
|
|
|
+ 'order_price': order_price, # 订单价格
|
|
|
|
|
+ 'cash_change': cash_change, # 资金变化
|
|
|
|
|
+ 'margin_change': margin_change, # 保证金
|
|
|
|
|
+ 'time': context.current_dt # 时间
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
# 记录交易信息
|
|
# 记录交易信息
|
|
@@ -1133,7 +1137,7 @@ def open_position(context, security, target_hands, direction, reason=''):
|
|
|
'entry_price': order_price,
|
|
'entry_price': order_price,
|
|
|
'target_hands': target_hands,
|
|
'target_hands': target_hands,
|
|
|
'actual_hands': order_amount,
|
|
'actual_hands': order_amount,
|
|
|
- 'actual_margin': cash_change,
|
|
|
|
|
|
|
+ 'actual_margin': margin_change,
|
|
|
'direction': direction,
|
|
'direction': direction,
|
|
|
'entry_time': context.current_dt,
|
|
'entry_time': context.current_dt,
|
|
|
'entry_trading_day': entry_trading_day
|
|
'entry_trading_day': entry_trading_day
|
|
@@ -1153,7 +1157,7 @@ def open_position(context, security, target_hands, direction, reason=''):
|
|
|
g.trade_history[security]['ma_trailing_enabled'] = ma_trailing_enabled
|
|
g.trade_history[security]['ma_trailing_enabled'] = ma_trailing_enabled
|
|
|
|
|
|
|
|
log.info(f"开仓成功: {security} {direction} {order_amount}手 @{order_price:.2f}, "
|
|
log.info(f"开仓成功: {security} {direction} {order_amount}手 @{order_price:.2f}, "
|
|
|
- f"保证金: {cash_change:.0f}, 原因: {reason}")
|
|
|
|
|
|
|
+ f"保证金: {margin_change:.0f}, 资金变化: {cash_change:.0f}, 原因: {reason}")
|
|
|
|
|
|
|
|
return True
|
|
return True
|
|
|
|
|
|
|
@@ -1261,8 +1265,8 @@ def calculate_target_hands(context, security, direction):
|
|
|
multiplier = get_multiplier(underlying_symbol)
|
|
multiplier = get_multiplier(underlying_symbol)
|
|
|
|
|
|
|
|
# 计算单手保证金
|
|
# 计算单手保证金
|
|
|
- log.debug(f"计算单手保证金: {current_price:.2f} * {multiplier:.2f} * {margin_rate:.2f} = {current_price * multiplier * margin_rate:.2f}")
|
|
|
|
|
single_hand_margin = current_price * multiplier * margin_rate
|
|
single_hand_margin = current_price * multiplier * margin_rate
|
|
|
|
|
+ log.debug(f"计算单手保证金: {current_price:.2f} * {multiplier:.2f} * {margin_rate:.2f} = {single_hand_margin:.2f}")
|
|
|
|
|
|
|
|
# 还要考虑可用资金限制
|
|
# 还要考虑可用资金限制
|
|
|
available_cash = context.portfolio.available_cash * g.usage_percentage
|
|
available_cash = context.portfolio.available_cash * g.usage_percentage
|
|
@@ -1283,14 +1287,14 @@ def calculate_target_hands(context, security, direction):
|
|
|
|
|
|
|
|
log.info(f"单手保证金: {single_hand_margin:.0f}, 目标开仓手数: {actual_hands}")
|
|
log.info(f"单手保证金: {single_hand_margin:.0f}, 目标开仓手数: {actual_hands}")
|
|
|
|
|
|
|
|
- return actual_hands
|
|
|
|
|
|
|
+ return actual_hands, single_hand_margin
|
|
|
else:
|
|
else:
|
|
|
# 如果单手保证金超过最大限制,默认开仓1手
|
|
# 如果单手保证金超过最大限制,默认开仓1手
|
|
|
actual_hands = 1
|
|
actual_hands = 1
|
|
|
|
|
|
|
|
log.info(f"单手保证金: {single_hand_margin:.0f} 超过最大限制: {max_margin}, 默认开仓1手")
|
|
log.info(f"单手保证金: {single_hand_margin:.0f} 超过最大限制: {max_margin}, 默认开仓1手")
|
|
|
|
|
|
|
|
- return actual_hands
|
|
|
|
|
|
|
+ return actual_hands, single_hand_margin
|
|
|
|
|
|
|
|
def check_symbol_prefix_match(symbol, hold_symbols):
|
|
def check_symbol_prefix_match(symbol, hold_symbols):
|
|
|
"""检查是否有相似的持仓品种"""
|
|
"""检查是否有相似的持仓品种"""
|