ソースを参照

平仓距离可以设置、价差变动了0.0005了才重新挂单

skyfffire 2 ヶ月 前
コミット
2e90399e19
3 ファイル変更20 行追加18 行削除
  1. 14 14
      checker/c_mexc_to_erc20.py
  2. 2 2
      s_erc20_to_mexc.py
  3. 4 2
      toto.readme

+ 14 - 14
checker/c_mexc_to_erc20.py

@@ -266,7 +266,7 @@ def update_data_for_plotly_and_table():
         fetch_time_chart = time.strftime("%H:%M:%S")
         fetch_time_chart = time.strftime("%H:%M:%S")
 
 
         # 1. 获取链上价格:卖出 DEX_QUERRY_AMOUNT 这么多的TARGET去卖成USDT,能卖到多少,以及价格 (USDT/TARGET)
         # 1. 获取链上价格:卖出 DEX_QUERRY_AMOUNT 这么多的TARGET去卖成USDT,能卖到多少,以及价格 (USDT/TARGET)
-        erc20_data, chain_swap_full_response, human_out_base = get_dex_price_vs_target_currency(
+        dex_data, chain_swap_full_response, human_out_base = get_dex_price_vs_target_currency(
             CHAIN_ID,
             CHAIN_ID,
             IN_TOKEN_ADDRESS,      # TARGET
             IN_TOKEN_ADDRESS,      # TARGET
             OUT_TOKEN_ADDRESS,     # USDT
             OUT_TOKEN_ADDRESS,     # USDT
@@ -276,8 +276,8 @@ def update_data_for_plotly_and_table():
             USER_WALLET,
             USER_WALLET,
             USER_EXCHANGE_WALLET
             USER_EXCHANGE_WALLET
         )
         )
-        dex_price = erc20_data.get("price_base_per_target") # USDT/TARGET
-        erc20_err = erc20_data.get("error")
+        dex_price = dex_data.get("price_base_per_target") # USDT/TARGET
+        dex_err = dex_data.get("error")
 
 
         # 2. CEX: 获取 price_target_per_base (例如 RATO/USDT)
         # 2. CEX: 获取 price_target_per_base (例如 RATO/USDT)
         # trade_value_usdt 是指如果以 EXCHANGE_OUT_AMOUNT 的目标代币在CEX上砸盘卖出,能获得的USDT估值
         # trade_value_usdt 是指如果以 EXCHANGE_OUT_AMOUNT 的目标代币在CEX上砸盘卖出,能获得的USDT估值
@@ -304,7 +304,7 @@ def update_data_for_plotly_and_table():
 
 
         # 3. 计算百分比差异
         # 3. 计算百分比差异
         # diff = (链上卖价 - CEX买价) / CEX买价
         # diff = (链上卖价 - CEX买价) / CEX买价
-        diff_erc20_vs_cex_pct = calculate_percentage_diff(
+        diff_dex_vs_cex_pct = calculate_percentage_diff(
             dex_price,                   # 链上卖价 (USDT/TARGET)
             dex_price,                   # 链上卖价 (USDT/TARGET)
             cex_price,                 # CEX买价 (USDT/TARGET)
             cex_price,                 # CEX买价 (USDT/TARGET)
         )
         )
@@ -312,16 +312,16 @@ def update_data_for_plotly_and_table():
         # 4. 计算实际利润额 (以USDT计价)
         # 4. 计算实际利润额 (以USDT计价)
         # 简化:利润百分比 * 投入的USDT金额
         # 简化:利润百分比 * 投入的USDT金额
         actual_profit_usdt = None
         actual_profit_usdt = None
-        if diff_erc20_vs_cex_pct is not None and dex_price is not None and dex_price > 0:
+        if diff_dex_vs_cex_pct is not None and dex_price is not None and dex_price > 0:
             # 基于百分比和投入金额
             # 基于百分比和投入金额
-            actual_profit_usdt = diff_erc20_vs_cex_pct * human_out_base
+            actual_profit_usdt = diff_dex_vs_cex_pct * human_out_base
 
 
         # 5. 满足利润条件,发送套利消息, PROFIT_LIMIT + 3的3是提前計算的成本,否則一直提交
         # 5. 满足利润条件,发送套利消息, PROFIT_LIMIT + 3的3是提前計算的成本,否則一直提交
         global mode
         global mode
