diff --git a/telegram/8619211027341.session b/telegram/8619211027341.session index 322b4a5..920d918 100644 Binary files a/telegram/8619211027341.session and b/telegram/8619211027341.session differ diff --git a/telegram/bot_session.session b/telegram/bot_session.session index 2de1b81..0bef7fa 100644 Binary files a/telegram/bot_session.session and b/telegram/bot_session.session differ diff --git a/telegram/sign.db b/telegram/sign.db index 99598c3..389b5d8 100644 Binary files a/telegram/sign.db and b/telegram/sign.db differ diff --git a/交易/bitmart_api交易.py b/交易/bitmart_api交易.py index 0c80613..8e83d3c 100644 --- a/交易/bitmart_api交易.py +++ b/交易/bitmart_api交易.py @@ -110,9 +110,16 @@ class BitmartFuturesTransaction: def get_current_price(self): """获取当前最新价格,用于计算张数""" try: - response = self.contractAPI.get_ticker(contract_symbol=self.contract_symbol)[0] + end_time = int(time.time()) + # 获取足够多的条目确保有最新3根 + response = self.contractAPI.get_kline( + contract_symbol=self.contract_symbol, + step=30, # 30分钟 + start_time=end_time - 3600 * 10, # 取最近10小时 + end_time=end_time + )[0] if response['code'] == 1000: - return float(response['data']['last_price']) + return float(response['data'][0]["close_price"]) return None except Exception as e: logger.error(f"获取价格异常: {e}") @@ -161,6 +168,7 @@ class BitmartFuturesTransaction: def calculate_size(self): """计算开仓张数:使用可用余额的1%作为保证金""" balance = self.get_available_balance() + self.balance = balance if not balance or balance < 10: logger.warning("余额不足,无法开仓") return 0 @@ -299,6 +307,13 @@ class BitmartFuturesTransaction: self.execute_trade() self.pbar.reset() + + # =================================================================================================== + size = self.calculate_size() # 获取可用余额 + if not self.get_position_status(): + self.ding(error=True, msg="获取仓位信息失败!!!") + continue + # 持仓方向,开仓价格,现价,持仓量,盈亏,当前价值 # 1. 确保所有关键数据为 float 类型(BitMart API 常返回字符串) current_price = float(kline_3["close"]) @@ -316,9 +331,9 @@ class BitmartFuturesTransaction: # 3. 计算收益率(可选,更直观) if self.start != 0 and open_avg_price > 0: if self.start == 1: - pnl_rate = (current_price - open_avg_price) / open_avg_price * 100 + pnl_rate = (current_price - open_avg_price) / open_avg_price * 10000 else: - pnl_rate = (open_avg_price - current_price) / open_avg_price * 100 + pnl_rate = (open_avg_price - current_price) / open_avg_price * 10000 rate_str = f" ({pnl_rate:+.2f}%)" else: rate_str = "" @@ -327,21 +342,20 @@ class BitmartFuturesTransaction: direction_str = "空" if self.start == -1 else ("多" if self.start == 1 else "无") pnl_str = f"{unrealized_pnl:+.2f} USDT" - # 5. 持仓量显示优化(k为单位) - amount_display = f"{current_amount / 1000:.1f}k 张" if current_amount >= 1000 else f"{current_amount:.0f} 张" - # 6. 最终消息 msg = ( f"【BitMart {self.contract_symbol} 永续】\n" f"当前方向:{direction_str}\n" f"开仓均价:{open_avg_price:.2f} USDT\n" f"当前现价:{current_price:.2f} USDT\n" - f"持仓数量:{amount_display}\n" - f"浮动盈亏:{pnl_str}{rate_str}" + f"持仓量:{float(self.current_amount) / 1000} eth\n" + f"浮动盈亏:{pnl_str}{rate_str}\n" + f"账户可用余额:{self.balance:.2f} usdt" ) # 7. 发送钉钉消息 self.ding(msg=msg) + if __name__ == '__main__': BitmartFuturesTransaction().action()