|
|
@@ -7,14 +7,10 @@
|
|
|
|
|
|
import logging
|
|
|
from enum import Enum
|
|
|
-from datetime import datetime
|
|
|
import os
|
|
|
import lighter
|
|
|
-import json
|
|
|
import time
|
|
|
|
|
|
-from lighter.models import tx_hash
|
|
|
-
|
|
|
|
|
|
# 配置日志
|
|
|
logs_dir = "logs"
|
|
|
@@ -30,8 +26,9 @@ class StrategyState(Enum):
|
|
|
IDLE_MONITORING = 2 # 空闲状态监听价差
|
|
|
EXECUTING_TRADE = 3 # 价差达成触发交易
|
|
|
WAITING_CONVERGENCE = 4 # 交易完成等待价差收敛
|
|
|
- CLOSING_POSITION = 5 # 收敛完成进行平仓
|
|
|
- POSITION_CLOSED = 6 # 平仓完成
|
|
|
+ EXECUTING_CLOSE = 5 # 执行平仓操作
|
|
|
+ CHECKING_CLOSE = 6 # 检查平仓结果
|
|
|
+ POSITION_CLOSED = 7 # 平仓完成
|
|
|
|
|
|
|
|
|
class TradingStrategy:
|
|
|
@@ -119,8 +116,10 @@ class TradingStrategy:
|
|
|
await self._handle_executing_trade(market_data)
|
|
|
elif self.state == StrategyState.WAITING_CONVERGENCE:
|
|
|
await self._handle_waiting_convergence(market_data)
|
|
|
- elif self.state == StrategyState.CLOSING_POSITION:
|
|
|
- await self._handle_closing_position(market_data)
|
|
|
+ elif self.state == StrategyState.EXECUTING_CLOSE:
|
|
|
+ await self._handle_executing_close(market_data)
|
|
|
+ elif self.state == StrategyState.CHECKING_CLOSE:
|
|
|
+ await self._handle_checking_close(market_data)
|
|
|
elif self.state == StrategyState.POSITION_CLOSED:
|
|
|
await self._handle_position_closed()
|
|
|
|
|
|
@@ -274,7 +273,9 @@ class TradingStrategy:
|
|
|
logger.info(f"做空平仓条件触发:价差={price_diff_bps:.2f}bps <= 0")
|
|
|
|
|
|
if should_close:
|
|
|
- await self._close_position(orderbook, binance_price_float, lighter_price_float)
|
|
|
+ # 转换到执行平仓状态
|
|
|
+ self.state = StrategyState.EXECUTING_CLOSE
|
|
|
+ logger.info(f"状态转换: WAITING_CONVERGENCE -> EXECUTING_CLOSE")
|
|
|
|
|
|
async def _close_position(self, orderbook, binance_price, lighter_price):
|
|
|
"""平仓"""
|
|
|
@@ -303,18 +304,39 @@ class TradingStrategy:
|
|
|
|
|
|
if error:
|
|
|
logger.error(f"平仓失败: {error}")
|
|
|
+ # 平仓失败,保持在执行平仓状态,等待下次重试
|
|
|
+ logger.info(f"平仓失败,保持在 EXECUTING_CLOSE 状态等待重试")
|
|
|
return
|
|
|
|
|
|
# 记录平仓时间
|
|
|
self.last_trade_time = time.time()
|
|
|
|
|
|
- # 转换状态到平仓中
|
|
|
- self.state = StrategyState.CLOSING_POSITION
|
|
|
- logger.info(f"状态转换: WAITING_CONVERGENCE -> CLOSING_POSITION,交易哈希={tx_hash}")
|
|
|
+ # 转换状态到检查平仓
|
|
|
+ self.state = StrategyState.CHECKING_CLOSE
|
|
|
+ logger.info(f"状态转换: EXECUTING_CLOSE -> CHECKING_CLOSE,交易哈希={tx_hash}")
|
|
|
logger.info(f"等待1秒后检查平仓是否生效...")
|
|
|
|
|
|
- async def _handle_closing_position(self, market_data):
|
|
|
- """处理平仓中状态 - 等待1秒后检查持仓是否为0"""
|
|
|
+ async def _handle_executing_close(self, market_data):
|
|
|
+ """处理执行平仓状态 - 执行平仓操作"""
|
|
|
+ symbol = market_data.get('symbol')
|
|
|
+ if symbol != self.target_symbol:
|
|
|
+ return
|
|
|
+
|
|
|
+ binance_price = market_data.get('binance_price')
|
|
|
+ lighter_price = market_data.get('lighter_price')
|
|
|
+ orderbook = market_data.get('orderbook')
|
|
|
+
|
|
|
+ if not binance_price or not lighter_price or not orderbook:
|
|
|
+ return
|
|
|
+
|
|
|
+ # 计算价格
|
|
|
+ binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
|
|
|
+ lighter_price_float = float(lighter_price) if isinstance(lighter_price, str) else lighter_price
|
|
|
+
|
|
|
+ await self._close_position(orderbook, binance_price_float, lighter_price_float)
|
|
|
+
|
|
|
+ async def _handle_checking_close(self, market_data):
|
|
|
+ """处理检查平仓状态 - 等待1秒后检查持仓是否为0"""
|
|
|
# 检查是否已经等待了至少1秒
|
|
|
if time.time() - self.last_trade_time < 1.0:
|
|
|
return
|
|
|
@@ -341,12 +363,12 @@ class TradingStrategy:
|
|
|
self.state = StrategyState.IDLE_MONITORING
|
|
|
self.position_side = None
|
|
|
self.current_position = None
|
|
|
- logger.info(f"状态转换: CLOSING_POSITION -> IDLE_MONITORING")
|
|
|
+ logger.info(f"状态转换: CHECKING_CLOSE -> IDLE_MONITORING")
|
|
|
else:
|
|
|
- # 平仓未生效,回到执行交易状态重新检查
|
|
|
- logger.warning(f"平仓未生效,当前持仓={position.position},重新检查持仓状态")
|
|
|
- self.state = StrategyState.EXECUTING_TRADE
|
|
|
- logger.info(f"状态转换: CLOSING_POSITION -> EXECUTING_TRADE")
|
|
|
+ # 平仓未生效,重新执行平仓
|
|
|
+ logger.warning(f"平仓未生效,当前持仓={position.position},重新执行平仓")
|
|
|
+ self.state = StrategyState.EXECUTING_CLOSE
|
|
|
+ logger.info(f"状态转换: CHECKING_CLOSE -> EXECUTING_CLOSE")
|
|
|
|
|
|
async def _handle_position_closed(self):
|
|
|
"""处理平仓完成状态"""
|
|
|
@@ -399,7 +421,7 @@ class TradingStrategy:
|
|
|
logger.error(f"订单签名失败: {error}")
|
|
|
return None, error
|
|
|
|
|
|
- logger.info(f"订单签名成功,准备发送交易", tx_info)
|
|
|
+ logger.info(f"订单签名成功,准备发送交易: {tx_info}")
|
|
|
|
|
|
# 发送交易
|
|
|
tx_response = await self.transaction_api.send_tx(self.signer_client.TX_TYPE_CREATE_ORDER, tx_info)
|