|
|
@@ -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
|
|
|
)
|