Переглянути джерело

记录交易事件的地方修复

skyfffire 3 днів тому
батько
коміт
d06a6111a2
1 змінених файлів з 73 додано та 2 видалено
  1. 73 2
      src/leadlag/strategy.py

+ 73 - 2
src/leadlag/strategy.py

@@ -340,6 +340,34 @@ class TradingStrategy:
             # 从有持仓到无持仓:记录平仓成功并清理状态
             elif prev_position and not self.current_position:
                 logger.info("检测到持仓已平仓,进入空闲监听")
+
+                # 记录平仓成功事件到数据库
+                prev_position_qty = abs(float(prev_position.position))
+                prev_position_side = 'long' if getattr(prev_position, 'sign', 1) == 1 else 'short'
+                event_type = 'close_long' if prev_position_side == 'long' else 'close_short'
+
+                # 获取当前市场数据
+                binance_price = market_data.get('binance_price')
+                binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price if binance_price else None
+
+                self.database.record_trading_event(
+                    symbol=self.target_symbol,
+                    event_type=event_type,
+                    price=self.last_closing_price if self.last_closing_price else binance_price_float,
+                    quantity=prev_position_qty,
+                    side=prev_position_side,
+                    strategy_state=new_state.name if new_state else None,
+                    binance_price=binance_price_float,
+                    order_id=str(self.closing_order_id) if self.closing_order_id else None,
+                    tx_hash=None,  # 平仓成功时无法获取tx_hash
+                    success=True,
+                    metadata={
+                        'ask_bps': self.current_ask_bps,
+                        'bid_bps': self.current_bid_bps,
+                        'order_type': 'limit_close'
+                    }
+                )
+
                 # 清理平仓相关状态
                 self.closing_order_id = None
                 self.last_closing_price = None
@@ -482,13 +510,35 @@ class TradingStrategy:
                 return
             
             logger.info(f"预签名{side_desc}交易已发送,交易哈希={tx_hash}")
-            
+
+            # 记录开仓事件到数据库
+            event_type = 'open_long' if direction == 'long' else 'open_short'
+            binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
+            self.database.record_trading_event(
+                symbol=market_info.get('symbol'),
+                event_type=event_type,
+                price=price,
+                quantity=quantity,
+                side=direction,
+                strategy_state=self.state.name if self.state else None,
+                binance_price=binance_price_float,
+                order_id=str(oid),
+                tx_hash=tx_hash,
+                success=True,
+                metadata={
+                    'is_ask': is_ask,
+                    'ask_bps': self.current_ask_bps,
+                    'bid_bps': self.current_bid_bps,
+                    'order_type': 'presigned_market'
+                }
+            )
+
             # 清空已使用的预签名交易
             if direction == 'long':
                 self.presigned_long_tx = None
             else:
                 self.presigned_short_tx = None
-            
+
         except Exception as e:
             logger.error(f"发送预签名{side_desc}交易时出错: {str(e)}")
             # 回退到普通开仓
@@ -531,6 +581,27 @@ class TradingStrategy:
         # 记录开仓时间
         self.last_trade_time = time.time()
         logger.info(f"备选开仓交易已发送,交易哈希={tx_hash}")
+
+        # 记录开仓事件到数据库
+        event_type = 'open_long' if self.position_side == 'long' else 'open_short'
+        self.database.record_trading_event(
+            symbol=market_info.get('symbol'),
+            event_type=event_type,
+            price=price,
+            quantity=self.trade_quantity,
+            side=self.position_side,
+            strategy_state=self.state.name if self.state else None,
+            binance_price=binance_price_float,
+            order_id=str(oid),
+            tx_hash=tx_hash,
+            success=True,
+            metadata={
+                'is_ask': is_ask,
+                'ask_bps': self.current_ask_bps,
+                'bid_bps': self.current_bid_bps,
+                'order_type': 'fallback_market'
+            }
+        )
     
     async def _handle_closing_with_limit_order(self, market_data):
         """处理限价单平仓状态 - 挂仅减仓的限价单平仓"""