소스 검색

添加价格变动阈值检查机制:只有当价格变动超过5bps时才执行策略

skyfffire 7 시간 전
부모
커밋
5cc474d8c2
2개의 변경된 파일57개의 추가작업 그리고 0개의 파일을 삭제
  1. 57 0
      src/leadlag/strategy.py
  2. 0 0
      tests/test_optimizations.py

+ 57 - 0
src/leadlag/strategy.py

@@ -132,6 +132,11 @@ class TradingStrategy:
         self.account_update_task = None
         self.account_update_interval = 1.0  # 每1秒更新一次账户信息
 
+        # 价格变动阈值相关变量
+        self.last_binance_price = None      # 上次执行策略时的binance价格
+        self.last_lighter_mid_price = None  # 上次执行策略时的lighter中间价
+        self.price_change_threshold_bps = 5  # 价格变动阈值(bps),超过此值才执行策略
+
         # 从配置文件读取Lighter相关参数
         lighter_config = config.get('lighter', {})
         self.account_index = lighter_config.get('account_index', 318163)
@@ -246,6 +251,54 @@ class TradingStrategy:
         except Exception as e:
             logger.error(f"预签名订单时发生错误: {str(e)}")
     
+    def _check_price_change_threshold(self, market_data):
+        """
+        检查价格变动是否超过阈值
+
+        Args:
+            market_data: 市场数据字典
+
+        Returns:
+            bool: True表示价格变动超过阈值,应该执行策略;False表示变动不足,跳过执行
+        """
+        try:
+            binance_price = market_data.get('binance_price')
+            lighter_bid = market_data.get('best_bid')
+            lighter_ask = market_data.get('best_ask')
+
+            # 如果缺少必要数据,返回True(执行策略)
+            if not binance_price or not lighter_bid or not lighter_ask:
+                return True
+
+            binance_price_float = float(binance_price) if isinstance(binance_price, str) else binance_price
+            lighter_mid_price = (lighter_bid + lighter_ask) / 2
+
+            # 第一次执行,记录价格并返回True
+            if self.last_binance_price is None or self.last_lighter_mid_price is None:
+                self.last_binance_price = binance_price_float
+                self.last_lighter_mid_price = lighter_mid_price
+                return True
+
+            # 计算binance价格变动(bps)
+            binance_change_bps = abs(binance_price_float - self.last_binance_price) / self.last_binance_price * 10000
+
+            # 计算lighter中间价变动(bps)
+            lighter_change_bps = abs(lighter_mid_price - self.last_lighter_mid_price) / self.last_lighter_mid_price * 10000
+
+            # 如果任一价格变动超过阈值,更新记录的价格并返回True
+            if binance_change_bps >= self.price_change_threshold_bps or lighter_change_bps >= self.price_change_threshold_bps:
+                # logger.debug(f"价格变动超过阈值: Binance {binance_change_bps:.2f}bps, Lighter {lighter_change_bps:.2f}bps")
+                self.last_binance_price = binance_price_float
+                self.last_lighter_mid_price = lighter_mid_price
+                return True
+
+            # 价格变动不足,跳过执行
+            return False
+
+        except Exception as e:
+            logger.error(f"检查价格变动阈值失败: {e}")
+            return True  # 出错时执行策略
+
     async def _update_bps_from_order_book(self, market_data):
         """
         从market_data中提取买卖价并计算价差(bps)
@@ -302,6 +355,10 @@ class TradingStrategy:
         if symbol != self.target_symbol:
             return
 
+        # 检查价格变动是否超过阈值,如果不超过则跳过策略执行
+        if not self._check_price_change_threshold(market_data):
+            return
+
         # 在最前面计算和更新价差数据
         await self._update_bps_from_order_book(market_data)
 

+ 0 - 0
test_optimizations.py → tests/test_optimizations.py