|
|
@@ -25,25 +25,22 @@ class ArbitrageProcess:
|
|
|
Args:
|
|
|
process_item: 信號發送端傳入的原始參數
|
|
|
"""
|
|
|
- self.NETWORK = 'ETH'
|
|
|
-
|
|
|
|
|
|
|
|
|
'''
|
|
|
process_item
|
|
|
{
|
|
|
'cexPrice': '0.0104700000',
|
|
|
- 'closeLimit': '-1.000',
|
|
|
'dexPrice': '0.01030220134289945035466016829',
|
|
|
- 'exchangeOutAmount': '3000.000000000000000000',
|
|
|
- 'openLimit': '0.000',
|
|
|
'pct': '0',
|
|
|
+ 'closeLimit': '-1.000',
|
|
|
+ 'openLimit': '0.000',
|
|
|
+ 'exchangeOutAmount': '3000.000000000000000000',
|
|
|
'queryPriceUrl': '127.0.0.1:7777/table-data',
|
|
|
'strategy': 'erc20_to_mexc',
|
|
|
'symbol': 'APETH_USDT'
|
|
|
}
|
|
|
'''
|
|
|
-
|
|
|
self.core_data = core_data
|
|
|
self.core_lock = core_lock
|
|
|
self.mexc_data = mexc_data
|
|
|
@@ -51,46 +48,27 @@ class ArbitrageProcess:
|
|
|
|
|
|
# symbol轉大寫
|
|
|
self.symbol = process_item['symbol'].upper()
|
|
|
-
|
|
|
self.coin = self.symbol.split('_')[0]
|
|
|
self.base_coin = self.symbol.split('_')[1]
|
|
|
-
|
|
|
- # 获取eth价格
|
|
|
- with self.core_lock:
|
|
|
- self.eth_price = self.core_data['eth_price']
|
|
|
-
|
|
|
- # 获取提现信息
|
|
|
- with self.mexc_lock:
|
|
|
- self.withdraw_info = copy.deepcopy(self.mexc_data['coin_info_map'][self.base_coin][self.NETWORK])
|
|
|
- self.WITHDRAW_FEE = Decimal(self.withdraw_info['withdrawFee']) # 提現手續費
|
|
|
-
|
|
|
- withdraw_info_formated = pformat(self.withdraw_info, indent=2)
|
|
|
- logger.info(f'提現信息識別, 手續費:{self.WITHDRAW_FEE}\n{withdraw_info_formated}')
|
|
|
-
|
|
|
- self.tx = tx
|
|
|
|
|
|
- self.profit = Decimal(process_item['profit']) # 這個利潤是實際到手利潤
|
|
|
- self.profit_limit = Decimal(process_item['profitLimit']) # 這個利潤是實際到手利潤的limit
|
|
|
-
|
|
|
- self.gas_limit_multiplier = gas_limit_multiplier
|
|
|
- self.gas_price_multiplier = gas_price_multiplier
|
|
|
-
|
|
|
- self.from_token_addr = process_item['fromToken']
|
|
|
- self.from_token_decimal = Decimal(process_item['fromTokenDecimal'])
|
|
|
- self.to_token_addr = process_item['toToken']
|
|
|
- self.to_token_decimal = Decimal(process_item['toTokenDecimal'])
|
|
|
-
|
|
|
- self.user_exchange_wallet = process_item['userExchangeWallet']
|
|
|
- self.user_wallet = process_item['userWallet']
|
|
|
+ # 其它参数
|
|
|
+ self.cex_price = Decimal(process_item['cexPrice'])
|
|
|
+ self.dex_price = Decimal(process_item['dexPrice'])
|
|
|
+ self.pct = Decimal(process_item['pct'])
|
|
|
+ self.open_limit = Decimal(process_item['openLimit'])
|
|
|
+ self.close_limit = Decimal(process_item['closeLimit'])
|
|
|
+ self.query_price_url = Decimal(process_item['queryPriceUrl'])
|
|
|
+
|
|
|
+ # 留档
|
|
|
self.process_item = process_item
|
|
|
|
|
|
-
|
|
|
# 存储当前套利交易的细节信息,例如买入数量、价格等
|
|
|
- self.sell_price = Decimal(0)
|
|
|
+ self.sell_amount = Decimal(process_item['exchangeOutAmount']) # 交易所卖出量
|
|
|
+ self.sell_price = Decimal(0) # 实际卖出价格
|
|
|
self.sell_value = Decimal(0) # 实际卖出价值
|
|
|
self.buy_price = Decimal(0)
|
|
|
- self.chain_tx_hash = None # 链上买入的tx hash
|
|
|
- self.exchange_sell_amount = Decimal(process_item['exchangeOutAmount']) # 交易所卖出量
|
|
|
+ self.buy_amount = Decimal(0)
|
|
|
+ self.buy_value = Decimal(0)
|
|
|
self.actual_profit = Decimal(0) # 實際利潤
|
|
|
|
|
|
# 定义可能的状态
|
|
|
@@ -194,7 +172,7 @@ class ArbitrageProcess:
|
|
|
try:
|
|
|
# step1,檢查交易所的餘額是否夠用
|
|
|
# 处理精度
|
|
|
- pseudo_amount_to_sell = self.exchange_sell_amount.quantize(self.coin_asset_precision, rounding=ROUND_DOWN)
|
|
|
+ pseudo_amount_to_sell = self.sell_amount.quantize(self.coin_asset_precision, rounding=ROUND_DOWN)
|
|
|
|
|
|
# 交易所套保余额判断
|
|
|
with self.mexc_lock:
|
|
|
@@ -293,10 +271,10 @@ class ArbitrageProcess:
|
|
|
try:
|
|
|
order_times = 0
|
|
|
self.already_sold_amount = Decimal(0)
|
|
|
- while self.already_sold_amount < self.exchange_sell_amount and order_times < 5:
|
|
|
+ while self.already_sold_amount < self.sell_amount and order_times < 5:
|
|
|
order_times = order_times + 1
|
|
|
# 第一步直接卖出,这个数量用固定数量
|
|
|
- pseudo_amount_to_sell = self.exchange_sell_amount - self.already_sold_amount
|
|
|
+ pseudo_amount_to_sell = self.sell_amount - self.already_sold_amount
|
|
|
# 处理精度
|
|
|
pseudo_amount_to_sell = pseudo_amount_to_sell.quantize(self.coin_asset_precision, rounding=ROUND_DOWN)
|
|
|
# 初始化 quantity 变量
|
|
|
@@ -496,7 +474,7 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
# 判斷快速二賣條件
|
|
|
- diff = to_token_amount_human - self.exchange_sell_amount
|
|
|
+ diff = to_token_amount_human - self.sell_amount
|
|
|
diff = diff.quantize(self.coin_asset_precision, rounding=ROUND_DOWN)
|
|
|
|
|
|
value = diff * self.sell_price
|
|
|
@@ -647,8 +625,8 @@ class ArbitrageProcess:
|
|
|
break
|
|
|
|
|
|
# 有可能会遇到手续费占用问题
|
|
|
- ready_to_buy = self.exchange_sell_amount
|
|
|
- if self.exchange_sell_amount * self.sell_price * Decimal(1.002) >= free_balance:
|
|
|
+ ready_to_buy = self.sell_amount
|
|
|
+ if self.sell_amount * self.sell_price * Decimal(1.002) >= free_balance:
|
|
|
ready_to_buy = (free_balance * Decimal(0.998)) / self.sell_price
|
|
|
ready_to_buy = ready_to_buy.quantize(self.coin_asset_precision, rounding=ROUND_DOWN)
|
|
|
|
|
|
@@ -698,7 +676,7 @@ class ArbitrageProcess:
|
|
|
|
|
|
if order['status'] == "FILLED":
|
|
|
money = Decimal(order['cummulativeQuoteQty'])
|
|
|
- amount = self.exchange_sell_amount
|
|
|
+ amount = self.sell_amount
|
|
|
price = money / amount
|
|
|
price = price.quantize(self.price_precision, rounding=ROUND_DOWN)
|
|
|
|