Quellcode durchsuchen

feat(配置): 添加TOML配置文件支持并重构策略参数加载

将硬编码的策略参数和Lighter配置迁移到TOML配置文件中
添加config.toml.sample示例文件和.gitignore配置
更新requirements.txt添加toml依赖
skyfffire vor 4 Tagen
Ursprung
Commit
b4df3d46e2
4 geänderte Dateien mit 64 neuen und 8 gelöschten Zeilen
  1. 4 1
      .gitignore
  2. 24 0
      config.toml.sample
  3. 1 0
      requirements.txt
  4. 35 7
      src/leadlag/strategy.py

+ 4 - 1
.gitignore

@@ -107,4 +107,7 @@ venv.bak/
 dmypy.json
 
 # Pyre type checker
-.pyre/
+.pyre/
+
+# config
+config.toml

+ 24 - 0
config.toml.sample

@@ -0,0 +1,24 @@
+# Lead-Lag Trading Strategy Configuration
+
+[strategy]
+# 入场时的价差阈值(单位:bps,1bps = 0.01%)
+entry_price_bps = 20
+
+# 目标交易对
+target_symbol = "1000FLOKI"
+
+# 交易数量(买卖数量)
+trade_quantity = 100
+
+[lighter]
+# Lighter账户索引
+account_index = 0
+
+# API密钥索引
+api_key_index = 0
+
+# 私钥,不带0x打头应该就行了
+private_key = ""
+
+# Lighter API URL
+url = "https://mainnet.zklighter.elliot.ai"

+ 1 - 0
requirements.txt

@@ -3,3 +3,4 @@ requests
 aiohttp
 websockets
 lighter-sdk
+toml

+ 35 - 7
src/leadlag/strategy.py

@@ -10,6 +10,7 @@ from enum import Enum
 import os
 import lighter
 import time
+import toml
 
 
 # 配置日志
@@ -61,6 +62,26 @@ if not logger.handlers:
 logger.propagate = False
 
 
+def load_config():
+    """加载配置文件"""
+    # 获取项目根目录(向上两级目录)
+    current_dir = os.path.dirname(os.path.abspath(__file__))
+    project_root = os.path.dirname(os.path.dirname(current_dir))
+    config_path = os.path.join(project_root, 'config.toml')
+    
+    try:
+        with open(config_path, 'r', encoding='utf-8') as f:
+            config = toml.load(f)
+        logger.info(f"配置文件加载成功: {config_path}")
+        return config
+    except FileNotFoundError:
+        logger.error(f"配置文件未找到: {config_path}")
+        raise
+    except Exception as e:
+        logger.error(f"配置文件加载失败: {str(e)}")
+        raise
+
+
 class StrategyState(Enum):
     """策略状态枚举"""
     WAITING_INIT = 1  # 等待初始化
@@ -78,25 +99,32 @@ class TradingStrategy:
     
     def __init__(self):
         """初始化策略"""
+        # 加载配置文件
+        self.config = load_config()
+        
         self.state = StrategyState.WAITING_INIT
         self.current_position = None    # 当前持仓信息
-        self.entry_price_bps = 20        # 入场时的价差(单位:bps)
-        self.target_symbol = "1000FLOKI"     # 目标交易对
-        self.trade_quantity = 100         # 交易数量(买卖数量)
+        
+        # 从配置文件读取策略参数
+        self.entry_price_bps = self.config['strategy']['entry_price_bps']        # 入场时的价差(单位:bps)
+        self.target_symbol = self.config['strategy']['target_symbol']     # 目标交易对
+        self.trade_quantity = self.config['strategy']['trade_quantity']         # 交易数量(买卖数量)
+        
         self.account_info = None        # 存储账户信息
         self.last_account_update_time = 0  # 上次更新账户信息的时间戳
         self.last_trade_time = 0        # 上次交易时间戳(开仓或平仓)
         self.position_side = None       # 持仓方向:'long' 或 'short'
 
-        self.account_index = 318163
-        self.api_key_index = 0
+        # 从配置文件读取Lighter相关参数
+        self.account_index = self.config['lighter']['account_index']
+        self.api_key_index = self.config['lighter']['api_key_index']
 
         self.api_client = lighter.ApiClient()
         self.account_api = lighter.AccountApi(self.api_client)
         self.transaction_api = lighter.TransactionApi(self.api_client)
         self.signer_client = lighter.SignerClient(  
-            url='https://mainnet.zklighter.elliot.ai',  
-            private_key='72aafa0426f7ff2806c68625ca5c88de153e34fcb23567f3b872cd56334d2848fb223466efff9c21',  
+            url=self.config['lighter']['url'],  
+            private_key=self.config['lighter']['private_key'],  
             account_index=self.account_index,
             api_key_index=self.api_key_index
         )