|
|
@@ -196,8 +196,27 @@ class TradingStrategy:
|
|
|
# 记录价格数据到数据库
|
|
|
await self._record_price_data(market_data)
|
|
|
|
|
|
- # 基于账户信息更新 current_position
|
|
|
+ # 基于账户信息更新 current_position 和状态
|
|
|
+ await self._update_position_and_state(market_data)
|
|
|
+
|
|
|
+ # 根据当前状态执行相应逻辑
|
|
|
+ if self.state == StrategyState.IDLE_MONITORING:
|
|
|
+ await self._handle_idle_monitoring(market_data)
|
|
|
+ elif self.state == StrategyState.CLOSING_WITH_LIMIT_ORDER:
|
|
|
+ await self._handle_closing_with_limit_order(market_data)
|
|
|
+
|
|
|
+ async def _update_position_and_state(self, market_data):
|
|
|
+ """
|
|
|
+ 更新持仓信息和策略状态
|
|
|
+
|
|
|
+ Args:
|
|
|
+ market_data: 市场数据字典
|
|
|
+ """
|
|
|
+ # 保存旧状态和持仓用于对比
|
|
|
+ prev_state = self.state
|
|
|
prev_position = self.current_position
|
|
|
+
|
|
|
+ # 从账户信息中提取当前持仓
|
|
|
position = None
|
|
|
if self.account_info and self.account_info.accounts:
|
|
|
try:
|
|
|
@@ -212,6 +231,7 @@ class TradingStrategy:
|
|
|
break
|
|
|
except Exception as e:
|
|
|
logger.warning(f"解析账户持仓失败: {e}")
|
|
|
+
|
|
|
self.current_position = position
|
|
|
|
|
|
# 同步持仓方向
|
|
|
@@ -220,62 +240,29 @@ class TradingStrategy:
|
|
|
else:
|
|
|
self.position_side = None
|
|
|
|
|
|
- # 状态切换与变化日志
|
|
|
- prev_state = self.state
|
|
|
+ # 根据持仓情况确定新状态
|
|
|
new_state = StrategyState.CLOSING_WITH_LIMIT_ORDER if self.current_position else StrategyState.IDLE_MONITORING
|
|
|
+
|
|
|
+ # 处理状态变化
|
|
|
if prev_state != new_state:
|
|
|
prev_name = prev_state.name if prev_state else 'None'
|
|
|
logger.info(f"状态转换: {prev_name} -> {new_state.name}")
|
|
|
- if self.current_position:
|
|
|
- # 记录开仓成功事件
|
|
|
- event_type = 'open_long' if self.position_side == 'long' else 'open_short'
|
|
|
- self.database.record_trading_event(
|
|
|
- symbol=self.target_symbol,
|
|
|
- event_type=event_type,
|
|
|
- price=binance_price_float,
|
|
|
- quantity=self.trade_quantity,
|
|
|
- side=self.position_side,
|
|
|
- strategy_state=self.state.name if self.state else 'IDLE_MONITORING',
|
|
|
- binance_price=binance_price_float,
|
|
|
- success=True,
|
|
|
- metadata={
|
|
|
- 'tx_hash': tx_hash,
|
|
|
- 'order_id': oid
|
|
|
- }
|
|
|
- )
|
|
|
-
|
|
|
- logger.info(f"检测到持仓:方向={'做多' if getattr(self.current_position, 'sign', 1) == 1 else '做空'},数量={self.current_position.position}")
|
|
|
- else:
|
|
|
- # 记录平仓成功事件
|
|
|
- event_type = 'close_long' if self.position_side == 'long' else 'close_short'
|
|
|
- self.database.record_trading_event(
|
|
|
- symbol=self.target_symbol,
|
|
|
- event_type=event_type,
|
|
|
- price=binance_price_float,
|
|
|
- quantity=abs(float(self.current_position.position)),
|
|
|
- side=self.position_side,
|
|
|
- strategy_state=self.state.name,
|
|
|
- binance_price=binance_price_float,
|
|
|
- success=True,
|
|
|
- metadata={'closing_order_id': self.closing_order_id}
|
|
|
- )
|
|
|
- logger.info("检测到无持仓,进入空闲监听")
|
|
|
- # 清理状态
|
|
|
- self.current_position = None
|
|
|
- self.position_side = None
|
|
|
+
|
|
|
+ # 从无持仓到有持仓:记录开仓成功事件
|
|
|
+ if not prev_position and self.current_position:
|
|
|
+ position_qty = abs(float(self.current_position.position))
|
|
|
+ logger.info(f"检测到新持仓:方向={'做多' if self.position_side == 'long' else '做空'},数量={position_qty}")
|
|
|
+
|
|
|
+ # 从有持仓到无持仓:记录平仓成功并清理状态
|
|
|
+ elif prev_position and not self.current_position:
|
|
|
+ logger.info("检测到持仓已平仓,进入空闲监听")
|
|
|
+ # 清理平仓相关状态
|
|
|
self.closing_order_id = None
|
|
|
self.last_closing_price = None
|
|
|
- self.state = StrategyState.IDLE_MONITORING
|
|
|
- logger.info(f"状态转换: CLOSING_WITH_LIMIT_ORDER -> IDLE_MONITORING")
|
|
|
- logger.info(f"========================================================================")
|
|
|
+ logger.info("========================================================================")
|
|
|
+
|
|
|
+ # 更新状态
|
|
|
self.state = new_state
|
|
|
-
|
|
|
- # 根据当前状态执行相应逻辑
|
|
|
- if self.state == StrategyState.IDLE_MONITORING:
|
|
|
- await self._handle_idle_monitoring(market_data)
|
|
|
- elif self.state == StrategyState.CLOSING_WITH_LIMIT_ORDER:
|
|
|
- await self._handle_closing_with_limit_order(market_data)
|
|
|
- # 旧的平仓状态已移除,使用CLOSING_WITH_LIMIT_ORDER代替
|
|
|
|
|
|
async def _record_price_data(self, market_data):
|
|
|
"""记录价格数据到数据库 - 简化版本"""
|