diff --git a/交易/bitmart-五分之一策略交易.py b/交易/bitmart-五分之一策略交易.py index f8c5660..ddeaf24 100644 --- a/交易/bitmart-五分之一策略交易.py +++ b/交易/bitmart-五分之一策略交易.py @@ -22,6 +22,7 @@ BitMart 五分之一回归策略交易(精准版) import time import datetime +from concurrent.futures import ThreadPoolExecutor from tqdm import tqdm from loguru import logger @@ -32,6 +33,9 @@ from DrissionPage import ChromiumOptions from bitmart.api_contract import APIContract from 交易.tools import send_dingtalk_message +# 创建全局线程池用于异步发送钉钉消息 +ding_executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="dingtalk") + class BitmartOneFifthStrategy: def __init__(self, bit_id): @@ -374,14 +378,30 @@ class BitmartOneFifthStrategy: return False def ding(self, msg, error=False): + """ + 异步发送钉钉消息,不阻塞主程序的K线获取和交易操作 + """ prefix = "❌五分之一策略:" if error else "🔔五分之一策略:" + full_msg = f"{prefix}{msg}" + if error: logger.error(msg) + # 异步发送多条错误消息 for i in range(10): - send_dingtalk_message(f"{prefix}{msg}") + ding_executor.submit(self._send_ding_safe, full_msg) else: logger.info(msg) - send_dingtalk_message(f"{prefix}{msg}") + # 异步发送单条消息 + ding_executor.submit(self._send_ding_safe, full_msg) + + def _send_ding_safe(self, msg): + """ + 安全发送钉钉消息,捕获异常防止线程崩溃 + """ + try: + send_dingtalk_message(msg) + except Exception as e: + logger.warning(f"钉钉消息发送失败: {e}") # ========================= 主循环 ========================= @@ -556,4 +576,12 @@ class BitmartOneFifthStrategy: if __name__ == '__main__': - BitmartOneFifthStrategy(bit_id="f2320f57e24c45529a009e1541e25961").action() + try: + BitmartOneFifthStrategy(bit_id="f2320f57e24c45529a009e1541e25961").action() + except KeyboardInterrupt: + logger.info("程序被用户中断") + finally: + # 关闭线程池,等待所有钉钉消息发送完成 + logger.info("正在关闭钉钉消息线程池...") + ding_executor.shutdown(wait=True) + logger.info("线程池已关闭")