-        if diff_erc20_vs_cex_pct is not None and diff_erc20_vs_cex_pct > OPEN_LIMIT and mode == 'trade' and count >= 120:
+        if diff_dex_vs_cex_pct is not None and diff_dex_vs_cex_pct > OPEN_LIMIT and mode == 'trade' and count >= 120:
             if chain_swap_full_response: # 确保有完整的链上数据
             if chain_swap_full_response: # 确保有完整的链上数据
                 human_in_base = DEX_QUERRY_AMOUNT
                 human_in_base = DEX_QUERRY_AMOUNT
-                send_arb_msg(diff_erc20_vs_cex_pct, chain_swap_full_response, cex_price, dex_price)
+                send_arb_msg(diff_dex_vs_cex_pct, chain_swap_full_response, cex_price, dex_price)
             else:
             else:
                 logger.warning("利润满足但链上数据不完整,无法发送套利消息。")
                 logger.warning("利润满足但链上数据不完整,无法发送套利消息。")
 
 
@@ -329,7 +329,7 @@ def update_data_for_plotly_and_table():
             "time": fetch_time_chart,
             "time": fetch_time_chart,
             "dex_price": float(dex_price) if dex_price else None,
             "dex_price": float(dex_price) if dex_price else None,
             "cex_price": float(cex_price) if cex_price else None,
             "cex_price": float(cex_price) if cex_price else None,
-            "diff_erc20_vs_cex": float(diff_erc20_vs_cex_pct) if diff_erc20_vs_cex_pct is not None else None,
+            "diff_dex_vs_cex": float(diff_dex_vs_cex_pct) if diff_dex_vs_cex_pct is not None else None,
             "profit_value": float(actual_profit_usdt) if actual_profit_usdt is not None else None, # 新增:用于图表的实际利润额
             "profit_value": float(actual_profit_usdt) if actual_profit_usdt is not None else None, # 新增:用于图表的实际利润额
         }
         }
 
 
@@ -337,16 +337,16 @@ def update_data_for_plotly_and_table():
             historical_data_points.append(current_point)
             historical_data_points.append(current_point)
             latest_values_for_table["dex_price"] = f"{dex_price:.8f}" if dex_price else "N/A"
             latest_values_for_table["dex_price"] = f"{dex_price:.8f}" if dex_price else "N/A"
             latest_values_for_table["cex_price"] = f"{cex_price:.8f}" if cex_price else "N/A"
             latest_values_for_table["cex_price"] = f"{cex_price:.8f}" if cex_price else "N/A"
-            latest_values_for_table["diff_dex_vs_cex_percentage"] = f"{diff_erc20_vs_cex_pct:+.4%}" if diff_erc20_vs_cex_pct is not None else "N/A" # 显示为百分比
+            latest_values_for_table["diff_dex_vs_cex_percentage"] = f"{diff_dex_vs_cex_pct:+.4%}" if diff_dex_vs_cex_pct is not None else "N/A" # 显示为百分比
             latest_values_for_table["profit_value_for_table"] = f"{actual_profit_usdt:.2f} {BASE_CURRENCY_SYMBOL}" if actual_profit_usdt is not None else "N/A" # 新增
             latest_values_for_table["profit_value_for_table"] = f"{actual_profit_usdt:.2f} {BASE_CURRENCY_SYMBOL}" if actual_profit_usdt is not None else "N/A" # 新增
-            latest_values_for_table["dex_error"] = erc20_err
+            latest_values_for_table["dex_error"] = dex_err
             latest_values_for_table["cex_error"] = cex_err
             latest_values_for_table["cex_error"] = cex_err
             latest_values_for_table["last_updated"] = fetch_time_full
             latest_values_for_table["last_updated"] = fetch_time_full
             latest_values_for_table["in_amount_for_query_display"] = f"{human_out_base:.2f} {BASE_CURRENCY_SYMBOL}" if human_out_base > 0 else "N/A"
             latest_values_for_table["in_amount_for_query_display"] = f"{human_out_base:.2f} {BASE_CURRENCY_SYMBOL}" if human_out_base > 0 else "N/A"
 
 
         # logger.info(f"{fetch_time_chart} Price Query: Chain Input {human_out_base:.2f} {BASE_CURRENCY_SYMBOL} | OKX Price: {dex_price_display} | CEX Price: {cex_price_display} | Diff: {diff_display} | Profit: {profit_display}")
         # logger.info(f"{fetch_time_chart} Price Query: Chain Input {human_out_base:.2f} {BASE_CURRENCY_SYMBOL} | OKX Price: {dex_price_display} | CEX Price: {cex_price_display} | Diff: {diff_display} | Profit: {profit_display}")
