bitmart优化完成

This commit is contained in:
Administrator
2025-12-17 16:56:48 +08:00
parent 74d5be276b
commit fa6722ea0a
4 changed files with 23 additions and 9 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -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()