日志展示优化

This commit is contained in:
ddrwode
2026-02-06 16:59:40 +08:00
parent 96a6ec9d20
commit 61c1459cd2

View File

@@ -137,8 +137,8 @@ class BitmartFuturesTransaction:
self.open_type = "cross" # 全仓模式
self.risk_percent = 0 # 未使用;若启用则可为每次开仓占可用余额的百分比
self.stop_loss_usd = -3 # 固定止损:亏损达到 3 美元平仓
self.trailing_activation_usd = 2 # 盈利达到此金额后启动移动止损
self.trailing_distance_usd = 1.5 # 从最高盈利回撤此金额则平仓
self.trailing_activation_usd = 6 # 盈利达到此金额后启动移动止损(调高避免波动不大就触发)
self.trailing_distance_usd = 3 # 从最高盈利回撤此金额则平仓(调高避免小幅回撤就平)
self.max_unrealized_pnl_seen = None # 持仓期间见过的最大盈利(用于移动止损)
# 当前K线从极值回落平仓
# 模式: 'fixed'=固定点数(drop_from_high_to_close)'pct_retrace'=按本K线涨幅比例动态算回撤
@@ -146,11 +146,12 @@ class BitmartFuturesTransaction:
self.drop_from_high_to_close = 2 # fixed 模式下:回落/反弹超过此价格点数则平仓0 表示关闭
# pct_retrace 模式本K线涨幅 = (最高-开盘)/开盘*100允许回撤% = 涨幅% * retrace_ratio从最高点回撤超过则平仓
self.retrace_ratio = 0.5 # 回撤系数,如 0.5 表示允许回撤涨幅的 50%(类似斐波那契 50% 回撤)
self.min_rise_pct_to_activate = 0.02 # 至少涨/跌这么多才启用动态回撤,避免噪音
self.min_drop_pct_from_high = 0.03 # 至少从最高点回撤这么多%才平仓(保底,避免过于敏感
self.min_rise_pct_to_activate = 0.06 # 至少涨/跌这么多才启用动态回撤(调高避免波动不大就触发)
self.min_drop_pct_from_high = 0.08 # 至少从最高点回撤这么多%才平仓(调高避免小幅波动就平
# EMA(10) + ATR(14) 平仓(多/空一致):多单跌破 EMA10 或从最高回撤≥ATR 平;空单涨破 EMA10 或从最低反弹≥ATR 平
self.use_ema_atr_exit = True # 是否启用 EMA/ATR 平仓规则(多单+空单)
self.atr_multiplier = 1.1 # 追踪止盈:从极值回撤/反弹 ≥ 此倍数 × ATR(14) 则平仓
self.atr_multiplier = 1.8 # 追踪止盈:从极值回撤/反弹 ≥ 此倍数×ATR(14) 则平仓(调高避免波动不大就平)
self.min_hold_seconds = 90 # 开仓/反手后至少持仓此时长才允许技术性止盈EMA/ATR/K线回撤/移动止损);固定止损始终生效
self._candle_high_seen = None # 当前K线内见过的最高价多头用
self._candle_low_seen = None # 当前K线内见过的最低价空头用
self._candle_id_for_high_low = None # 记录高低对应的K线 id换线则重置
@@ -705,6 +706,7 @@ class BitmartFuturesTransaction:
if self.verify_position_direction(1):
self.max_unrealized_pnl_seen = None
self.last_open_time = time.time() # 反手后的新仓位也受最短持仓时间保护
logger.success(LOG_POSITION + "反手做多成功")
self.last_reverse_time = time.time()
time.sleep(20)
@@ -731,6 +733,7 @@ class BitmartFuturesTransaction:
if self.verify_position_direction(-1):
self.max_unrealized_pnl_seen = None
self.last_open_time = time.time() # 反手后的新仓位也受最短持仓时间保护
logger.success(LOG_POSITION + "反手做空成功")
self.last_reverse_time = time.time()
time.sleep(20)
@@ -869,8 +872,11 @@ class BitmartFuturesTransaction:
elif self.start == -1:
self._candle_low_seen = min(self._candle_low_seen or float('inf'), current_price)
# 多单EMA(10) + ATR(14) 平仓 —— 收盘跌破 EMA10 先平;或从最高价回撤 ≥ 1.1×ATR 平
if self.start == 1 and self.use_ema_atr_exit:
hold_sec = (time.time() - self.last_open_time) if self.last_open_time else 999999
allow_technical_exit = hold_sec >= self.min_hold_seconds # 未满最短持仓时间则只允许固定止损
# 多单EMA(10) + ATR(14) 平仓(需满最短持仓时间)
if allow_technical_exit and self.start == 1 and self.use_ema_atr_exit:
kline_series = self.get_klines_series(35)
ema10, ema20, atr14 = self.get_ema_atr_for_exit(kline_series)
if ema10 is not None and current_price < ema10:
@@ -888,8 +894,8 @@ class BitmartFuturesTransaction:
time.sleep(3)
continue
# 空单EMA(10) + ATR(14) 平仓 —— 收盘涨破 EMA10 先平;或从最低价反弹 ≥ 1.1×ATR 平
if self.start == -1 and self.use_ema_atr_exit:
# 空单EMA(10) + ATR(14) 平仓(需满最短持仓时间)
if allow_technical_exit and self.start == -1 and self.use_ema_atr_exit:
kline_series = self.get_klines_series(35)
ema10, ema20, atr14 = self.get_ema_atr_for_exit(kline_series)
if ema10 is not None and current_price > ema10:
@@ -909,7 +915,7 @@ class BitmartFuturesTransaction:
use_fixed = self.drop_from_high_mode == 'fixed' and self.drop_from_high_to_close and self.drop_from_high_to_close > 0
use_pct = self.drop_from_high_mode == 'pct_retrace'
if use_fixed or use_pct:
if allow_technical_exit and (use_fixed or use_pct):
if self.start == 1: # 多头:最高价已在上面更新,这里只做回落判断
do_close = False
if use_fixed and self._candle_high_seen and current_price <= self._candle_high_seen - self.drop_from_high_to_close:
@@ -970,8 +976,8 @@ class BitmartFuturesTransaction:
self.max_unrealized_pnl_seen = pnl_usd
else:
self.max_unrealized_pnl_seen = max(self.max_unrealized_pnl_seen, pnl_usd)
# 移动止损:盈利曾达到 activation 后,从最高盈利回撤 trailing_distance 则平仓
if self.max_unrealized_pnl_seen >= self.trailing_activation_usd:
# 移动止损:盈利曾达到 activation 后,从最高盈利回撤 trailing_distance 则平仓(需满最短持仓时间)
if allow_technical_exit and self.max_unrealized_pnl_seen >= self.trailing_activation_usd:
if pnl_usd < self.max_unrealized_pnl_seen - self.trailing_distance_usd:
logger.info(LOG_POSITION + f"移动止损平仓 | 盈利 {pnl_usd:.2f} 从最高 {self.max_unrealized_pnl_seen:.2f} 回撤≥{self.trailing_distance_usd}$")
self.平仓()