优化完美代码,优化计算盈亏

This commit is contained in:
27942
2026-02-08 01:10:29 +08:00
parent e1cc39a18e
commit 438cffd31a

View File

@@ -639,21 +639,59 @@ class BitmartFuturesTransaction:
if atr is not None:
if self.start == 1: # 持多:价格跌破 trailing_stop 平仓
trailing_stop = self.max_high_since_open - atr * self.atr_k
logger.debug(f"ATR止损(多)trailing_stop={trailing_stop:.2f}, 当前价={current_price:.2f}, "
f"最高={self.max_high_since_open:.2f}, ATR={atr:.4f}*{self.atr_k}")
if current_price < trailing_stop:
logger.info(f"ATR移动止损(多):当前价 {current_price:.2f} < 跟踪止盈 {trailing_stop:.2f} (最高{self.max_high_since_open:.2f} - ATR{atr:.4f}*{self.atr_k}),平仓")
logger.info(f"ATR移动止损(多)触发:当前价 {current_price:.2f} < trailing_stop {trailing_stop:.2f} "
f"(最高{self.max_high_since_open:.2f} - ATR{atr:.4f}*{self.atr_k}),执行平仓")
self.平仓()
self.max_high_since_open = None
self.last_trade_kline_id = current_kline_time
time.sleep(3)
time.sleep(1)
# 轮询验证平仓是否成功最多等约10秒
closed = False
for _ in range(10):
if self.get_position_status() and self.start == 0:
closed = True
break
time.sleep(1)
if closed:
logger.success("ATR移动止损(多)平仓成功")
self.max_high_since_open = None
self.min_low_since_open = None
self.last_trade_kline_id = current_kline_time
page_start = True
self.page.close()
time.sleep(5)
else:
logger.error("ATR移动止损(多)平仓失败,仓位仍在,下次循环重试")
# 不重置 max_high_since_open保留原止损线下次继续触发
continue
else: # 持空:价格升破 trailing_stop 平仓
trailing_stop = self.min_low_since_open + atr * self.atr_k
logger.debug(f"ATR止损(空)trailing_stop={trailing_stop:.2f}, 当前价={current_price:.2f}, "
f"最低={self.min_low_since_open:.2f}, ATR={atr:.4f}*{self.atr_k}")
if current_price > trailing_stop:
logger.info(f"ATR移动止损(空):当前价 {current_price:.2f} > 跟踪止盈 {trailing_stop:.2f} (最低{self.min_low_since_open:.2f} + ATR{atr:.4f}*{self.atr_k}),平仓")
logger.info(f"ATR移动止损(空)触发:当前价 {current_price:.2f} > trailing_stop {trailing_stop:.2f} "
f"(最低{self.min_low_since_open:.2f} + ATR{atr:.4f}*{self.atr_k}),执行平仓")
self.平仓()
self.min_low_since_open = None
self.last_trade_kline_id = current_kline_time
time.sleep(3)
time.sleep(1)
# 轮询验证平仓是否成功最多等约10秒
closed = False
for _ in range(10):
if self.get_position_status() and self.start == 0:
closed = True
break
time.sleep(1)
if closed:
logger.success("ATR移动止损(空)平仓成功")
self.min_low_since_open = None
self.max_high_since_open = None
self.last_trade_kline_id = current_kline_time
page_start = True
self.page.close()
time.sleep(5)
else:
logger.error("ATR移动止损(空)平仓失败,仓位仍在,下次循环重试")
# 不重置 min_low_since_open保留原止损线下次继续触发
continue
# 4. 检查信号