-        if erc20_err or cex_err :
-             logger.warning(f"{fetch_time_chart} Errors: erc20:{erc20_err}, CEX:{cex_err}")
+        if dex_err or cex_err :
+             logger.warning(f"{fetch_time_chart} Errors: dex:{dex_err}, CEX:{cex_err}")
 
 
         time.sleep(REFRESH_INTERVAL_SECONDS)
         time.sleep(REFRESH_INTERVAL_SECONDS)
 
 
@@ -412,7 +412,7 @@ def get_plotly_chart_data():
         # Percentage Difference Chart
         # Percentage Difference Chart
         fig_diffs = go.Figure()
         fig_diffs = go.Figure()
         fig_diffs.add_trace(
         fig_diffs.add_trace(
-            go.Scatter(x=times, y=[p['diff_erc20_vs_cex'] for p in points], mode='lines', name=f'价差百分比 (CEX卖价 vs 链上买价)',
+            go.Scatter(x=times, y=[p['diff_dex_vs_cex'] for p in points], mode='lines', name=f'价差百分比 (CEX卖价 vs 链上买价)',
                        line=dict(color='rgb(255, 159, 64)'),
                        line=dict(color='rgb(255, 159, 64)'),
                        hovertemplate=f'<b>(CEX卖价-链上买价)/链上买价</b><br>百分比: %{{y:+.4%}}<extra></extra>', # 显示为百分比
                        hovertemplate=f'<b>(CEX卖价-链上买价)/链上买价</b><br>百分比: %{{y:+.4%}}<extra></extra>', # 显示为百分比
                        connectgaps=True))
                        connectgaps=True))

+ 2 - 2
s_erc20_to_mexc.py

@@ -410,7 +410,7 @@ class ArbitrageProcess:
                     dex_price = Decimal(table_data['dex_price'])
                     dex_price = Decimal(table_data['dex_price'])
                     dex_price = dex_price.quantize(self.price_precision, rounding=ROUND_DOWN)
                     dex_price = dex_price.quantize(self.price_precision, rounding=ROUND_DOWN)
 
 
-                    ready_order_price = dex_price # + self.price_precision
+                    ready_order_price = dex_price * (Decimal(1) - self.close_limit)
 
 
                     # 准备购入的价值, 如果小于2u就不要提交了
                     # 准备购入的价值, 如果小于2u就不要提交了
                     pseudo_value_to_buy = ready_order_price * (self.already_sold_amount - self.already_bought_amount)
                     pseudo_value_to_buy = ready_order_price * (self.already_sold_amount - self.already_bought_amount)
@@ -499,7 +499,7 @@ class ArbitrageProcess:
                             exchange_buy_order = None
                             exchange_buy_order = None
                         
                         
                         # 如果没有成交或取消则判断是否达到取消条件了, 这里面不能置空
                         # 如果没有成交或取消则判断是否达到取消条件了, 这里面不能置空
-                        elif order_price != dex_price:
+                        elif (Decimal(1) - order_price / ready_order_price) > Decimal(0.0005):
                             params = {
                             params = {
                                 "symbol": self.symbol.replace('_', ''),
                                 "symbol": self.symbol.replace('_', ''),
                                 "orderId": self.exchange_buy_order_id
                                 "orderId": self.exchange_buy_order_id

+ 4 - 2
toto.readme

@@ -24,7 +24,9 @@
 [-] reject不要放进日志去了
 [-] reject不要放进日志去了
 [-] 优化前端monitor,用表格形式展示,总利润加一下进去
 [-] 优化前端monitor,用表格形式展示,总利润加一下进去
 
 
+2025-08-26
+[-] 平仓逻辑
+[-] 价差变动了0.0005了才重新挂单
+
 待定
 待定
-[ ] 平仓逻辑
 [ ] 日志细化
 [ ] 日志细化
-[ ] 价差变动了0.0005了才重新挂单