feat(config): execute all json
in config/
This commit is contained in:
98
main.py
98
main.py
@@ -55,9 +55,9 @@ class MexcSpotMarket:
|
||||
- get_price(symbol): 获取指定交易对的当前价格
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, config):
|
||||
"""初始化市场数据查询接口"""
|
||||
self.market = mexc_spot_v3.mexc_market()
|
||||
self.market = mexc_spot_v3.mexc_market(config)
|
||||
|
||||
def get_price(self, symbol: str) -> Optional[float]:
|
||||
"""
|
||||
@@ -123,12 +123,12 @@ class MexcSpotTrade:
|
||||
"FILL_OR_KILL": ["quantity", "price"],
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, config, symbol_mapping):
|
||||
"""初始化交易机器人"""
|
||||
self.trader = mexc_spot_v3.mexc_trade()
|
||||
self.market = MexcSpotMarket()
|
||||
self.trader = mexc_spot_v3.mexc_trade(config)
|
||||
self.market = MexcSpotMarket(config)
|
||||
self.csv_file = "output/mexc_spot_trade.csv"
|
||||
self.symbol_mapping = TradingConfig().config_data.get("symbol_mapping", {})
|
||||
self.symbol_mapping = symbol_mapping
|
||||
|
||||
def _api_get_order(self, symbol: str, order_id: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
@@ -414,7 +414,7 @@ class TradingConfig:
|
||||
- _load_config(): 加载JSON配置文件
|
||||
"""
|
||||
|
||||
def __init__(self, config_file: str = "public/trading_config.json"):
|
||||
def __init__(self, config_file: str = "config/trading_config.json"):
|
||||
"""
|
||||
初始化交易配置
|
||||
|
||||
@@ -481,52 +481,74 @@ class TradingConfig:
|
||||
def main():
|
||||
"""主函数"""
|
||||
|
||||
# 初始化交易配置
|
||||
config = TradingConfig()
|
||||
|
||||
# 获取今天需要执行的交易
|
||||
today_trades = config.get_today_trades()
|
||||
|
||||
if not today_trades:
|
||||
logger.info("今天没有需要执行的交易")
|
||||
# 确保config目录存在
|
||||
if not os.path.exists("config"):
|
||||
logger.error("配置目录 config 不存在")
|
||||
return
|
||||
|
||||
logger.info("今天有 %d 个交易需要执行", len(today_trades))
|
||||
# 获取config目录下所有json文件
|
||||
config_files = list(Path("config").glob("*.json"))
|
||||
|
||||
# 初始化交易类
|
||||
spot_trader = MexcSpotTrade()
|
||||
if not config_files:
|
||||
logger.info("配置目录中没有找到任何JSON文件")
|
||||
return
|
||||
|
||||
# 执行每个交易
|
||||
for trade_config in today_trades:
|
||||
logger.info("找到 %d 个配置文件需要处理", len(config_files))
|
||||
|
||||
for config_file in config_files:
|
||||
try:
|
||||
# 提取交易参数
|
||||
symbol = trade_config.get("symbol")
|
||||
order_type = trade_config.get("order_type")
|
||||
side = trade_config.get("side")
|
||||
params = trade_config.get("params", {})
|
||||
logger.info("处理配置文件: %s", config_file)
|
||||
config = TradingConfig(str(config_file))
|
||||
spot_trader = MexcSpotTrade(
|
||||
config.config_data.get("api", {}),
|
||||
config.config_data.get("symbol_mapping", {}),
|
||||
)
|
||||
today_trades = config.get_today_trades()
|
||||
|
||||
if not all([symbol, order_type, side]):
|
||||
logger.error("交易配置缺少必要参数: %s", trade_config)
|
||||
if not today_trades:
|
||||
logger.info("%s - 今天没有需要执行的交易", config_file)
|
||||
continue
|
||||
|
||||
logger.info("执行交易: %s %s %s", symbol, order_type, side)
|
||||
logger.debug("交易参数: %s", params)
|
||||
logger.info("%s - 今天有 %d 个交易需要执行", config_file, len(today_trades))
|
||||
|
||||
# 执行交易
|
||||
result = spot_trader.trade(
|
||||
symbol=symbol, order_type=order_type, side=side, **params
|
||||
)
|
||||
for trade_config in today_trades:
|
||||
try:
|
||||
symbol = trade_config.get("symbol")
|
||||
order_type = trade_config.get("order_type")
|
||||
side = trade_config.get("side")
|
||||
params = trade_config.get("params", {})
|
||||
|
||||
if result:
|
||||
logger.info("交易执行成功: %s", result)
|
||||
else:
|
||||
logger.error("交易执行失败")
|
||||
if not all([symbol, order_type, side]):
|
||||
logger.error(
|
||||
"%s - 交易配置缺少必要参数: %s", config_file, trade_config
|
||||
)
|
||||
continue
|
||||
|
||||
logger.info(
|
||||
"%s - 执行交易: %s %s %s", config_file, symbol, order_type, side
|
||||
)
|
||||
logger.debug("%s - 交易参数: %s", config_file, params)
|
||||
|
||||
# 执行交易
|
||||
result = spot_trader.trade(
|
||||
symbol=symbol, order_type=order_type, side=side, **params
|
||||
)
|
||||
|
||||
if result:
|
||||
logger.info("%s - 交易执行成功: %s", config_file, result)
|
||||
else:
|
||||
logger.error("%s - 交易执行失败", config_file)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("%s - 执行交易时出错: %s", config_file, str(e))
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
logger.error("执行交易时出错: %s", str(e))
|
||||
logger.error("处理配置文件 %s 时出错: %s", config_file, str(e))
|
||||
continue
|
||||
|
||||
logger.info("执行完毕")
|
||||
logger.info("所有配置文件处理完毕")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user