bitmart优化完成

This commit is contained in:
Administrator
2025-12-17 18:10:48 +08:00
parent 7ecdd92776
commit 86b4616246
5 changed files with 159 additions and 120 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -154,6 +154,7 @@ class BitmartFuturesTransaction:
self.start = 1 if positions[0]['position_type'] == 1 else -1
self.open_avg_price = positions[0]['open_avg_price']
self.current_amount = positions[0]['current_amount']
self.position_cross = positions[0]["position_cross"]
return True
@@ -309,49 +310,63 @@ class BitmartFuturesTransaction:
self.pbar.reset()
# ===================================================================================================
size = self.calculate_size() # 获取可用余额
if not self.get_position_status():
self.ding(error=True, msg="获取仓位信息失败!!!")
continue
# 持仓方向,开仓价格,现价,持仓量,盈亏,当前价值
# 1. 确保所有关键数据为 float 类型BitMart API 常返回字符串)
msg = None
current_price = float(kline_3["close"])
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
self.balance = self.get_available_balance()
if self.start:
if not self.get_position_status():
self.ding(error=True, msg="获取仓位信息失败!!!")
continue
# 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
# 持仓方向,开仓价格,现价,持仓量,盈亏,当前价值
# 1. 确保所有关键数据为 float 类型BitMart API 常返回字符串)
# 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
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
# 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:
pnl_rate = (open_avg_price - current_price) / open_avg_price * 10000
rate_str = f" ({pnl_rate:+.2f}%)"
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(self.current_amount) / 1000} eth\n"
f"持仓量(usdt){float(self.position_cross)} usdt\n"
f"浮动盈亏:{pnl_str}{rate_str}\n"
f"账户可用余额:{self.balance:.2f} usdt"
)
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"持仓量:{float(self.current_amount) / 1000} eth\n"
f"浮动盈亏:{pnl_str}{rate_str}\n"
f"账户可用余额:{self.balance:.2f} usdt"
)
msg = (
f"【BitMart {self.contract_symbol} 永续】\n"
f"当前方向:无\n"
f"当前现价:{current_price:.2f} USDT\n"
# f"开仓均价:{open_avg_price:.2f} USDT\n"
# f"持仓量:{float(self.current_amount) / 1000} eth\n"
# f"浮动盈亏:{pnl_str}{rate_str}\n"
f"账户可用余额:{self.balance:.2f} usdt"
)
# 7. 发送钉钉消息
self.ding(msg=msg)

View File

@@ -412,27 +412,37 @@ class WeexTransaction:
self.pbar.reset() # 重置进度条
# ===============================================================================================
# 提取并转换
direction = "" if int(self.resp_data['type']) == 2 else ""
contracts = int(self.resp_data['number']) # 张数
eth_amount = float(self.resp_data['numberConvert']) # ETH数量
open_price = float(self.resp_data['openPriceAvg'])
current_price = float(self.resp_data['markPrice']) # 标记价格(更准确用于盈亏)
unrealized_pnl = float(self.resp_data['unProfitLoss'])
pnl_rate = float(self.resp_data['profitRate'])
available_balance = float(self.resp_data['accountAvailable'])
num = self.get_num()
if self.start:
# 提取并转换
direction = "" if int(self.resp_data['type']) == 2 else ""
contracts = int(self.resp_data['number']) # 张数
eth_amount = float(self.resp_data['numberConvert']) # ETH数量
open_price = float(self.resp_data['openPriceAvg'])
current_price = float(self.resp_data['markPrice']) # 标记价格(更准确用于盈亏)
unrealized_pnl = float(self.resp_data['unProfitLoss'])
pnl_rate = float(self.resp_data['profitRate'])
available_balance = float(self.resp_data['accountAvailable'])
# 钉钉消息示例
msg = (
f"【WebSea ETH-USDT 永续持仓】\n"
f"持仓方向:{direction}\n"
f"持仓张数:{contracts}\n"
f"持仓数量(eth){eth_amount:.3f} ETH\n"
f"持仓数量(usdt){float(self.resp_data["numberConvertU"]) / 100:.3f} usdt\n"
f"开仓均价:{open_price:.2f} USDT\n"
f"标记现价:{current_price:.2f} USDT\n"
f"浮动盈亏:{unrealized_pnl:+.2f} USDT ({pnl_rate:+.2f}%)\n"
f"账户可用:{available_balance:.2f} USDT"
)
else:
msg = (
f"【WebSea ETH-USDT 永续持仓】\n"
f"持仓方向:无\n"
f"账户可用:{float(num):.2f} USDT"
)
# 钉钉消息示例
msg = (
f"【WebSea ETH-USDT 永续持仓】\n"
f"持仓方向:{direction}\n"
f"持仓张数:{contracts}\n"
f"持仓数量:{eth_amount:.3f} ETH\n"
f"开仓均价:{open_price:.2f} USDT\n"
f"标记现价:{current_price:.2f} USDT\n"
f"浮动盈亏:{unrealized_pnl:+.2f} USDT ({pnl_rate:+.2f}%)\n"
f"账户可用:{available_balance:.2f} USDT"
)
self.send_dingtalk_message(
message_content=
msg

View File

@@ -442,79 +442,93 @@ class WeexTransaction:
num = self.get_num()
# 持仓方向,开仓价格,现价,持仓量,盈亏,当前价值
# 1. 从 self.datas 中提取并转换关键数据
# 假设 self.datas 是你贴的那个JSON字典如果是个列表取最新一笔
data = self.datas
if isinstance(data, list) and data:
data = data[-1] # 如果是列表,取最新一笔成交
fill_size = float(self.datas['fillSize']) # 持仓量单位ETH
fill_value = float(self.datas['fillValue']) # 成交名义价值 USDT
open_avg_price = fill_value / fill_size # 开仓均价(本笔成交均价)
position_side = self.datas['positionSide'] # "SHORT" 或 "LONG"
# 2. 方向判断并设置 self.start方便后续策略使用
if position_side == 'SHORT':
direction = ""
self.start = -1
elif position_side == 'LONG':
direction = ""
self.start = 1
else:
direction = ""
self.start = 0
# 3. 当前价格从你的K线数据
message_content = None
current_price = float(self.kline_3["close"])
if self.start:
# 4. 持仓量(假设当前持仓等于这笔成交量,如有加减仓后续可维护累计)
current_amount = fill_size # 单位ETH
# 1. 从 self.datas 中提取并转换关键数据
# 假设 self.datas 是你贴的那个JSON字典如果是个列表取最新一笔
data = self.datas
if isinstance(data, list) and data:
data = data[-1] # 如果是列表,取最新一笔成交
# 5. 计算浮动盈亏USDT
if self.start == 1: # 多头
unrealized_pnl = current_amount * (current_price - open_avg_price)
elif self.start == -1: # 空头
unrealized_pnl = current_amount * (open_avg_price - current_price)
else:
unrealized_pnl = 0.0
fill_size = float(self.datas['fillSize']) # 持仓量单位ETH
fill_value = float(self.datas['fillValue']) # 成交名义价值 USDT
open_avg_price = fill_value / fill_size # 开仓均价(本笔成交均价)
# 6. 收益率
if self.start != 0 and open_avg_price > 0:
if self.start == 1:
pnl_rate = (current_price - open_avg_price) / open_avg_price * 10000
position_side = self.datas['positionSide'] # "SHORT" 或 "LONG"
# 2. 方向判断并设置 self.start方便后续策略使用
if position_side == 'SHORT':
direction = ""
self.start = -1
elif position_side == 'LONG':
direction = ""
self.start = 1
else:
pnl_rate = (open_avg_price - current_price) / open_avg_price * 10000
rate_str = f" ({pnl_rate:+.2f}%)"
direction = ""
self.start = 0
# 3. 当前价格从你的K线数据
current_price = float(self.kline_3["close"])
# 4. 持仓量(假设当前持仓等于这笔成交量,如有加减仓后续可维护累计)
current_amount = fill_size # 单位ETH
# 5. 计算浮动盈亏USDT
if self.start == 1: # 多头
unrealized_pnl = current_amount * (current_price - open_avg_price)
elif self.start == -1: # 空头
unrealized_pnl = current_amount * (open_avg_price - current_price)
else:
unrealized_pnl = 0.0
# 6. 收益率
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 = ""
pnl_str = f"{unrealized_pnl:+.2f} USDT"
# 7. 当前持仓名义价值
current_value = current_amount * current_price
# 8. 持仓量显示
amount_display = f"{current_amount:.3f} ETH"
# 9. 组装消息钉钉Markdown格式更美观
message_content = (
"**【WEEX ETHUSDT 永续持仓监控】**\n\n"
f"**持仓方向**{direction}\n"
f"**当前现价**{current_price:.2f} USDT\n"
f"**开仓均价**{open_avg_price:.2f} USDT\n"
f"**持仓数量(eth)**{amount_display} eth\n"
f"**持仓数量(usdt)**{float(self.datas['fillValue']) / 100:.2f} usdt\n"
f"**名义价值**{current_value:.2f} USDT\n"
f"**浮动盈亏**{pnl_str}{rate_str}\n"
f"**账户可用余额**{float(num):.2f} USDT"
)
else:
rate_str = ""
pnl_str = f"{unrealized_pnl:+.2f} USDT"
# 7. 当前持仓名义价值
current_value = current_amount * current_price
# 8. 持仓量显示
amount_display = f"{current_amount:.3f} ETH"
# 9. 组装消息钉钉Markdown格式更美观
message_content = (
"**【WEEX ETHUSDT 永续持仓监控】**\n\n"
f"**持仓方向**{direction}\n"
f"**当前现价**{current_price:.2f} USDT\n"
f"**开仓均价**{open_avg_price:.2f} USDT\n"
f"**持仓数量**{amount_display}\n"
f"**名义价值**{current_value:.2f} USDT\n"
f"**浮动盈亏**{pnl_str}{rate_str}\n"
f"**账户可用余额**{float(num):.2f} USDT"
)
message_content = (
"**【WEEX ETHUSDT 永续持仓监控】**\n\n"
f"**持仓方向**:无\n"
f"**当前现价**{current_price:.2f} USDT\n"
# f"**开仓均价**{open_avg_price:.2f} USDT\n"
# f"**持仓数量**{amount_display}\n"
# f"**名义价值**{current_value:.2f} USDT\n"
# f"**浮动盈亏**{pnl_str}{rate_str}\n"
f"**账户可用余额**{float(num):.2f} USDT"
)
# 10. 发送钉钉消息
self.send_dingtalk_message(message_content=message_content)
if __name__ == '__main__':
WeexTransaction(
tge_id=146473,