fix(risk_manager): 修复止损和止盈价格选择逻辑
在风险管理模块中,优化了止损和止盈价格的选择逻辑,确保在做多和做空时分别选择更高和更低的止损价,以提高风险控制的有效性。同时,调整了代码缩进和结构,提升了可读性和一致性。这一改动旨在增强系统的稳定性和交易策略的安全性。
This commit is contained in:
parent
87c018594b
commit
beafeb2707
|
|
@ -1090,10 +1090,10 @@ class RiskManager:
|
||||||
logger.debug(f"优先使用ATR止损: {stop_loss_price:.4f}, 忽略保证金止损: {stop_loss_price_margin:.4f}")
|
logger.debug(f"优先使用ATR止损: {stop_loss_price:.4f}, 忽略保证金止损: {stop_loss_price_margin:.4f}")
|
||||||
else:
|
else:
|
||||||
# ATR不可用,使用保证金止损和价格百分比止损中"更紧"的一个(保护资金)
|
# ATR不可用,使用保证金止损和价格百分比止损中"更紧"的一个(保护资金)
|
||||||
if side == 'BUY':
|
if side == 'BUY':
|
||||||
# 做多:取最大值(更高的止损价,更接近入场价)
|
# 做多:取最大值(更高的止损价,更接近入场价)
|
||||||
stop_loss_price = max(p[1] for p in candidate_prices if p[0] != 'ATR')
|
stop_loss_price = max(p[1] for p in candidate_prices if p[0] != 'ATR')
|
||||||
else:
|
else:
|
||||||
# 做空:取最小值(更低的止损价,更接近入场价)
|
# 做空:取最小值(更低的止损价,更接近入场价)
|
||||||
stop_loss_price = min(p[1] for p in candidate_prices if p[0] != 'ATR')
|
stop_loss_price = min(p[1] for p in candidate_prices if p[0] != 'ATR')
|
||||||
|
|
||||||
|
|
@ -1159,15 +1159,15 @@ class RiskManager:
|
||||||
final_stop_loss = atr_candidate[1]
|
final_stop_loss = atr_candidate[1]
|
||||||
selected_method = 'ATR'
|
selected_method = 'ATR'
|
||||||
else:
|
else:
|
||||||
if side == 'BUY':
|
if side == 'BUY':
|
||||||
# 做多:选择更高的止损价(更紧)
|
# 做多:选择更高的止损价(更紧)
|
||||||
final_stop_loss = max(p[1] for p in candidate_prices)
|
final_stop_loss = max(p[1] for p in candidate_prices)
|
||||||
selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0]
|
selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0]
|
||||||
else:
|
else:
|
||||||
# ⚠️ 关键修复:做空必须选择更低的止损价(更接近入场价,更紧)
|
# ⚠️ 关键修复:做空必须选择更低的止损价(更接近入场价,更紧)
|
||||||
# 注意:对于SELL单,止损价高于入场价,所以"更低的止损价"意味着更接近入场价
|
# 注意:对于SELL单,止损价高于入场价,所以"更低的止损价"意味着更接近入场价
|
||||||
final_stop_loss = min(p[1] for p in candidate_prices)
|
final_stop_loss = min(p[1] for p in candidate_prices)
|
||||||
selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0]
|
selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0]
|
||||||
|
|
||||||
# ⚠️ 关键修复:验证最终止损价对应的保证金百分比不超过配置值
|
# ⚠️ 关键修复:验证最终止损价对应的保证金百分比不超过配置值
|
||||||
if side == 'BUY':
|
if side == 'BUY':
|
||||||
|
|
@ -1392,7 +1392,7 @@ class RiskManager:
|
||||||
selected_method = 'ATR盈亏比'
|
selected_method = 'ATR盈亏比'
|
||||||
if use_margin_cap and take_profit_price_margin is not None:
|
if use_margin_cap and take_profit_price_margin is not None:
|
||||||
# 取「更近」的止盈,避免盈亏比止盈过远难以触及
|
# 取「更近」的止盈,避免盈亏比止盈过远难以触及
|
||||||
if side == 'BUY':
|
if side == 'BUY':
|
||||||
if take_profit_price_margin < take_profit_price:
|
if take_profit_price_margin < take_profit_price:
|
||||||
take_profit_price = take_profit_price_margin
|
take_profit_price = take_profit_price_margin
|
||||||
selected_method = '保证金(止盈上限)'
|
selected_method = '保证金(止盈上限)'
|
||||||
|
|
@ -1406,10 +1406,10 @@ class RiskManager:
|
||||||
# 如果没有基于盈亏比的止盈,选择最远的止盈(给利润更多空间,提高盈亏比)
|
# 如果没有基于盈亏比的止盈,选择最远的止盈(给利润更多空间,提高盈亏比)
|
||||||
if side == 'BUY':
|
if side == 'BUY':
|
||||||
# 做多:选择更高的止盈价(更远,给利润更多空间)
|
# 做多:选择更高的止盈价(更远,给利润更多空间)
|
||||||
take_profit_price = max(p[1] for p in candidate_prices)
|
take_profit_price = max(p[1] for p in candidate_prices)
|
||||||
else:
|
else:
|
||||||
# 做空:选择更低的止盈价(更远,给利润更多空间)
|
# 做空:选择更低的止盈价(更远,给利润更多空间)
|
||||||
take_profit_price = min(p[1] for p in candidate_prices)
|
take_profit_price = min(p[1] for p in candidate_prices)
|
||||||
selected_method = [p[0] for p in candidate_prices if p[1] == take_profit_price][0]
|
selected_method = [p[0] for p in candidate_prices if p[1] == take_profit_price][0]
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user