From 86b4616246e93f94dde268252d9912d84ee4ea43 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 17 Dec 2025 18:10:48 +0800 Subject: [PATCH] =?UTF-8?q?bitmart=E4=BC=98=E5=8C=96=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- telegram/8619211027341.session | Bin 45056 -> 45056 bytes telegram/bot_session.session | Bin 40960 -> 40960 bytes 交易/bitmart_api交易.py | 91 +++++++++++++--------- 交易/websea_交易.py | 50 +++++++----- 交易/weex_交易.py | 138 ++++++++++++++++++--------------- 5 files changed, 159 insertions(+), 120 deletions(-) diff --git a/telegram/8619211027341.session b/telegram/8619211027341.session index e26a093931c123f3ec8ee5135a200f4c4f44100e..df12d0b6b0a3f1af25cb5232cf5dec5db5f720ed 100644 GIT binary patch delta 64 zcmV-G0Kfl$-~xc)0+1U4;E^0d0pPJ$<;ipV^fo8Wlx7CX KY_6Zv$P54y>nrg9 diff --git a/交易/bitmart_api交易.py b/交易/bitmart_api交易.py index bf3ab5c..0055b4d 100644 --- a/交易/bitmart_api交易.py +++ b/交易/bitmart_api交易.py @@ -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) diff --git a/交易/websea_交易.py b/交易/websea_交易.py index 55fa7bf..db6ad10 100644 --- a/交易/websea_交易.py +++ b/交易/websea_交易.py @@ -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 diff --git a/交易/weex_交易.py b/交易/weex_交易.py index 8efeb8f..ef0c4dc 100644 --- a/交易/weex_交易.py +++ b/交易/weex_交易.py @@ -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,