This commit is contained in:
27942
2026-01-06 13:51:48 +08:00
parent 334dcccbf2
commit 28003da75e
3 changed files with 78 additions and 8 deletions

Binary file not shown.

View File

@@ -9,6 +9,7 @@ from DrissionPage import ChromiumPage
from DrissionPage import ChromiumOptions
from bitmart.api_contract import APIContract
from 交易.tools import send_dingtalk_message
class BitmartFuturesTransaction:
@@ -66,7 +67,7 @@ class BitmartFuturesTransaction:
return formatted # 最近3根: kline_1 (最老), kline_2, kline_3 (最新)
except Exception as e:
logger.error(f"获取K线异常: {e}")
self.ding(error=True, msg="获取K线异常")
self.ding(msg="获取K线异常", error=True)
return None
def get_current_price(self):
@@ -112,6 +113,9 @@ class BitmartFuturesTransaction:
positions = response['data']
if not positions:
self.start = 0
self.open_avg_price = None
self.current_amount = None
self.position_cross = None
return True
self.start = 1 if positions[0]['position_type'] == 1 else -1
self.open_avg_price = positions[0]['open_avg_price']
@@ -221,8 +225,16 @@ class BitmartFuturesTransaction:
self.page.ele('x://*[@id="size_0"]').input(1)
self.click_safe('x://span[normalize-space(text()) ="买入/做多"]')
def ding(self, text, error=False):
logger.info(text)
def ding(self, msg, error=False):
"""统一消息格式"""
prefix = "❌bitmart" if error else "🔔bitmart"
if error:
logger.error(msg)
for i in range(10):
send_dingtalk_message(f"{prefix}{msg}")
else:
logger.info(msg)
send_dingtalk_message(f"{prefix}{msg}")
def close_extra_tabs_in_browser(self):
@@ -349,7 +361,7 @@ class BitmartFuturesTransaction:
else:
logger.info("获取仓位信息失败!!!")
self.send_dingtalk_message(message_content=f"获取仓位信息失败!!!", type=0)
self.ding(msg="获取仓位信息失败!!!", error=True)
continue
if self.start == 1:
@@ -375,6 +387,64 @@ class BitmartFuturesTransaction:
elif self.start == 0:
self.开单(marketPriceLongOrder=-1, size=self.get_available_balance() * self.risk_percent)
# ===================================================================================================
# 发送持仓信息消息
msg = None
current_price = float(self.kline_3["close"])
balance = self.get_available_balance()
self.balance = balance if balance is not None else 0.0
if self.start:
# 持仓方向,开仓价格,现价,持仓量,盈亏,当前价值
# 1. 确保所有关键数据为 float 类型BitMart API 常返回字符串)
open_avg_price = float(self.open_avg_price) if self.open_avg_price else 0.0
current_amount = float(self.current_amount) if self.current_amount else 0.0
position_cross = float(self.position_cross) if hasattr(self, 'position_cross') and self.position_cross else 0.0
# 2. 计算浮动盈亏USDT
if self.start == 1: # 多头
unrealized_pnl = current_amount * 0.001 * (current_price - open_avg_price)
elif self.start == -1: # 空头
unrealized_pnl = current_amount * 0.001 * (open_avg_price - current_price)
else: # 无仓
unrealized_pnl = 0.0
# 3. 计算收益率(可选,更直观)
if self.start != 0 and open_avg_price > 0:
if self.start == 1:
pnl_rate = (current_price - open_avg_price) / open_avg_price * 10000
else:
pnl_rate = (open_avg_price - current_price) / open_avg_price * 10000
rate_str = f" ({pnl_rate:+.2f}%)"
else:
rate_str = ""
# 4. 格式化输出字符串
direction_str = "" if self.start == -1 else ("" if self.start == 1 else "")
pnl_str = f"{unrealized_pnl:+.2f} USDT"
# 6. 最终消息
msg = (
f"【BitMart {self.contract_symbol} 永续】\n"
f"当前方向:{direction_str}\n"
f"当前现价:{current_price:.2f} USDT\n"
f"开仓均价:{open_avg_price:.2f} USDT\n"
f"持仓量(eht){float(current_amount) / 1000} eth\n"
f"持仓量(usdt){position_cross} usdt\n"
f"浮动盈亏:{pnl_str}{rate_str}\n"
f"账户可用余额:{self.balance:.2f} usdt"
)
else:
msg = (
f"【BitMart {self.contract_symbol} 永续】\n"
f"当前方向:无\n"
f"当前现价:{current_price:.2f} USDT\n"
f"账户可用余额:{self.balance:.2f} usdt"
)
# 7. 发送钉钉消息
self.ding(msg=msg)
self.pbar.reset() # 重置进度条

View File

@@ -16,10 +16,10 @@ async def to_do_tg(message_content):
PROXY = {
'proxy_type': "socks5",
'addr': "202.155.144.102",
'port': 31102,
'username': "SyNuejCtrQ",
'password': "MH8ioL7EXf"
'addr': "199.168.137.123",
'port': 12345,
'username': "haha",
'password': "haha"
}
try: