|
|
@@ -10,6 +10,7 @@ from decimal import Decimal, ROUND_DOWN
|
|
|
from as_utils import add_state_flow_entry
|
|
|
from checker.logger_config import get_logger
|
|
|
from pprint import pformat
|
|
|
+from pprint import pprint
|
|
|
|
|
|
mexc = MexcClient()
|
|
|
|
|
|
@@ -124,7 +125,7 @@ class ArbitrageProcess:
|
|
|
self.current_state = self.STATE_FAILED
|
|
|
return
|
|
|
|
|
|
- # 精度取得, 假如是RATOUSDT这个交易对的话:
|
|
|
+ # 精度取得, 假如是RATOUSDT这个交易对的话
|
|
|
self.coin_asset_precision = Decimal(f'1e-{exchange_info['baseAssetPrecision']}') # 这是RATO的精度
|
|
|
self.base_coin_asset_precision = Decimal(f'1e-{exchange_info['quoteAssetPrecision']}') # 这是USDT的精度
|
|
|
self.price_precision = Decimal(f'1e-{exchange_info['quotePrecision']}') # 这是价格的精度
|
|
|
@@ -141,11 +142,11 @@ class ArbitrageProcess:
|
|
|
设置系统状态,并打印日志
|
|
|
"""
|
|
|
if state in self.STATES:
|
|
|
- logger.info(f"状态变更:{self.current_state} -> {state}")
|
|
|
+ logger.info(f"状态变更 {self.current_state} -> {state}")
|
|
|
logger.info('')
|
|
|
self.current_state = state
|
|
|
else:
|
|
|
- logger.error(f"尝试设置无效状态:{state}")
|
|
|
+ logger.error(f"尝试设置无效状态 {state}")
|
|
|
|
|
|
def run_arbitrage_step(self):
|
|
|
"""
|
|
|
@@ -240,13 +241,13 @@ class ArbitrageProcess:
|
|
|
for balance in balances:
|
|
|
if balance['asset'] == self.coin:
|
|
|
if Decimal(balance['free']) < pseudo_amount_to_sell:
|
|
|
- msg = f"交易所剩余{self.coin}: {balance['free']}, 交易所准备卖出:{pseudo_amount_to_sell}, 不能触发交易。"
|
|
|
+ msg = f"交易所剩余{self.coin}: {balance['free']}, 交易所准备卖出 {pseudo_amount_to_sell}, 不能触发交易。"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
self._set_state(self.STATE_REJECT)
|
|
|
return
|
|
|
else:
|
|
|
- msg = f"交易所剩余{self.coin}: {balance['free']}, 交易所准备卖出:{pseudo_amount_to_sell}, 余额校验通过(可以交易)。"
|
|
|
+ msg = f"交易所剩余{self.coin}: {balance['free']}, 交易所准备卖出 {pseudo_amount_to_sell}, 余额校验通过(可以交易)。"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
@@ -269,7 +270,7 @@ class ArbitrageProcess:
|
|
|
"""
|
|
|
在中心化交易所卖出现货
|
|
|
"""
|
|
|
- msg = "执行:中心化交易所卖出现货..."
|
|
|
+ msg = "执行 中心化交易所卖出现货..."
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
|
|
|
try:
|
|
|
@@ -353,7 +354,7 @@ class ArbitrageProcess:
|
|
|
waiting_times = waiting_times - 1
|
|
|
|
|
|
order_formated = pformat(order, indent=2)
|
|
|
- msg = f"交易所现货卖出订单已完成, 价格:{self.sell_price}, sell_value: {self.sell_value}\n order: {order_formated}"
|
|
|
+ msg = f"交易所现货卖出订单已完成, 价格 {self.sell_price}, sell_value: {self.sell_value}\n order: {order_formated}"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
@@ -383,7 +384,7 @@ class ArbitrageProcess:
|
|
|
"""
|
|
|
等待价差回归
|
|
|
"""
|
|
|
- msg = f"等待中心化交易所价差回归,目标:{self.close_limit}"
|
|
|
+ msg = f"等待中心化交易所价差回归,目标 {self.close_limit}"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
|
|
|
try:
|
|
|
@@ -398,7 +399,7 @@ class ArbitrageProcess:
|
|
|
dex_vs_cex_percentage = Decimal(table_data['diff_dex_vs_cex_percentage']) * 100
|
|
|
|
|
|
if dex_vs_cex_percentage < self.close_limit:
|
|
|
- msg = f"价差已回归,目标:{self.close_limit}%, 当前: {dex_vs_cex_percentage}%"
|
|
|
+ msg = f"价差已回归,目标 {self.close_limit}%, 当前: {dex_vs_cex_percentage}%"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
@@ -422,7 +423,7 @@ class ArbitrageProcess:
|
|
|
"""
|
|
|
执行回购操作
|
|
|
"""
|
|
|
- msg = f"正在回购,目标回购数量:{self.already_sold_amount}, 金额: {self.buy_value}"
|
|
|
+ msg = f"正在回购, 目标回购数量 {self.already_sold_amount}, 金额: {self.sell_value}"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
|
|
|
|
|
|
@@ -472,7 +473,7 @@ class ArbitrageProcess:
|
|
|
self._set_state(self.STATE_FAILED)
|
|
|
return
|
|
|
else:
|
|
|
- msg = f"交易所剩余{self.base_coin}: {free_balance}, 交易所准备使用:{pseudo_value_to_buy}, 余额校验通过。"
|
|
|
+ msg = f"交易所剩余{self.base_coin}: {free_balance}, 交易所准备使用 {pseudo_value_to_buy}, 余额校验通过。"
|
|
|
logger.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
break
|
|
|
@@ -522,11 +523,16 @@ class ArbitrageProcess:
|
|
|
if order['status'] in ["FILLED", "PARTIALLY_CANCELED", "CANCELED"]:
|
|
|
# 以实际成交价值为准
|
|
|
money = Decimal(order['cummulativeQuoteQty'])
|
|
|
- self.already_bought_amount = self.already_bought_amount + Decimal(order['executedQty'])
|
|
|
|
|
|
- self.buy_value = self.buy_value + money
|
|
|
- self.buy_price = self.buy_value / self.already_bought_amount
|
|
|
- self.buy_price = self.buy_price.quantize(self.price_precision, rounding=ROUND_DOWN)
|
|
|
+ # 实际成交价值大于0才计算
|
|
|
+ if money > Decimal(0):
|
|
|
+ order_formated = pformat(order, indent=2)
|
|
|
+ logger.info(f"检测到有成交 \n {order_formated}")
|
|
|
+ self.already_bought_amount = self.already_bought_amount + Decimal(order['executedQty'])
|
|
|
+
|
|
|
+ self.buy_value = self.buy_value + money
|
|
|
+ self.buy_price = self.buy_value / self.already_bought_amount
|
|
|
+ self.buy_price = self.buy_price.quantize(self.price_precision, rounding=ROUND_DOWN)
|
|
|
|
|
|
exchange_buy_order = None
|
|
|
|