From c19ae00e84fb085a4c90749b867a160a95766e24 Mon Sep 17 00:00:00 2001 From: 27942 <1313123@342> Date: Fri, 6 Feb 2026 00:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=8C=E7=BE=8E=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=8A=A0=E5=85=A5=E6=AD=A2=E7=9B=88=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bitmart/四分之一,五分钟,反手条件充足.py | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/bitmart/四分之一,五分钟,反手条件充足.py b/bitmart/四分之一,五分钟,反手条件充足.py index 280e993..4ffac36 100644 --- a/bitmart/四分之一,五分钟,反手条件充足.py +++ b/bitmart/四分之一,五分钟,反手条件充足.py @@ -38,6 +38,7 @@ class BitmartFuturesTransaction: self.leverage = "100" # 高杠杆(全仓模式下可开更大仓位) self.open_type = "cross" # 全仓模式 self.risk_percent = 0.01 # 每次开仓使用可用余额的 1% + self.take_profit_usd = 5 # 仓位盈利达到此金额(美元)时平仓止盈 self.open_avg_price = None # 开仓价格 self.current_amount = None # 持仓量 @@ -125,10 +126,12 @@ class BitmartFuturesTransaction: positions = response['data'] if not positions: self.start = 0 + self.open_avg_price = None + self.current_amount = None return True self.start = 1 if positions[0]['position_type'] == 1 else -1 self.open_avg_price = float(positions[0]['open_avg_price']) - self.current_amount = positions[0]['current_amount'] + self.current_amount = float(positions[0]['current_amount']) self.position_cross = positions[0]["position_cross"] return True else: @@ -137,6 +140,19 @@ class BitmartFuturesTransaction: logger.error(f"持仓查询异常: {e}") return False + def get_unrealized_pnl_usd(self, current_price): + """ + 计算当前持仓未实现盈亏(美元) + 多仓: (当前价 - 开仓均价) * 持仓量(ETH) = USDT + 空仓: (开仓均价 - 当前价) * 持仓量(ETH) = USDT + """ + if self.start == 0 or self.open_avg_price is None or self.current_amount is None: + return None + if self.start == 1: # 多 + return (current_price - self.open_avg_price) * self.current_amount + else: # 空 + return (self.open_avg_price - current_price) * self.current_amount + def set_leverage(self): """程序启动时设置全仓 + 高杠杆""" try: @@ -557,6 +573,15 @@ class BitmartFuturesTransaction: logger.debug(f"当前持仓状态: {self.start} (0=无, 1=多, -1=空)") + # 3.5 止盈:仓位盈利达到 take_profit_usd 则平仓,下一轮循环再根据K线信号开仓 + if self.start != 0: + pnl_usd = self.get_unrealized_pnl_usd(current_price) + if pnl_usd is not None and pnl_usd >= self.take_profit_usd: + logger.info(f"仓位盈利 {pnl_usd:.2f} 美元 >= {self.take_profit_usd} 美元,执行止盈平仓") + self.平仓() + time.sleep(3) + continue # 下一轮循环将无持仓,会按K线信号重新判断是否开仓 + # 4. 检查信号 signal = self.check_signal(current_price, prev_kline, current_kline)