Browse Source

再次修复回补逻辑

skyfffire 3 months ago
parent
commit
8ef3e1fd48
1 changed files with 19 additions and 12 deletions
  1. 19 12
      s_erc20_to_mexc.py

+ 19 - 12
s_erc20_to_mexc.py

@@ -431,6 +431,7 @@ class ArbitrageProcess:
             exchange_buy_order = None
             order_error_times = 0
             order_price = Decimal(0)
+            bid1_price = Decimal(0)
             self.already_bought_amount = Decimal(0)
 
             while order_error_times < 10:
@@ -448,10 +449,9 @@ class ArbitrageProcess:
                         continue
 
                     bid1_price = Decimal(depth['bids'][0][0])
-                    order_price = bid1_price
 
                     # 准备购入的价值, 如果小于2u就不要提交了
-                    pseudo_value_to_buy = order_price * (self.already_sold_amount - self.already_bought_amount)
+                    pseudo_value_to_buy = bid1_price * (self.already_sold_amount - self.already_bought_amount)
 
                     if pseudo_value_to_buy < 2:
                         break
@@ -479,7 +479,7 @@ class ArbitrageProcess:
                                         break
 
                         # 实际能购入的数量(可能会亏损导致买不回来, 所以要考虑实际有多少money)
-                        quantity_for_api = pseudo_value_to_buy / order_price
+                        quantity_for_api = pseudo_value_to_buy / bid1_price
                         quantity_for_api = quantity_for_api.quantize(self.coin_asset_precision, rounding=ROUND_DOWN)
                         # 用求余法判断是否是整数
                         if quantity_for_api % 1 == 0:
@@ -493,7 +493,7 @@ class ArbitrageProcess:
                             "symbol": self.symbol.replace('_', ''),
                             "side": "BUY",
                             "type": "LIMIT",
-                            "price": order_price,
+                            "price": bid1_price,
                             "quantity": quantity_for_api,
                         }
                         order_params_formated = pformat(order_params, indent=2)
@@ -507,9 +507,9 @@ class ArbitrageProcess:
 
                             exchange_buy_order = None
                             order_error_times = order_error_times + 1
-                        
-                        self.exchange_buy_order_id = exchange_buy_order['orderId']
-                    
+                        else:
+                            self.exchange_buy_order_id = exchange_buy_order['orderId']
+                            order_price = bid1_price
                     # 有订单时的逻辑
                     else:
                         # 获取订单状态,直到完全成交或超时
@@ -532,7 +532,7 @@ class ArbitrageProcess:
                             exchange_buy_order = None
                         
                         # 如果没有成交或取消则判断是否达到取消条件了,这里面不能置空
-                        else:
+                        elif order_price != bid1_price:
                             params = {
                                 "symbol": self.symbol.replace('_', ''),
                                 "orderId": self.exchange_buy_order_id
@@ -548,12 +548,19 @@ class ArbitrageProcess:
                     msg = f"请求价格接口时出现错误\n{exc_traceback}"
                     logger.error(msg)
             
-            # 卖值, 买值, 差量
+            # diff 仍然代表未买回的数量,非常重要,需要记录
             diff = self.already_sold_amount - self.already_bought_amount
-            diff_value = diff * order_price
 
-            profit = (self.sell_value - self.buy_value) - diff_value
-            msg = f"套利流程完成, 最终利润{profit}, 卖值{self.sell_value}, 买值{self.buy_value}, 差量{diff}({diff_value})"
+            # 已实现的利润 = 总卖出额 - 总买入额
+            realized_profit = self.sell_value - self.buy_value
+
+            if diff > 0:
+                # 如果有未买回的部分,将其与最后一次的市价相乘,作为 "浮动亏损" 或 "未平仓成本" 单独记录
+                unrealized_cost = diff * bid1_price # 使用最后一次获取到的市价 bid1_price 更合适
+                msg = f"套利流程完成, 但有 {diff} 的数量未回补。已实现利润: {realized_profit}, 未平仓成本估算: {unrealized_cost} (基于价格 {bid1_price})"
+            else:
+                msg = f"套利流程完成, 全部回补。最终利润: {realized_profit}, 总卖值: {self.sell_value}, 总买值: {self.buy_value}"
+
             logger.info(msg)
             add_state_flow_entry(self.process_item, self.current_state, msg, "success")