|
|
@@ -74,6 +74,12 @@ historical_data_points = deque(maxlen=MAX_HISTORY_POINTS_PLOTLY)
|
|
|
TARGET_ASSET_SYMBOL = CEX_TARGET_PAIR_USDT.split('_')[0] # e.g., RATO
|
|
|
BASE_CURRENCY_SYMBOL = CEX_TARGET_PAIR_USDT.split('_')[1] # e.g., USDT (assumed to be consistent with IN_TOKEN_ADDRESS)
|
|
|
|
|
|
+# 价差与平均价差
|
|
|
+GAMMA = decimal.Decimal(0.9999)
|
|
|
+GAMMA_1 = decimal.Decimal(0.99)
|
|
|
+spread_ema = decimal.Decimal(0)
|
|
|
+count = decimal.Decimal(0)
|
|
|
+
|
|
|
# --- 链上价格获取函数 (链上) ---
|
|
|
# 返回: price_base_per_target (例如 USDT per RATO)
|
|
|
def get_dex_price_vs_target_currency(chain_id, in_token_addr, out_token_addr, amount_in_base_human, in_token_decimals, slippage, user_wallet_addr, user_exchange_wallet_addr):
|
|
|
@@ -93,7 +99,9 @@ def get_dex_price_vs_target_currency(chain_id, in_token_addr, out_token_addr, am
|
|
|
if human_out_base == 0: return {"error": f"DEX输出目标代币为0 ({CHAIN_ID})"}, data, 0 # data 也返回
|
|
|
|
|
|
# 1target = x usdt,这个价格
|
|
|
- return {"price_base_per_target": human_out_base / human_in_target }, data, human_out_base
|
|
|
+ price = human_out_base / human_in_target
|
|
|
+
|
|
|
+ return {"price_base_per_target": price}, data, human_out_base
|
|
|
else:
|
|
|
pprint.pprint(data)
|
|
|
return {
|
|
|
@@ -277,6 +285,19 @@ def update_data_for_plotly_and_table():
|
|
|
cex_price = cex_data.get("price_target_per_base") # TARGET/USDT
|
|
|
cex_err = cex_data.get("error")
|
|
|
|
|
|
+ # 加入价差,考虑spread重新计算dex_price
|
|
|
+ global spread_ema, count
|
|
|
+ spread = dex_price - cex_price
|
|
|
+ if count < 120:
|
|
|
+ count = count + 1
|
|
|
+ if count == 1:
|
|
|
+ spread_ema = spread
|
|
|
+ else:
|
|
|
+ spread_ema = spread_ema * GAMMA_1 + spread * (decimal.Decimal(1) - GAMMA_1)
|
|
|
+ else:
|
|
|
+ spread_ema = spread_ema * GAMMA + spread * (decimal.Decimal(1) - GAMMA)
|
|
|
+ dex_price = dex_price - spread_ema
|
|
|
+
|
|
|
# 3. 计算百分比差异
|
|
|
# diff = (链上卖价 - CEX买价) / CEX买价
|
|
|
diff_erc20_vs_cex_pct = calculate_percentage_diff(
|
|
|
@@ -293,7 +314,7 @@ def update_data_for_plotly_and_table():
|
|
|
|
|
|
# 5. 满足利润条件,发送套利消息, PROFIT_LIMIT + 3的3是提前計算的成本,否則一直提交
|
|
|
global mode
|
|
|
- if diff_erc20_vs_cex_pct is not None and diff_erc20_vs_cex_pct > OPEN_LIMIT and mode == 'trade':
|
|
|
+ if diff_erc20_vs_cex_pct is not None and diff_erc20_vs_cex_pct > OPEN_LIMIT and mode == 'trade' and count >= 120:
|
|
|
if chain_swap_full_response: # 确保有完整的链上数据
|
|
|
human_in_base = DEX_QUERRY_AMOUNT
|
|
|
send_arb_msg(diff_erc20_vs_cex_pct, chain_swap_full_response, cex_price, dex_price)
|