|
|
@@ -10,7 +10,6 @@ from enum import Enum
|
|
|
import os
|
|
|
import lighter
|
|
|
import time
|
|
|
-import toml
|
|
|
|
|
|
|
|
|
# 配置日志
|
|
|
@@ -62,26 +61,6 @@ 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 # 等待初始化
|
|
|
@@ -97,18 +76,24 @@ class StrategyState(Enum):
|
|
|
class TradingStrategy:
|
|
|
"""交易策略类"""
|
|
|
|
|
|
- def __init__(self):
|
|
|
- """初始化策略"""
|
|
|
- # 加载配置文件
|
|
|
- self.config = load_config()
|
|
|
+ def __init__(self, config):
|
|
|
+ """
|
|
|
+ 初始化策略
|
|
|
+
|
|
|
+ Args:
|
|
|
+ config: 配置字典,包含strategy和lighter两个部分
|
|
|
+ """
|
|
|
+ # 保存传入的配置
|
|
|
+ self.config = config
|
|
|
|
|
|
self.state = StrategyState.WAITING_INIT
|
|
|
self.current_position = None # 当前持仓信息
|
|
|
|
|
|
# 从配置文件读取策略参数
|
|
|
- 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'] # 交易数量(买卖数量)
|
|
|
+ strategy_config = config.get('strategy', {})
|
|
|
+ self.entry_price_bps = strategy_config.get('entry_price_bps', 20) # 入场时的价差(单位:bps)
|
|
|
+ self.target_symbol = strategy_config.get('target_symbol', '1000FLOKI') # 目标交易对
|
|
|
+ self.trade_quantity = strategy_config.get('trade_quantity', 100) # 交易数量(买卖数量)
|
|
|
|
|
|
self.account_info = None # 存储账户信息
|
|
|
self.last_account_update_time = 0 # 上次更新账户信息的时间戳
|
|
|
@@ -116,15 +101,16 @@ class TradingStrategy:
|
|
|
self.position_side = None # 持仓方向:'long' 或 'short'
|
|
|
|
|
|
# 从配置文件读取Lighter相关参数
|
|
|
- self.account_index = self.config['lighter']['account_index']
|
|
|
- self.api_key_index = self.config['lighter']['api_key_index']
|
|
|
+ lighter_config = config.get('lighter', {})
|
|
|
+ self.account_index = lighter_config.get('account_index', 318163)
|
|
|
+ self.api_key_index = lighter_config.get('api_key_index', 0)
|
|
|
|
|
|
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=self.config['lighter']['url'],
|
|
|
- private_key=self.config['lighter']['private_key'],
|
|
|
+ url=lighter_config.get('url', 'https://mainnet.zklighter.elliot.ai'),
|
|
|
+ private_key=lighter_config.get('private_key', ''),
|
|
|
account_index=self.account_index,
|
|
|
api_key_index=self.api_key_index
|
|
|
)
|
|
|
@@ -551,8 +537,12 @@ class TradingStrategy:
|
|
|
logger.error(f"创建订单时发生错误: {str(e)}")
|
|
|
return None, str(e)
|
|
|
|
|
|
-async def main():
|
|
|
- strategy = TradingStrategy()
|
|
|
+async def main():
|
|
|
+ from config import load_config
|
|
|
+
|
|
|
+ # 加载配置文件
|
|
|
+ config = load_config()
|
|
|
+ strategy = TradingStrategy(config)
|
|
|
# account = await strategy.account_api.account(by="index", value=f"{strategy.account_index}")
|
|
|
|
|
|
# [AccountPosition(market_id=3, symbol='DOGE', initial_margin_fraction='10.00', open_order_count=0, pending_order_count=0, position_tied_order_count=0, sign=1, position='1', avg_entry_price='0.194368', position_value='0.194360', unrealized_pnl='-0.000008', realized_pnl='0.000000', liquidation_price='0', total_funding_paid_out=None, margin_mode=0, allocated_margin='0.000000', additional_properties={})]
|