|
|
@@ -400,7 +400,7 @@ def calculate_percentage_diff(price_a_base_per_target, price_b_base_per_target):
|
|
|
return rst
|
|
|
return None
|
|
|
|
|
|
-def send_arb_msg(profit_amount, chain_swap_data, mexc_price_usdt_per_target, in_amount_to_query_human):
|
|
|
+def send_arb_msg(profit_amount, chain_swap_data, mexc_price_usdt_per_target, in_amount_copy):
|
|
|
# chain_swap_data 是从 get_chain_price_vs_target_currency 返回的第二个值
|
|
|
if not (chain_swap_data and chain_swap_data.get('data') and chain_swap_data['data']):
|
|
|
logger.error(f"套利消息发送失败:链上交易数据不完整 {chain_swap_data}")
|
|
|
@@ -425,7 +425,7 @@ def send_arb_msg(profit_amount, chain_swap_data, mexc_price_usdt_per_target, in_
|
|
|
# "mexcPriceUsdtPerTarget": str(mexc_price_usdt_per_target.quantize(Decimal('1e-8'))),
|
|
|
"symbol": MEXC_TARGET_PAIR_USDT,
|
|
|
"fromToken": IN_TOKEN_ADDRESS,
|
|
|
- "fromTokenAmountHuman": str(in_amount_to_query_human.quantize(Decimal(f'1e-{in_dec}'))),
|
|
|
+ "fromTokenAmountHuman": str(in_amount_copy.quantize(Decimal(f'1e-{in_dec}'))),
|
|
|
"fromTokenDecimal": str(in_dec),
|
|
|
"toToken": OUT_TOKEN_ADDRESS,
|
|
|
"toTokenAmountHuman": str(human_out_target.quantize(Decimal(f'1e-{out_dec}'))),
|
|
|
@@ -466,10 +466,6 @@ def update_data_for_plotly_and_table():
|
|
|
if mexc_price_target_per_usdt_bid is not None and mexc_price_target_per_usdt_bid > 0:
|
|
|
mexc_price_usdt_per_target_bid1_for_calc = mexc_price_target_per_usdt_bid # RATO/USDT => USDT/TARGET (命名约定)
|
|
|
|
|
|
- global in_amount_to_query_human
|
|
|
- with in_amount_lock:
|
|
|
- in_amount_to_query_human = mexc_price_usdt_per_target_bid1_for_calc
|
|
|
-
|
|
|
elif not mexc_err and mexc_price_target_per_usdt_bid is not None:
|
|
|
mexc_err = mexc_err or "MEXC价格为0或无效"
|
|
|
|
|
|
@@ -485,13 +481,18 @@ def update_data_for_plotly_and_table():
|
|
|
# 2. 确定链上查询的输入金额 (USDT)
|
|
|
# 使用 MEXC 卖出 EXCHANGE_OUT_AMOUNT 个目标币能得到的USDT数量 (trade_value_usdt)
|
|
|
# 作为链上购买目标币时花费的USDT数量 (in_amount_to_query_human)
|
|
|
- in_amount_to_query_human = trade_value_usdt.quantize(Decimal('1e-2'), rounding=ROUND_DOWN) # 保留两位小数
|
|
|
- if in_amount_to_query_human <= Decimal('0'):
|
|
|
- logger.warning(f"计算出的链上查询金额为0或负数 ({in_amount_to_query_human} USDT),跳过。trade_value_usdt: {trade_value_usdt}")
|
|
|
+ # 交易所賣了多少
|
|
|
+ global in_amount_to_query_human
|
|
|
+ with in_amount_lock:
|
|
|
+ in_amount_to_query_human = trade_value_usdt.quantize(Decimal('1e-2'), rounding=ROUND_DOWN) # 保留两位小数
|
|
|
+
|
|
|
+ in_amount_copy = Decimal(str(in_amount_to_query_human))
|
|
|
+ if in_amount_copy <= Decimal('0'):
|
|
|
+ logger.warning(f"计算出的链上查询金额为0或负数 ({in_amount_copy} USDT),跳过。trade_value_usdt: {trade_value_usdt}")
|
|
|
time.sleep(REFRESH_INTERVAL_SECONDS)
|
|
|
return
|
|
|
|
|
|
- # 3. 获取链上价格:用 in_amount_to_query_human 这么多的USDT去买目标币,能买到多少,以及价格 (USDT/TARGET)
|
|
|
+ # 3. 获取链上价格:用 in_amount_copy 这么多的USDT去买目标币,能买到多少,以及价格 (USDT/TARGET)
|
|
|
oo_data, chain_swap_full_response = get_chain_price_vs_target_currency()
|
|
|
oo_price_usdt_per_target = oo_data.get("price_base_per_target") # USDT/TARGET
|
|
|
oo_err = oo_data.get("error")
|
|
|
@@ -505,18 +506,18 @@ def update_data_for_plotly_and_table():
|
|
|
|
|
|
# 5. 计算实际利润额 (以USDT计价)
|
|
|
# 利润 = (MEXC每目标币卖价 - 链上每目标币买价) * 链上买入的目标币数量
|
|
|
- # 链上买入的目标币数量 = in_amount_to_query_human / oo_price_usdt_per_target
|
|
|
+ # 链上买入的目标币数量 = in_amount_copy / oo_price_usdt_per_target
|
|
|
# 简化:利润百分比 * 投入的USDT金额
|
|
|
actual_profit_usdt = None
|
|
|
if diff_oo_vs_mexc_bid1_pct is not None and oo_price_usdt_per_target is not None and oo_price_usdt_per_target > 0:
|
|
|
# 方案A: 基于百分比和投入金额
|
|
|
- actual_profit_usdt = diff_oo_vs_mexc_bid1_pct * in_amount_to_query_human
|
|
|
+ actual_profit_usdt = diff_oo_vs_mexc_bid1_pct * in_amount_copy
|
|
|
|
|
|
# 6. 满足利润条件,发送套利消息, PROFIT_LIMIT + 3的3是提前計算的成本,否則一直提交
|
|
|
global mode
|
|
|
if actual_profit_usdt is not None and actual_profit_usdt > PROFIT_LIMIT + 3 and mode == 'trade':
|
|
|
if chain_swap_full_response: # 确保有完整的链上数据
|
|
|
- send_arb_msg(actual_profit_usdt, chain_swap_full_response, mexc_price_usdt_per_target_bid1_for_calc, in_amount_to_query_human)
|
|
|
+ send_arb_msg(actual_profit_usdt, chain_swap_full_response, mexc_price_usdt_per_target_bid1_for_calc, in_amount_copy)
|
|
|
else:
|
|
|
logger.warning("利润满足但链上数据不完整,无法发送套利消息。")
|
|
|
|
|
|
@@ -542,7 +543,7 @@ def update_data_for_plotly_and_table():
|
|
|
latest_values_for_table["oo_error"] = oo_err
|
|
|
latest_values_for_table["mexc_error"] = mexc_err
|
|
|
latest_values_for_table["last_updated"] = fetch_time_full
|
|
|
- latest_values_for_table["in_amount_for_query_display"] = f"{in_amount_to_query_human:.2f} {BASE_CURRENCY_SYMBOL}" if in_amount_to_query_human > 0 else "N/A"
|
|
|
+ latest_values_for_table["in_amount_for_query_display"] = f"{in_amount_copy:.2f} {BASE_CURRENCY_SYMBOL}" if in_amount_copy > 0 else "N/A"
|
|
|
|
|
|
# if oo_err or mexc_err :
|
|
|
# logger.warning(f"{fetch_time_chart} Errors: OO:{oo_err}, MEXC:{mexc_err}")
|