This commit is contained in:
薇薇安 2026-01-26 20:26:21 +08:00
parent 51fd3c6550
commit 1eb5c618eb
5 changed files with 128 additions and 37 deletions

View File

@ -724,12 +724,40 @@ class ConfigManager:
"""
# API key/secret/testnet 永远按账号读取(在 get() 内部已处理)
if key in RISK_KNOBS_KEYS:
return self.get(key, default)
value = self.get(key, default)
else:
# 从全局配置表读取
try:
return global_config_mgr.get(key, default)
value = global_config_mgr.get(key, default)
except Exception:
return self.get(key, default)
value = self.get(key, default)
# ⚠️ 关键修复:百分比配置值格式转换
# 如果配置值是百分比形式(>1转换为比例形式除以100
# 兼容数据库中存储的百分比形式如30表示30%和比例形式如0.30表示30%
if isinstance(value, (int, float)) and value is not None:
# 需要转换的百分比配置项
percent_keys = [
'TRAILING_STOP_ACTIVATION',
'TRAILING_STOP_PROTECT',
'MIN_VOLATILITY',
'TAKE_PROFIT_PERCENT',
'STOP_LOSS_PERCENT',
'MIN_STOP_LOSS_PRICE_PCT',
'MIN_TAKE_PROFIT_PRICE_PCT',
'FIXED_RISK_PERCENT',
'MAX_POSITION_PERCENT',
'MAX_TOTAL_POSITION_PERCENT',
'MIN_POSITION_PERCENT',
]
if key in percent_keys:
# 如果值>1认为是百分比形式转换为比例形式
if value > 1:
value = value / 100.0
logger.debug(f"配置值格式转换: {key} = {value*100:.2f}% (从百分比形式转换为比例形式)")
return value
return {
# 仓位控制

View File

@ -343,8 +343,16 @@ async def main():
logger.info(f" 每笔风险: {config.TRADING_CONFIG.get('FIXED_RISK_PERCENT', 0.02)*100:.1f}%")
logger.info(f" 移动止损: {'开启' if config.TRADING_CONFIG.get('USE_TRAILING_STOP') else '关闭'}")
if config.TRADING_CONFIG.get('USE_TRAILING_STOP'):
logger.info(f" 激活条件: 盈利{config.TRADING_CONFIG.get('TRAILING_STOP_ACTIVATION', 0.1)*100:.0f}%")
logger.info(f" 保护利润: {config.TRADING_CONFIG.get('TRAILING_STOP_PROTECT', 0.05)*100:.0f}%")
# 修复配置值已经是比例形式直接乘以100显示
trailing_activation = config.TRADING_CONFIG.get('TRAILING_STOP_ACTIVATION', 0.1)
trailing_protect = config.TRADING_CONFIG.get('TRAILING_STOP_PROTECT', 0.05)
# 如果值>1说明可能还是百分比形式需要转换否则直接使用
if trailing_activation > 1:
trailing_activation = trailing_activation / 100.0
if trailing_protect > 1:
trailing_protect = trailing_protect / 100.0
logger.info(f" 激活条件: 盈利{trailing_activation*100:.0f}%")
logger.info(f" 保护利润: {trailing_protect*100:.0f}%")
logger.info(f" 最小持仓时间: {config.TRADING_CONFIG.get('MIN_HOLD_TIME_SEC', 0)}")
logger.info("")
logger.info("【市场扫描】")
@ -352,7 +360,11 @@ async def main():
logger.info(f" 扫描交易对数量: {config.TRADING_CONFIG.get('MAX_SCAN_SYMBOLS', 500)}")
logger.info(f" 处理交易对数量: {config.TRADING_CONFIG['TOP_N_SYMBOLS']}")
logger.info(f" 最小24H成交额: ${config.TRADING_CONFIG.get('MIN_VOLUME_24H', 0)/1000000:.0f}M")
logger.info(f" 最小波动率: {config.TRADING_CONFIG.get('MIN_VOLATILITY', 0.02)*100:.1f}%")
# 修复:配置值格式转换
min_volatility = config.TRADING_CONFIG.get('MIN_VOLATILITY', 0.02)
if min_volatility > 1:
min_volatility = min_volatility / 100.0
logger.info(f" 最小波动率: {min_volatility*100:.1f}%")
logger.info(f" 最小信号强度: {config.TRADING_CONFIG.get('MIN_SIGNAL_STRENGTH', 5)}")
logger.info(f" 最小涨跌幅: {config.TRADING_CONFIG['MIN_CHANGE_PERCENT']:.1f}%")
logger.info("")

View File

@ -490,6 +490,9 @@ class PositionManager:
# ===== 成交后基于“实际成交价/数量”重新计算止损止盈(修复限价/滑点导致的偏差)=====
stop_loss_pct_margin = config.TRADING_CONFIG.get('STOP_LOSS_PERCENT', 0.03)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if stop_loss_pct_margin is not None and stop_loss_pct_margin > 1:
stop_loss_pct_margin = stop_loss_pct_margin / 100.0
stop_loss_price = self.risk_manager.get_stop_loss_price(
entry_price, side, quantity, leverage,
stop_loss_pct=stop_loss_pct_margin,
@ -510,6 +513,9 @@ class PositionManager:
stop_distance_for_tp = entry_price * atr_percent * atr_multiplier
take_profit_pct_margin = config.TRADING_CONFIG.get('TAKE_PROFIT_PERCENT', 0.30)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if take_profit_pct_margin is not None and take_profit_pct_margin > 1:
take_profit_pct_margin = take_profit_pct_margin / 100.0
if take_profit_pct_margin is None or take_profit_pct_margin == 0:
take_profit_pct_margin = float(stop_loss_pct_margin or 0) * 3.0
take_profit_price = self.risk_manager.get_take_profit_price(
@ -1442,6 +1448,13 @@ class PositionManager:
trailing_activation = config.TRADING_CONFIG.get('TRAILING_STOP_ACTIVATION', 0.01) # 相对于保证金
trailing_protect = config.TRADING_CONFIG.get('TRAILING_STOP_PROTECT', 0.01) # 相对于保证金
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
# 如果值>1认为是百分比形式转换为比例形式
if trailing_activation > 1:
trailing_activation = trailing_activation / 100.0
if trailing_protect > 1:
trailing_protect = trailing_protect / 100.0
if not position_info.get('trailingStopActivated', False):
# 盈利超过阈值后(相对于保证金),激活移动止损
if pnl_percent_margin > trailing_activation * 100:
@ -2346,7 +2359,13 @@ class PositionManager:
# 计算止损止盈(基于保证金)
leverage = binance_position.get('leverage', 10)
stop_loss_pct_margin = config.TRADING_CONFIG.get('STOP_LOSS_PERCENT', 0.08)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if stop_loss_pct_margin is not None and stop_loss_pct_margin > 1:
stop_loss_pct_margin = stop_loss_pct_margin / 100.0
take_profit_pct_margin = config.TRADING_CONFIG.get('TAKE_PROFIT_PERCENT', 0.15)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if take_profit_pct_margin is not None and take_profit_pct_margin > 1:
take_profit_pct_margin = take_profit_pct_margin / 100.0
# 如果配置中没有设置止盈则使用止损的2倍作为默认
if take_profit_pct_margin is None or take_profit_pct_margin == 0:
take_profit_pct_margin = stop_loss_pct_margin * 2.0
@ -2441,7 +2460,13 @@ class PositionManager:
# 计算止损止盈(基于保证金)
leverage = position.get('leverage', 10)
stop_loss_pct_margin = config.TRADING_CONFIG.get('STOP_LOSS_PERCENT', 0.08)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if stop_loss_pct_margin is not None and stop_loss_pct_margin > 1:
stop_loss_pct_margin = stop_loss_pct_margin / 100.0
take_profit_pct_margin = config.TRADING_CONFIG.get('TAKE_PROFIT_PERCENT', 0.15)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if take_profit_pct_margin is not None and take_profit_pct_margin > 1:
take_profit_pct_margin = take_profit_pct_margin / 100.0
# 如果配置中没有设置止盈则使用止损的2倍作为默认
if take_profit_pct_margin is None or take_profit_pct_margin == 0:
take_profit_pct_margin = stop_loss_pct_margin * 2.0
@ -2701,6 +2726,13 @@ class PositionManager:
f"{symbol} [实时监控] 移动止损激活: 止损移至成本价 {entry_price:.4f} "
f"(盈利: {pnl_percent_margin:.2f}% of margin)"
)
else:
# ⚠️ 优化:如果分步止盈第一目标已触发,移动止损不再更新剩余仓位的止损价
# 原因分步止盈第一目标触发后剩余50%仓位止损已移至成本价(保本),等待第二目标
# 移动止损不应该覆盖分步止盈设置的止损价
if position_info.get('partialProfitTaken', False):
# 分步止盈第一目标已触发,移动止损不再更新
logger.debug(f"{symbol} [实时监控-移动止损] 分步止盈第一目标已触发,移动止损不再更新剩余仓位止损价")
else:
# 盈利超过阈值后,止损移至保护利润位(基于保证金)
# 计算需要保护的利润金额

View File

@ -650,6 +650,11 @@ class RiskManager:
# 获取止损百分比(相对于保证金)
stop_loss_percent = stop_loss_pct or config.TRADING_CONFIG['STOP_LOSS_PERCENT']
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
# 如果值>1认为是百分比形式转换为比例形式
if stop_loss_percent > 1:
stop_loss_percent = stop_loss_percent / 100.0
# 计算止损金额(相对于保证金)
stop_loss_amount = margin * stop_loss_percent
@ -813,6 +818,11 @@ class RiskManager:
# 获取止盈百分比(相对于保证金)
take_profit_percent = take_profit_pct or config.TRADING_CONFIG['TAKE_PROFIT_PERCENT']
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
# 如果值>1认为是百分比形式转换为比例形式
if take_profit_percent > 1:
take_profit_percent = take_profit_percent / 100.0
# 计算止盈金额(相对于保证金)
take_profit_amount = margin * take_profit_percent

View File

@ -432,7 +432,13 @@ class TradeRecommender:
# 计算基于保证金的止损止盈
stop_loss_pct_margin = config.TRADING_CONFIG.get('STOP_LOSS_PERCENT', 0.08)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if stop_loss_pct_margin is not None and stop_loss_pct_margin > 1:
stop_loss_pct_margin = stop_loss_pct_margin / 100.0
take_profit_pct_margin = config.TRADING_CONFIG.get('TAKE_PROFIT_PERCENT', 0.15)
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if take_profit_pct_margin is not None and take_profit_pct_margin > 1:
take_profit_pct_margin = take_profit_pct_margin / 100.0
stop_loss_price = self.risk_manager.get_stop_loss_price(
entry_price,
@ -454,6 +460,9 @@ class TradeRecommender:
# 第一目标30%固定止盈基于保证金保证拿到30%盈利,然后留一部分去追求更高利润
take_profit_1_pct_margin = config.TRADING_CONFIG.get('TAKE_PROFIT_PERCENT', 0.30) # 30%固定止盈
# ⚠️ 关键修复:配置值格式转换(兼容百分比形式和比例形式)
if take_profit_1_pct_margin > 1:
take_profit_1_pct_margin = take_profit_1_pct_margin / 100.0
take_profit_1 = self.risk_manager.get_take_profit_price(
entry_price, direction, estimated_quantity, leverage,
take_profit_pct=take_profit_1_pct_margin