优化
This commit is contained in:
parent
3e5e505851
commit
0deff19c4d
@ -1,24 +0,0 @@
|
|||||||
# 加密货币智能体 crontab 配置示例
|
|
||||||
# 将以下内容添加到crontab中: crontab -e
|
|
||||||
|
|
||||||
# 设置环境变量
|
|
||||||
SHELL=/bin/bash
|
|
||||||
PATH=/usr/local/bin:/usr/bin:/bin
|
|
||||||
PYTHONPATH=/path/to/your/cryptoai
|
|
||||||
|
|
||||||
# 日志文件
|
|
||||||
LOGFILE=/path/to/your/cryptoai/logs/cron.log
|
|
||||||
|
|
||||||
# 每天早上8:30运行加密货币智能体
|
|
||||||
30 8 * * * cd /path/to/your/cryptoai && python run.py --agent crypto --run-once >> $LOGFILE 2>&1
|
|
||||||
|
|
||||||
# 每天晚上20:30运行黄金智能体
|
|
||||||
30 20 * * * cd /path/to/your/cryptoai && python run.py --agent gold --run-once >> $LOGFILE 2>&1
|
|
||||||
|
|
||||||
# 每6小时运行一次加密货币智能体
|
|
||||||
0 */6 * * * cd /path/to/your/cryptoai && python run.py --agent crypto --run-once >> $LOGFILE 2>&1
|
|
||||||
|
|
||||||
# 每周一早上9点运行完整分析
|
|
||||||
0 9 * * 1 cd /path/to/your/cryptoai && python run.py --agent crypto --symbol BTCUSDT --days 90 >> $LOGFILE 2>&1
|
|
||||||
|
|
||||||
# 注意:使用前请将路径替换为您实际的项目路径
|
|
||||||
Binary file not shown.
Binary file not shown.
@ -39,7 +39,6 @@ class CryptoAgent:
|
|||||||
self.deepseek_config = self.config_loader.get_deepseek_config()
|
self.deepseek_config = self.config_loader.get_deepseek_config()
|
||||||
self.crypto_config = self.config_loader.get_crypto_config()
|
self.crypto_config = self.config_loader.get_crypto_config()
|
||||||
self.data_config = self.config_loader.get_data_config()
|
self.data_config = self.config_loader.get_data_config()
|
||||||
self.agent_config = self.config_loader.get_agent_config()
|
|
||||||
self.dingtalk_config = self.config_loader.get_dingtalk_config()
|
self.dingtalk_config = self.config_loader.get_dingtalk_config()
|
||||||
|
|
||||||
# 初始化API客户端
|
# 初始化API客户端
|
||||||
@ -84,12 +83,6 @@ class CryptoAgent:
|
|||||||
|
|
||||||
# 设置时间间隔
|
# 设置时间间隔
|
||||||
self.time_interval = self.crypto_config['time_interval']
|
self.time_interval = self.crypto_config['time_interval']
|
||||||
|
|
||||||
# 风险等级
|
|
||||||
self.risk_level = self.agent_config['risk_level']
|
|
||||||
|
|
||||||
# 支持的策略
|
|
||||||
self.strategies = self.agent_config['strategies']
|
|
||||||
|
|
||||||
def fetch_historical_data(self, symbol: str, days: int = 30) -> pd.DataFrame:
|
def fetch_historical_data(self, symbol: str, days: int = 30) -> pd.DataFrame:
|
||||||
"""
|
"""
|
||||||
@ -237,7 +230,7 @@ class CryptoAgent:
|
|||||||
print(f"\n开始分析{symbol}...")
|
print(f"\n开始分析{symbol}...")
|
||||||
|
|
||||||
# 获取并处理数据
|
# 获取并处理数据
|
||||||
raw_data = self.fetch_historical_data(symbol, days=self.data_config['historical_days'])
|
raw_data = self.fetch_historical_data(symbol, days=self.crypto_config['historical_days'])
|
||||||
|
|
||||||
if not raw_data.empty:
|
if not raw_data.empty:
|
||||||
processed_data = self.process_data(symbol, raw_data)
|
processed_data = self.process_data(symbol, raw_data)
|
||||||
@ -413,7 +406,7 @@ class CryptoAgent:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"导出 token 使用情况时出错: {e}")
|
print(f"导出 token 使用情况时出错: {e}")
|
||||||
|
|
||||||
def start_agent(self, run_once: bool = False) -> None:
|
def start_agent(self) -> None:
|
||||||
"""
|
"""
|
||||||
启动智能体
|
启动智能体
|
||||||
|
|
||||||
@ -423,18 +416,10 @@ class CryptoAgent:
|
|||||||
print("启动加密货币分析智能体...")
|
print("启动加密货币分析智能体...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if run_once:
|
self.run_analysis_cycle()
|
||||||
self.run_analysis_cycle()
|
|
||||||
# 导出最终的token使用情况
|
# 导出最终的token使用情况
|
||||||
self._export_token_usage()
|
self._export_token_usage()
|
||||||
else:
|
|
||||||
while True:
|
|
||||||
self.run_analysis_cycle()
|
|
||||||
|
|
||||||
# 等待下一个分析周期
|
|
||||||
analysis_interval = self.agent_config['analysis_interval']
|
|
||||||
print(f"等待{analysis_interval}分钟后进行下一次分析...")
|
|
||||||
time.sleep(analysis_interval * 60)
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\n智能体已停止")
|
print("\n智能体已停止")
|
||||||
|
|||||||
@ -35,10 +35,9 @@ class GoldAgent:
|
|||||||
# 获取各部分配置
|
# 获取各部分配置
|
||||||
self.binance_config = self.config_loader.get_binance_config()
|
self.binance_config = self.config_loader.get_binance_config()
|
||||||
self.deepseek_config = self.config_loader.get_deepseek_config()
|
self.deepseek_config = self.config_loader.get_deepseek_config()
|
||||||
self.alltick_config = self.config_loader.get_config('alltick')
|
self.alltick_config = self.config_loader.get_alltick_config()
|
||||||
self.gold_config = self.config_loader.get_config('gold') # 黄金特定配置
|
self.gold_config = self.config_loader.get_gold_config() # 黄金特定配置
|
||||||
self.data_config = self.config_loader.get_data_config()
|
self.data_config = self.config_loader.get_data_config()
|
||||||
self.agent_config = self.config_loader.get_agent_config()
|
|
||||||
self.dingtalk_config = self.config_loader.get_dingtalk_config()
|
self.dingtalk_config = self.config_loader.get_dingtalk_config()
|
||||||
|
|
||||||
# 初始化API客户端
|
# 初始化API客户端
|
||||||
@ -75,9 +74,7 @@ class GoldAgent:
|
|||||||
|
|
||||||
# 设置时间间隔
|
# 设置时间间隔
|
||||||
self.time_interval = self.gold_config.get('time_interval', '1d') # 默认日线
|
self.time_interval = self.gold_config.get('time_interval', '1d') # 默认日线
|
||||||
|
|
||||||
# 风险等级
|
|
||||||
self.risk_level = self.agent_config['risk_level']
|
|
||||||
|
|
||||||
def fetch_historical_data(self, symbol: str, days: int = 180) -> pd.DataFrame:
|
def fetch_historical_data(self, symbol: str, days: int = 180) -> pd.DataFrame:
|
||||||
"""
|
"""
|
||||||
@ -414,7 +411,7 @@ class GoldAgent:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"导出 token 使用情况时出错: {e}")
|
print(f"导出 token 使用情况时出错: {e}")
|
||||||
|
|
||||||
def start_agent(self, run_once: bool = False) -> None:
|
def start_agent(self) -> None:
|
||||||
"""
|
"""
|
||||||
启动智能体
|
启动智能体
|
||||||
|
|
||||||
@ -424,19 +421,9 @@ class GoldAgent:
|
|||||||
print("启动黄金分析智能体...")
|
print("启动黄金分析智能体...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if run_once:
|
self.run_analysis_cycle()
|
||||||
self.run_analysis_cycle()
|
# 导出最终的token使用情况
|
||||||
# 导出最终的token使用情况
|
self._export_token_usage()
|
||||||
self._export_token_usage()
|
|
||||||
else:
|
|
||||||
while True:
|
|
||||||
self.run_analysis_cycle()
|
|
||||||
|
|
||||||
# 等待下一个分析周期
|
|
||||||
analysis_interval = self.gold_config.get('analysis_interval',
|
|
||||||
self.agent_config['analysis_interval'])
|
|
||||||
print(f"等待{analysis_interval}分钟后进行下一次分析...")
|
|
||||||
time.sleep(analysis_interval * 60)
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\n智能体已停止")
|
print("\n智能体已停止")
|
||||||
|
|||||||
@ -1,90 +0,0 @@
|
|||||||
# Binance API设置
|
|
||||||
binance:
|
|
||||||
api_key: "your_binance_api_key_here"
|
|
||||||
api_secret: "your_binance_api_secret_here"
|
|
||||||
test_mode: true # 设置为false将使用实盘交易
|
|
||||||
|
|
||||||
# OKX API设置
|
|
||||||
okx:
|
|
||||||
api_key: "your_okx_api_key"
|
|
||||||
api_secret: "your_okx_api_secret"
|
|
||||||
passphrase: "your_okx_passphrase"
|
|
||||||
test_mode: false # 设置为true使用测试网络
|
|
||||||
|
|
||||||
# DeepSeek AI设置
|
|
||||||
deepseek:
|
|
||||||
api_key: "your_deepseek_api_key"
|
|
||||||
model: "deepseek-chat" # 使用的模型
|
|
||||||
|
|
||||||
# AllTick API设置(用于获取黄金数据)
|
|
||||||
alltick:
|
|
||||||
api_key: "your_alltick_api_key"
|
|
||||||
symbols:
|
|
||||||
- "XAUUSD" # 黄金/美元
|
|
||||||
# - "XAGUSD" # 白银/美元
|
|
||||||
|
|
||||||
# 加密货币设置
|
|
||||||
crypto:
|
|
||||||
base_currencies:
|
|
||||||
- "BTC"
|
|
||||||
- "ETH"
|
|
||||||
- "BNB"
|
|
||||||
- "SOL"
|
|
||||||
- "ARB"
|
|
||||||
quote_currency: "USDT"
|
|
||||||
time_interval: "1d" # 支持: 1m, 5m, 15m, 30m, 1h, 2h, 4h, 1d, 1w, 1M
|
|
||||||
min_data_points: 100 # 最小数据点数量,不应小于30
|
|
||||||
|
|
||||||
# 黄金市场分析配置
|
|
||||||
gold:
|
|
||||||
# 黄金交易对
|
|
||||||
symbols:
|
|
||||||
- "GOLD"
|
|
||||||
- "SILVER"
|
|
||||||
# 历史数据天数
|
|
||||||
historical_days: 180
|
|
||||||
# 时间间隔
|
|
||||||
time_interval: "1d"
|
|
||||||
# 分析间隔(分钟)
|
|
||||||
analysis_interval: 360
|
|
||||||
# 是否启用宏观经济因素分析
|
|
||||||
enable_macro_analysis: true
|
|
||||||
# 是否保存图表
|
|
||||||
save_charts: true
|
|
||||||
|
|
||||||
# 数据设置
|
|
||||||
data:
|
|
||||||
storage_path: "./cryptoai/data"
|
|
||||||
historical_days: 60 # 历史数据获取天数
|
|
||||||
update_interval: 60 # 数据更新间隔(分钟)
|
|
||||||
|
|
||||||
# Agent设置
|
|
||||||
agent:
|
|
||||||
analysis_interval: 120 # 分析间隔(分钟)
|
|
||||||
strategies:
|
|
||||||
- "trend_following"
|
|
||||||
- "mean_reversion"
|
|
||||||
- "breakout"
|
|
||||||
- "rsi_strategy"
|
|
||||||
risk_level: "medium" # 风险等级: low, medium, high
|
|
||||||
|
|
||||||
# 日志设置
|
|
||||||
logging:
|
|
||||||
level: "INFO" # 可选: DEBUG, INFO, WARNING, ERROR
|
|
||||||
file_path: "./cryptoai/logs"
|
|
||||||
|
|
||||||
# 钉钉机器人设置
|
|
||||||
dingtalk:
|
|
||||||
enabled: false # 是否启用钉钉通知
|
|
||||||
webhook_url: "https://oapi.dingtalk.com/robot/send?access_token=your_access_token"
|
|
||||||
secret: "your_secret" # 如果使用了加签安全设置,请填写此项
|
|
||||||
at_mobiles: [] # 需要@的手机号列表
|
|
||||||
at_all: false # 是否@所有人
|
|
||||||
|
|
||||||
# 数据库配置
|
|
||||||
database:
|
|
||||||
host: "gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com"
|
|
||||||
port: 27469
|
|
||||||
user: "root"
|
|
||||||
password: "Aa#223388"
|
|
||||||
db_name: "cryptoai"
|
|
||||||
@ -34,6 +34,7 @@ crypto:
|
|||||||
# - "WLD"
|
# - "WLD"
|
||||||
quote_currency: "USDT"
|
quote_currency: "USDT"
|
||||||
time_interval: "4h" # 可选: 1m, 5m, 15m, 30m, 1h, 4h, 1d
|
time_interval: "4h" # 可选: 1m, 5m, 15m, 30m, 1h, 4h, 1d
|
||||||
|
historical_days: 180
|
||||||
|
|
||||||
# 黄金市场分析配置
|
# 黄金市场分析配置
|
||||||
gold:
|
gold:
|
||||||
@ -43,27 +44,11 @@ gold:
|
|||||||
historical_days: 180
|
historical_days: 180
|
||||||
# 时间间隔
|
# 时间间隔
|
||||||
time_interval: "1d"
|
time_interval: "1d"
|
||||||
# 分析间隔(分钟)
|
|
||||||
analysis_interval: 360
|
|
||||||
# 是否启用宏观经济因素分析
|
|
||||||
enable_macro_analysis: true
|
|
||||||
# 是否保存图表
|
|
||||||
save_charts: true
|
|
||||||
|
|
||||||
# 数据设置
|
# 数据设置
|
||||||
data:
|
data:
|
||||||
storage_path: "./cryptoai/data"
|
storage_path: "./cryptoai/data"
|
||||||
historical_days: 180
|
|
||||||
update_interval: 60 # 数据更新间隔(分钟)
|
|
||||||
|
|
||||||
# Agent设置
|
|
||||||
agent:
|
|
||||||
analysis_interval: 120 # 分析间隔(分钟)
|
|
||||||
strategies:
|
|
||||||
- "trend_following"
|
|
||||||
- "momentum"
|
|
||||||
- "sentiment"
|
|
||||||
risk_level: "medium" # 可选: low, medium, high
|
|
||||||
|
|
||||||
# 日志设置
|
# 日志设置
|
||||||
logging:
|
logging:
|
||||||
|
|||||||
193
cryptoai/main.py
193
cryptoai/main.py
@ -15,197 +15,10 @@ from cryptoai.agents.crypto_agent import CryptoAgent
|
|||||||
from cryptoai.agents.gold_agent import GoldAgent
|
from cryptoai.agents.gold_agent import GoldAgent
|
||||||
from cryptoai.utils.config_loader import ConfigLoader
|
from cryptoai.utils.config_loader import ConfigLoader
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
|
||||||
"""解析命令行参数"""
|
|
||||||
parser = argparse.ArgumentParser(description='加密货币AI分析智能体')
|
|
||||||
|
|
||||||
parser.add_argument('--config', type=str, default=None,
|
|
||||||
help='配置文件路径,默认使用 cryptoai/config/config.yaml')
|
|
||||||
|
|
||||||
parser.add_argument('--run-once', action='store_true',
|
|
||||||
help='只运行一次分析周期,而不是持续运行')
|
|
||||||
|
|
||||||
parser.add_argument('--symbol', type=str, default=None,
|
|
||||||
help='只分析指定的交易对,例如 BTCUSDT')
|
|
||||||
|
|
||||||
parser.add_argument('--days', type=int, default=None,
|
|
||||||
help='获取历史数据的天数,默认使用配置中的值')
|
|
||||||
|
|
||||||
parser.add_argument('--interval', type=str, default=None,
|
|
||||||
help='K线时间间隔,例如 1h,默认使用配置中的值')
|
|
||||||
|
|
||||||
parser.add_argument('--risk-level', type=str, default=None,
|
|
||||||
choices=['low', 'medium', 'high'],
|
|
||||||
help='风险等级,默认使用配置中的值')
|
|
||||||
|
|
||||||
parser.add_argument('--agent', type=str, default='crypto',
|
|
||||||
choices=['crypto', 'gold'],
|
|
||||||
help='要使用的智能体类型,默认为加密货币智能体')
|
|
||||||
|
|
||||||
parser.add_argument('--alltick-key', type=str, default=None,
|
|
||||||
help='AllTick API密钥,用于获取黄金等商品数据')
|
|
||||||
|
|
||||||
parser.add_argument('--okx-key', type=str, default=None,
|
|
||||||
help='OKX API密钥')
|
|
||||||
|
|
||||||
parser.add_argument('--okx-secret', type=str, default=None,
|
|
||||||
help='OKX API密钥')
|
|
||||||
|
|
||||||
parser.add_argument('--okx-passphrase', type=str, default=None,
|
|
||||||
help='OKX API密码')
|
|
||||||
|
|
||||||
return parser.parse_args()
|
|
||||||
|
|
||||||
|
|
||||||
def override_config_with_args(config_loader: ConfigLoader, args) -> None:
|
|
||||||
"""
|
|
||||||
使用命令行参数覆盖配置
|
|
||||||
|
|
||||||
Args:
|
|
||||||
config_loader: 配置加载器
|
|
||||||
args: 命令行参数
|
|
||||||
"""
|
|
||||||
# 获取原始配置
|
|
||||||
crypto_config = config_loader.get_crypto_config()
|
|
||||||
agent_config = config_loader.get_agent_config()
|
|
||||||
data_config = config_loader.get_data_config()
|
|
||||||
|
|
||||||
# 覆盖配置
|
|
||||||
if args.symbol:
|
|
||||||
if args.agent == 'crypto':
|
|
||||||
crypto_config['base_currencies'] = [args.symbol.replace(crypto_config['quote_currency'], '')]
|
|
||||||
elif args.agent == 'gold':
|
|
||||||
gold_config = config_loader.get_config('gold')
|
|
||||||
gold_config['symbols'] = [args.symbol]
|
|
||||||
|
|
||||||
if args.days:
|
|
||||||
data_config['historical_days'] = args.days
|
|
||||||
|
|
||||||
if args.interval:
|
|
||||||
if args.agent == 'crypto':
|
|
||||||
crypto_config['time_interval'] = args.interval
|
|
||||||
elif args.agent == 'gold':
|
|
||||||
gold_config = config_loader.get_config('gold')
|
|
||||||
gold_config['time_interval'] = args.interval
|
|
||||||
|
|
||||||
if args.risk_level:
|
|
||||||
agent_config['risk_level'] = args.risk_level
|
|
||||||
|
|
||||||
# 设置AllTick API密钥
|
|
||||||
if args.alltick_key:
|
|
||||||
alltick_config = config_loader.get_config('alltick')
|
|
||||||
alltick_config['api_key'] = args.alltick_key
|
|
||||||
|
|
||||||
# 设置OKX API相关配置
|
|
||||||
if args.okx_key or args.okx_secret or args.okx_passphrase:
|
|
||||||
okx_config = config_loader.get_config('okx')
|
|
||||||
if args.okx_key:
|
|
||||||
okx_config['api_key'] = args.okx_key
|
|
||||||
if args.okx_secret:
|
|
||||||
okx_config['api_secret'] = args.okx_secret
|
|
||||||
if args.okx_passphrase:
|
|
||||||
okx_config['passphrase'] = args.okx_passphrase
|
|
||||||
|
|
||||||
# 保存修改后的配置
|
|
||||||
# 注意:这只是修改了内存中的配置,没有写入文件
|
|
||||||
# 如果需要保存到文件,可以实现一个 save_config 方法
|
|
||||||
|
|
||||||
|
|
||||||
def analyze_single_symbol(agent, symbol: str, days: int = 30) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
分析单个交易对
|
|
||||||
|
|
||||||
Args:
|
|
||||||
agent: 智能体实例
|
|
||||||
symbol: 交易对符号
|
|
||||||
days: 历史数据天数
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
分析结果
|
|
||||||
"""
|
|
||||||
print(f"\n开始分析{symbol}...")
|
|
||||||
|
|
||||||
# 获取并处理数据
|
|
||||||
raw_data = agent.fetch_historical_data(symbol, days=days)
|
|
||||||
|
|
||||||
if raw_data.empty:
|
|
||||||
print(f"无法获取{symbol}的数据")
|
|
||||||
return {}
|
|
||||||
|
|
||||||
processed_data = agent.process_data(symbol, raw_data)
|
|
||||||
|
|
||||||
# 根据agent类型执行不同的分析
|
|
||||||
if isinstance(agent, CryptoAgent):
|
|
||||||
# 分析市场
|
|
||||||
analysis_result = agent.analyze_market(symbol, processed_data)
|
|
||||||
|
|
||||||
# 预测价格
|
|
||||||
prediction_result = agent.predict_price(symbol)
|
|
||||||
|
|
||||||
# 生成策略
|
|
||||||
strategy = agent.generate_strategy(symbol, analysis_result)
|
|
||||||
|
|
||||||
# 整合结果
|
|
||||||
result = {
|
|
||||||
"analysis": analysis_result,
|
|
||||||
"prediction": prediction_result,
|
|
||||||
"strategy": strategy
|
|
||||||
}
|
|
||||||
elif isinstance(agent, GoldAgent):
|
|
||||||
# 黄金agent没有单独的analyze_market方法,使用完整分析
|
|
||||||
result = agent.analyze_gold()
|
|
||||||
|
|
||||||
print(f"{symbol}分析完成")
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""主函数"""
|
|
||||||
# 解析命令行参数
|
|
||||||
args = parse_arguments()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 加载配置
|
# 启动智能体
|
||||||
config_loader = ConfigLoader(args.config)
|
CryptoAgent().start_agent()
|
||||||
|
|
||||||
# 使用命令行参数覆盖配置
|
|
||||||
override_config_with_args(config_loader, args)
|
|
||||||
|
|
||||||
# 根据agent类型创建不同的智能体
|
|
||||||
if args.agent == 'crypto':
|
|
||||||
agent = CryptoAgent(config_path=args.config)
|
|
||||||
print("已启动加密货币分析智能体")
|
|
||||||
elif args.agent == 'gold':
|
|
||||||
agent = GoldAgent(config_path=args.config)
|
|
||||||
print("已启动黄金分析智能体")
|
|
||||||
|
|
||||||
# 如果指定了单个交易对
|
|
||||||
if args.symbol:
|
|
||||||
result = analyze_single_symbol(
|
|
||||||
agent=agent,
|
|
||||||
symbol=args.symbol,
|
|
||||||
days=args.days or (
|
|
||||||
agent.data_config['historical_days'] if isinstance(agent, CryptoAgent)
|
|
||||||
else agent.gold_config.get('historical_days', 180)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if isinstance(agent, CryptoAgent):
|
|
||||||
print("\n分析结果:")
|
|
||||||
print(f"市场趋势: {result.get('analysis', {}).get('market_trend', 'unknown')}")
|
|
||||||
print(f"24小时预测: {result.get('prediction', {}).get('prediction_24h', {})}")
|
|
||||||
print(f"建议操作: {result.get('strategy', {}).get('position', 'unknown')}")
|
|
||||||
elif isinstance(agent, GoldAgent) and result:
|
|
||||||
print("\n分析结果:")
|
|
||||||
symbol_result = result.get(args.symbol, {})
|
|
||||||
analysis = symbol_result.get('analysis', {})
|
|
||||||
print(f"市场趋势: {analysis.get('market_trend', 'unknown')}")
|
|
||||||
print(f"建议操作: {analysis.get('recommendation', 'unknown')}")
|
|
||||||
else:
|
|
||||||
# 启动智能体
|
|
||||||
agent.start_agent(run_once=args.run_once)
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\n程序已退出")
|
print("\n程序已退出")
|
||||||
@ -217,4 +30,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Binary file not shown.
@ -31,16 +31,6 @@ class ConfigLoader:
|
|||||||
|
|
||||||
# 加载配置
|
# 加载配置
|
||||||
self.config = self._load_config()
|
self.config = self._load_config()
|
||||||
|
|
||||||
# 确保database配置存在
|
|
||||||
if 'database' not in self.config:
|
|
||||||
self.config['database'] = {
|
|
||||||
'host': 'gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com',
|
|
||||||
'port': 27469,
|
|
||||||
'user': 'root',
|
|
||||||
'password': 'Aa#223388',
|
|
||||||
'db_name': 'cryptoai'
|
|
||||||
}
|
|
||||||
|
|
||||||
def _load_config(self) -> Dict[str, Any]:
|
def _load_config(self) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
@ -82,6 +72,10 @@ class ConfigLoader:
|
|||||||
"""获取DeepSeek配置"""
|
"""获取DeepSeek配置"""
|
||||||
return self.get_config('deepseek')
|
return self.get_config('deepseek')
|
||||||
|
|
||||||
|
def get_gold_config(self) -> Dict[str, Any]:
|
||||||
|
"""获取黄金配置"""
|
||||||
|
return self.get_config('gold')
|
||||||
|
|
||||||
def get_alltick_config(self) -> Dict[str, Any]:
|
def get_alltick_config(self) -> Dict[str, Any]:
|
||||||
"""获取AllTick配置"""
|
"""获取AllTick配置"""
|
||||||
return self.get_config('alltick')
|
return self.get_config('alltick')
|
||||||
@ -94,10 +88,6 @@ class ConfigLoader:
|
|||||||
"""获取数据配置"""
|
"""获取数据配置"""
|
||||||
return self.get_config('data')
|
return self.get_config('data')
|
||||||
|
|
||||||
def get_agent_config(self) -> Dict[str, Any]:
|
|
||||||
"""获取Agent配置"""
|
|
||||||
return self.get_config('agent')
|
|
||||||
|
|
||||||
def get_logging_config(self) -> Dict[str, Any]:
|
def get_logging_config(self) -> Dict[str, Any]:
|
||||||
"""获取日志配置"""
|
"""获取日志配置"""
|
||||||
return self.get_config('logging')
|
return self.get_config('logging')
|
||||||
|
|||||||
@ -14,7 +14,7 @@ services:
|
|||||||
# 持久化数据和日志
|
# 持久化数据和日志
|
||||||
- cryptoai_data:/app/cryptoai/data
|
- cryptoai_data:/app/cryptoai/data
|
||||||
- cryptoai_logs:/app/logs
|
- cryptoai_logs:/app/logs
|
||||||
command: ["--run-once", "--agent", "crypto"]
|
command: ["--agent", "crypto"]
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
cryptoai_data:
|
cryptoai_data:
|
||||||
|
|||||||
@ -1,108 +0,0 @@
|
|||||||
# 在青龙面板中部署CryptoAI项目
|
|
||||||
|
|
||||||
本文档介绍如何在青龙面板中部署和运行CryptoAI加密货币分析项目。
|
|
||||||
|
|
||||||
## 一、前提条件
|
|
||||||
|
|
||||||
1. 已经安装并配置好青龙面板
|
|
||||||
2. 拥有青龙面板的管理权限
|
|
||||||
3. 已获取必要的API密钥(Binance、DeepSeek等)
|
|
||||||
|
|
||||||
## 二、部署步骤
|
|
||||||
|
|
||||||
### 1. 上传项目到青龙容器
|
|
||||||
|
|
||||||
有两种方法可以将项目上传到青龙容器:
|
|
||||||
|
|
||||||
#### 方法一:使用青龙面板的文件管理功能
|
|
||||||
|
|
||||||
1. 在青龙面板中,点击左侧菜单的【文件管理】
|
|
||||||
2. 点击【新建目录】,创建 `/ql/scripts/cryptoai` 目录
|
|
||||||
3. 将CryptoAI项目文件上传到该目录中
|
|
||||||
|
|
||||||
#### 方法二:使用Docker CP命令
|
|
||||||
|
|
||||||
1. 确定青龙容器的ID或名称
|
|
||||||
```bash
|
|
||||||
docker ps | grep qinglong
|
|
||||||
```
|
|
||||||
|
|
||||||
2. 将项目文件复制到容器中
|
|
||||||
```bash
|
|
||||||
docker cp /path/to/cryptoai container_name:/ql/scripts/
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. 创建定时任务脚本
|
|
||||||
|
|
||||||
1. 创建 `qinglong_task.sh` 脚本文件(已在项目中提供)
|
|
||||||
2. 编辑脚本中的API密钥和其他配置,确保填入正确的值
|
|
||||||
3. 将脚本上传到青龙容器的 `/ql/scripts/` 目录下
|
|
||||||
|
|
||||||
### 3. 配置青龙定时任务
|
|
||||||
|
|
||||||
1. 在青龙面板中,点击左侧菜单的【定时任务】
|
|
||||||
2. 点击【创建任务】,填写以下信息:
|
|
||||||
- 名称:CryptoAI加密货币分析
|
|
||||||
- 命令:bash /ql/scripts/qinglong_task.sh
|
|
||||||
- 定时规则:根据需要设置,例如每天执行一次可设置为 `0 8 * * *`
|
|
||||||
- 其他选项根据需要设置
|
|
||||||
|
|
||||||
## 三、环境变量配置
|
|
||||||
|
|
||||||
为便于管理,可以在青龙面板中设置环境变量:
|
|
||||||
|
|
||||||
1. 在青龙面板中,点击左侧菜单的【环境变量】
|
|
||||||
2. 点击【创建变量】,添加以下变量(根据实际情况填写):
|
|
||||||
|
|
||||||
| 名称 | 值 | 备注 |
|
|
||||||
|------|----|----|
|
|
||||||
| DB_HOST | gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com | 数据库主机 |
|
|
||||||
| DB_PORT | 27469 | 数据库端口 |
|
|
||||||
| DB_USER | root | 数据库用户名 |
|
|
||||||
| DB_PASSWORD | Aa#223388 | 数据库密码 |
|
|
||||||
| DB_NAME | cryptoai | 数据库名 |
|
|
||||||
| BINANCE_API_KEY | your_key | 币安API密钥 |
|
|
||||||
| BINANCE_API_SECRET | your_secret | 币安密钥 |
|
|
||||||
| DEEPSEEK_API_KEY | your_key | DeepSeek API密钥 |
|
|
||||||
| DINGTALK_ENABLED | true | 是否启用钉钉通知 |
|
|
||||||
| DINGTALK_WEBHOOK_URL | your_url | 钉钉Webhook地址 |
|
|
||||||
| DINGTALK_SECRET | your_secret | 钉钉加签密钥 |
|
|
||||||
|
|
||||||
## 四、验证部署
|
|
||||||
|
|
||||||
1. 在青龙面板的【定时任务】页面,找到刚创建的任务
|
|
||||||
2. 点击【运行】按钮进行测试运行
|
|
||||||
3. 点击【日志】查看执行情况,确认是否正常运行
|
|
||||||
|
|
||||||
## 五、常见问题
|
|
||||||
|
|
||||||
### 依赖安装问题
|
|
||||||
|
|
||||||
如果遇到依赖安装问题,可以在青龙容器中手动安装:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd /ql/scripts/cryptoai
|
|
||||||
pip install -r requirements.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
### 权限问题
|
|
||||||
|
|
||||||
如果遇到权限问题,可以修改脚本权限:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
chmod +x /ql/scripts/qinglong_task.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
### 数据库连接问题
|
|
||||||
|
|
||||||
确保数据库能够从青龙容器所在网络访问。如果使用内网数据库,可能需要配置网络连接。
|
|
||||||
|
|
||||||
## 六、更新方式
|
|
||||||
|
|
||||||
当需要更新CryptoAI项目时,只需替换 `/ql/scripts/cryptoai` 目录下的文件即可。
|
|
||||||
|
|
||||||
确保配置文件和数据不被覆盖,最好在更新前备份数据目录:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker cp container_name:/ql/scripts/cryptoai/data /backup/path
|
|
||||||
```
|
|
||||||
108
qinglong_task.sh
108
qinglong_task.sh
@ -1,108 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# 青龙面板定时任务脚本 - CryptoAI加密货币分析
|
|
||||||
|
|
||||||
# 设置工作目录
|
|
||||||
WORKDIR="/ql/data/scripts/cryptoai"
|
|
||||||
|
|
||||||
# 如果目录不存在则退出
|
|
||||||
if [ ! -d "$WORKDIR" ]; then
|
|
||||||
echo "工作目录不存在,请先将CryptoAI项目上传到青龙容器内的 /ql/data/scripts/cryptoai 路径"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 进入工作目录
|
|
||||||
cd "$WORKDIR"
|
|
||||||
|
|
||||||
# 设置Python路径
|
|
||||||
export PYTHONPATH=$WORKDIR
|
|
||||||
|
|
||||||
# 设置环境变量
|
|
||||||
# 数据库配置
|
|
||||||
export DB_HOST="gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com"
|
|
||||||
export DB_PORT="27469"
|
|
||||||
export DB_USER="root"
|
|
||||||
export DB_PASSWORD="Aa#223388"
|
|
||||||
export DB_NAME="cryptoai"
|
|
||||||
|
|
||||||
# API配置 - 请替换为自己的API密钥
|
|
||||||
export BINANCE_API_KEY="HCpeel8g6fsTK2630b7BvGBcS09Z3qfXkLVcAY2JkpaiMm1J6DWRvoQZBQlElDJg"
|
|
||||||
export BINANCE_API_SECRET="TySs6onlHOTrGzV8fMdDxLKTWWYnQ4rCHVAmjrcHby17acKflmo7xVTWVsbqtxe7"
|
|
||||||
export BINANCE_TEST_MODE="false"
|
|
||||||
|
|
||||||
export DEEPSEEK_API_KEY="sk-9f6b56f08796435d988cf202e37f6ee3"
|
|
||||||
export DEEPSEEK_MODEL="deepseek-chat"
|
|
||||||
|
|
||||||
export OKX_API_KEY="7abe4037-3d93-40d4-a77b-c77f4a1e9490"
|
|
||||||
export OKX_API_SECRET="654946A2045F44CC2853D47F96C62F4E"
|
|
||||||
export OKX_PASSPHRASE="Aa@123456"
|
|
||||||
export OKX_TEST_MODE="false"
|
|
||||||
|
|
||||||
# 钉钉配置
|
|
||||||
export DINGTALK_ENABLED="true"
|
|
||||||
export DINGTALK_WEBHOOK_URL="https://oapi.dingtalk.com/robot/send?access_token=2278b723cd363bb6f85592c743b59b166e70b9e02a275bb5cedbc33b53a5cbdc"
|
|
||||||
export DINGTALK_SECRET="your_dingtalk_secret"
|
|
||||||
|
|
||||||
# 确保日志目录存在
|
|
||||||
if [ ! -d "$WORKDIR/logs" ]; then
|
|
||||||
mkdir -p "$WORKDIR/logs"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 记录开始时间
|
|
||||||
echo "=============================="
|
|
||||||
echo "开始执行CryptoAI分析: $(date)"
|
|
||||||
echo "=============================="
|
|
||||||
|
|
||||||
# 检查是否有Python虚拟环境
|
|
||||||
if [ -d "/ql/data/scripts/cryptoai/venv" ]; then
|
|
||||||
echo "使用项目虚拟环境"
|
|
||||||
source /ql/data/scripts/cryptoai/venv/bin/activate
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 安装编译工具
|
|
||||||
echo "检查编译工具..."
|
|
||||||
if ! command -v gcc &> /dev/null; then
|
|
||||||
echo "安装基本编译工具..."
|
|
||||||
if command -v apk &> /dev/null; then
|
|
||||||
# Alpine Linux
|
|
||||||
apk add --no-cache gcc g++ musl-dev python3-dev
|
|
||||||
elif command -v apt-get &> /dev/null; then
|
|
||||||
# Debian/Ubuntu
|
|
||||||
apt-get update && apt-get install -y build-essential python3-dev
|
|
||||||
elif command -v yum &> /dev/null; then
|
|
||||||
# CentOS/RHEL
|
|
||||||
yum install -y gcc gcc-c++ python3-devel
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 检查依赖是否安装
|
|
||||||
echo "检查并安装依赖包..."
|
|
||||||
if ! pip list | grep -q "pandas"; then
|
|
||||||
echo "开始安装依赖..."
|
|
||||||
# 首先升级pip
|
|
||||||
pip install --upgrade pip
|
|
||||||
|
|
||||||
# 安装setuptools的兼容版本
|
|
||||||
pip install -i https://mirrors.aliyun.com/pypi/simple/ "setuptools<60.0"
|
|
||||||
|
|
||||||
# 使用国内镜像源安装预编译的wheel包
|
|
||||||
pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary -r requirements.txt
|
|
||||||
|
|
||||||
# 验证关键依赖是否安装成功
|
|
||||||
if ! pip list | grep -q "pandas"; then
|
|
||||||
echo "错误:依赖安装失败,请检查网络连接或手动安装依赖"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "依赖检查完成"
|
|
||||||
|
|
||||||
# 运行加密货币分析
|
|
||||||
python run.py --run-once --agent crypto
|
|
||||||
|
|
||||||
# 如果需要运行黄金分析,取消下面的注释
|
|
||||||
# python run.py --run-once --agent gold
|
|
||||||
|
|
||||||
# 记录结束时间
|
|
||||||
echo "=============================="
|
|
||||||
echo "CryptoAI分析完成: $(date)"
|
|
||||||
echo "=============================="
|
|
||||||
1
run.py
1
run.py
@ -6,7 +6,6 @@ CryptoAI 启动脚本
|
|||||||
|
|
||||||
用法:
|
用法:
|
||||||
python run.py # 持续运行智能体
|
python run.py # 持续运行智能体
|
||||||
python run.py --run-once # 运行一次分析周期
|
|
||||||
python run.py --symbol BTCUSDT # 只分析指定的交易对
|
python run.py --symbol BTCUSDT # 只分析指定的交易对
|
||||||
python run.py --days 7 # 获取7天的历史数据
|
python run.py --days 7 # 获取7天的历史数据
|
||||||
python run.py --risk-level low # 设置低风险等级
|
python run.py --risk-level low # 设置低风险等级
|
||||||
|
|||||||
140
schedule_task.py
140
schedule_task.py
@ -1,140 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
加密货币分析智能体定时任务
|
|
||||||
支持以下配置:
|
|
||||||
- 定时执行:设置每天指定时间执行分析任务
|
|
||||||
- 周期执行:设置每隔一定时间执行一次分析任务
|
|
||||||
- 同时支持多个智能体:crypto和gold
|
|
||||||
|
|
||||||
使用方法:
|
|
||||||
python schedule_task.py # 使用默认配置执行
|
|
||||||
python schedule_task.py --agent crypto # 指定执行加密货币智能体
|
|
||||||
python schedule_task.py --agent gold # 指定执行黄金智能体
|
|
||||||
python schedule_task.py --both # 同时执行两种智能体
|
|
||||||
python schedule_task.py --time "12:00" # 设置每天执行的时间
|
|
||||||
python schedule_task.py --interval 240 # 设置执行间隔(分钟)
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
import argparse
|
|
||||||
import schedule
|
|
||||||
import subprocess
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
def parse_arguments():
|
|
||||||
"""解析命令行参数"""
|
|
||||||
parser = argparse.ArgumentParser(description='加密货币分析智能体定时任务')
|
|
||||||
|
|
||||||
parser.add_argument('--agent', type=str, default='crypto',
|
|
||||||
choices=['crypto', 'gold'],
|
|
||||||
help='要执行的智能体类型,默认为加密货币智能体')
|
|
||||||
|
|
||||||
parser.add_argument('--both', action='store_true',
|
|
||||||
help='同时执行两种智能体')
|
|
||||||
|
|
||||||
parser.add_argument('--time', type=str, default=None,
|
|
||||||
help='每天执行任务的时间,格式为HH:MM,例如 "08:30"')
|
|
||||||
|
|
||||||
parser.add_argument('--interval', type=int, default=360,
|
|
||||||
help='自动执行间隔(分钟),默认为360分钟(6小时)')
|
|
||||||
|
|
||||||
return parser.parse_args()
|
|
||||||
|
|
||||||
def run_agent(agent_type='crypto'):
|
|
||||||
"""
|
|
||||||
执行智能体
|
|
||||||
|
|
||||||
Args:
|
|
||||||
agent_type: 智能体类型,可选 'crypto' 或 'gold'
|
|
||||||
"""
|
|
||||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
print(f"[{current_time}] 执行{agent_type}智能体...")
|
|
||||||
|
|
||||||
try:
|
|
||||||
# 构建命令
|
|
||||||
cmd = ["python", "run.py", f"--agent={agent_type}", "--run-once"]
|
|
||||||
|
|
||||||
# 执行命令
|
|
||||||
process = subprocess.Popen(
|
|
||||||
cmd,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
universal_newlines=True
|
|
||||||
)
|
|
||||||
|
|
||||||
# 实时输出日志
|
|
||||||
while True:
|
|
||||||
output = process.stdout.readline()
|
|
||||||
if output == '' and process.poll() is not None:
|
|
||||||
break
|
|
||||||
if output:
|
|
||||||
print(output.strip())
|
|
||||||
|
|
||||||
# 获取执行结果
|
|
||||||
_, stderr = process.communicate()
|
|
||||||
|
|
||||||
# 检查是否有错误
|
|
||||||
if process.returncode != 0:
|
|
||||||
print(f"执行{agent_type}智能体时出错: {stderr}")
|
|
||||||
else:
|
|
||||||
print(f"[{current_time}] {agent_type}智能体执行完成")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"执行{agent_type}智能体时出错: {e}")
|
|
||||||
|
|
||||||
def run_both_agents():
|
|
||||||
"""执行两种智能体"""
|
|
||||||
run_agent('crypto')
|
|
||||||
run_agent('gold')
|
|
||||||
|
|
||||||
def main():
|
|
||||||
"""主函数"""
|
|
||||||
args = parse_arguments()
|
|
||||||
|
|
||||||
# 定义任务函数
|
|
||||||
if args.both:
|
|
||||||
job_func = run_both_agents
|
|
||||||
agent_desc = "加密货币和黄金"
|
|
||||||
else:
|
|
||||||
job_func = lambda: run_agent(args.agent)
|
|
||||||
agent_desc = f"{args.agent}分析"
|
|
||||||
|
|
||||||
# 根据参数设置定时任务
|
|
||||||
if args.time:
|
|
||||||
# 设置每天固定时间执行
|
|
||||||
schedule.every().day.at(args.time).do(job_func)
|
|
||||||
print(f"已设置每天 {args.time} 执行{agent_desc}智能体")
|
|
||||||
else:
|
|
||||||
# 设置定时间隔执行
|
|
||||||
schedule.every(args.interval).minutes.do(job_func)
|
|
||||||
print(f"已设置每 {args.interval} 分钟执行一次{agent_desc}智能体")
|
|
||||||
|
|
||||||
# 立即执行一次
|
|
||||||
print("立即执行一次任务...")
|
|
||||||
job_func()
|
|
||||||
|
|
||||||
# 循环等待下次执行
|
|
||||||
while True:
|
|
||||||
# 检查是否有定时任务需要执行
|
|
||||||
schedule.run_pending()
|
|
||||||
|
|
||||||
# 打印下次执行时间
|
|
||||||
next_run = schedule.next_run().strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
print(f"下次执行时间: {next_run}")
|
|
||||||
|
|
||||||
# 等待一段时间再检查
|
|
||||||
time.sleep(60)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
try:
|
|
||||||
main()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print("\n定时任务已停止")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"定时任务出错: {e}")
|
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
137
schedule_task.sh
137
schedule_task.sh
@ -1,137 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# 加密货币分析智能体定时任务启动脚本
|
|
||||||
# 该脚本用于在后台启动智能体定时任务
|
|
||||||
|
|
||||||
# 使用说明:
|
|
||||||
# ./schedule_task.sh start crypto 360 # 以360分钟为间隔启动加密货币智能体
|
|
||||||
# ./schedule_task.sh start gold "08:30" # 每天08:30运行黄金智能体
|
|
||||||
# ./schedule_task.sh start both 240 # 以240分钟为间隔同时启动两种智能体
|
|
||||||
# ./schedule_task.sh stop # 停止所有运行中的定时任务
|
|
||||||
# ./schedule_task.sh status # 查看运行状态
|
|
||||||
|
|
||||||
# 获取当前脚本所在目录
|
|
||||||
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
|
||||||
LOG_DIR="${SCRIPT_DIR}/logs"
|
|
||||||
SCHEDULE_LOG="${LOG_DIR}/schedule_task.log"
|
|
||||||
|
|
||||||
# 确保日志目录存在
|
|
||||||
mkdir -p "${LOG_DIR}"
|
|
||||||
|
|
||||||
# 激活虚拟环境(如果使用了虚拟环境)
|
|
||||||
VENV_PATH="${SCRIPT_DIR}/venv"
|
|
||||||
if [ -d "${VENV_PATH}" ]; then
|
|
||||||
source "${VENV_PATH}/bin/activate"
|
|
||||||
echo "已激活虚拟环境: ${VENV_PATH}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 定义启动定时任务的函数
|
|
||||||
start_schedule_task() {
|
|
||||||
local agent_type=$1
|
|
||||||
local time_or_interval=$2
|
|
||||||
local cmd="python ${SCRIPT_DIR}/schedule_task.py"
|
|
||||||
|
|
||||||
# 如果同时执行两种智能体
|
|
||||||
if [ "${agent_type}" == "both" ]; then
|
|
||||||
cmd="${cmd} --both"
|
|
||||||
else
|
|
||||||
cmd="${cmd} --agent ${agent_type}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 判断第二个参数是时间点还是间隔
|
|
||||||
if [[ "${time_or_interval}" =~ ^[0-9]+$ ]]; then
|
|
||||||
# 如果是纯数字,则认为是间隔
|
|
||||||
cmd="${cmd} --interval ${time_or_interval}"
|
|
||||||
else
|
|
||||||
# 否则认为是时间点
|
|
||||||
cmd="${cmd} --time \"${time_or_interval}\""
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 使用nohup在后台运行
|
|
||||||
echo "启动定时任务: ${cmd}"
|
|
||||||
nohup ${cmd} > "${SCHEDULE_LOG}" 2>&1 &
|
|
||||||
|
|
||||||
# 保存PID
|
|
||||||
echo $! > "${SCRIPT_DIR}/.schedule_task.pid"
|
|
||||||
echo "定时任务已在后台启动,PID: $!"
|
|
||||||
echo "日志文件: ${SCHEDULE_LOG}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# 定义停止定时任务的函数
|
|
||||||
stop_schedule_task() {
|
|
||||||
if [ -f "${SCRIPT_DIR}/.schedule_task.pid" ]; then
|
|
||||||
local pid=$(cat "${SCRIPT_DIR}/.schedule_task.pid")
|
|
||||||
echo "正在停止定时任务 (PID: ${pid})..."
|
|
||||||
kill -15 ${pid} 2>/dev/null || echo "进程 ${pid} 不存在"
|
|
||||||
rm -f "${SCRIPT_DIR}/.schedule_task.pid"
|
|
||||||
else
|
|
||||||
echo "没有找到运行中的定时任务"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# 定义查看状态的函数
|
|
||||||
check_status() {
|
|
||||||
if [ -f "${SCRIPT_DIR}/.schedule_task.pid" ]; then
|
|
||||||
local pid=$(cat "${SCRIPT_DIR}/.schedule_task.pid")
|
|
||||||
if ps -p ${pid} > /dev/null; then
|
|
||||||
echo "定时任务正在运行 (PID: ${pid})"
|
|
||||||
echo "最近的日志:"
|
|
||||||
tail -n 20 "${SCHEDULE_LOG}"
|
|
||||||
else
|
|
||||||
echo "定时任务已停止 (上次PID: ${pid})"
|
|
||||||
rm -f "${SCRIPT_DIR}/.schedule_task.pid"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "没有找到运行中的定时任务"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# 主逻辑
|
|
||||||
case "$1" in
|
|
||||||
start)
|
|
||||||
if [ -f "${SCRIPT_DIR}/.schedule_task.pid" ]; then
|
|
||||||
pid=$(cat "${SCRIPT_DIR}/.schedule_task.pid")
|
|
||||||
if ps -p ${pid} > /dev/null; then
|
|
||||||
echo "定时任务已在运行中 (PID: ${pid})"
|
|
||||||
echo "如需重启,请先执行: $0 stop"
|
|
||||||
exit 1
|
|
||||||
else
|
|
||||||
rm -f "${SCRIPT_DIR}/.schedule_task.pid"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 检查必要的参数
|
|
||||||
if [ -z "$2" ]; then
|
|
||||||
echo "缺少参数: agent_type (crypto, gold 或 both)"
|
|
||||||
echo "用法: $0 start [agent_type] [time_or_interval]"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -z "$3" ]; then
|
|
||||||
# 使用默认间隔
|
|
||||||
start_schedule_task "$2" "360"
|
|
||||||
else
|
|
||||||
start_schedule_task "$2" "$3"
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
|
|
||||||
stop)
|
|
||||||
stop_schedule_task
|
|
||||||
;;
|
|
||||||
|
|
||||||
status)
|
|
||||||
check_status
|
|
||||||
;;
|
|
||||||
|
|
||||||
*)
|
|
||||||
echo "用法: $0 {start|stop|status}"
|
|
||||||
echo " start [agent_type] [time_or_interval] - 启动定时任务"
|
|
||||||
echo " agent_type: crypto, gold 或 both"
|
|
||||||
echo " time_or_interval: 时间点(HH:MM)或间隔(分钟)"
|
|
||||||
echo " stop - 停止定时任务"
|
|
||||||
echo " status - 查看运行状态"
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
exit 0
|
|
||||||
Loading…
Reference in New Issue
Block a user