# ============================================ # 系统启动脚本 (Windows PowerShell) # 启动 API 服务和自动交易程序 # ============================================ $ErrorActionPreference = "Stop" function Write-Log { param([string]$Message, [string]$Color = "White") $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Write-Host "[$timestamp] $Message" -ForegroundColor $Color } Write-Log "正在启动系统..." "Yellow" # 检查 conda $condaPath = Get-Command conda -ErrorAction SilentlyContinue if (-not $condaPath) { Write-Log "错误: 未找到 conda,请先安装 Anaconda 或 Miniconda" "Red" Read-Host "按回车键退出" exit 1 } # 激活 conda 环境 Write-Log "激活 conda 环境: crypto_pred_312" "Yellow" try { conda activate crypto_pred_312 } catch { Write-Log "错误: 无法激活 conda 环境 crypto_pred_312" "Red" Write-Log "请先创建环境: conda create -n crypto_pred_312 python=3.12" "Yellow" Read-Host "按回车键退出" exit 1 } # 启动 API 服务 Write-Log "启动 API 服务..." "Yellow" $apiProcess = Start-Process -FilePath "python" ` -ArgumentList "-m uvicorn api.main:app --host 0.0.0.0 --port 8000" ` -PassThru -NoNewWindow Write-Log "API 服务已启动 (PID: $($apiProcess.Id))" "Green" # 等待 API 启动 Start-Sleep -Seconds 3 # 启动自动交易程序 Write-Log "启动自动交易程序..." "Yellow" $tradeProcess = Start-Process -FilePath "python" ` -ArgumentList "-u auto_trade_polymarket.py" ` -PassThru -NoNewWindow Write-Log "自动交易程序已启动 (PID: $($tradeProcess.Id))" "Green" Write-Host "" Write-Host "============================================" -ForegroundColor Cyan Write-Host " 系统启动完成!" -ForegroundColor Green Write-Host " 前端访问: http://localhost:8000" -ForegroundColor Green Write-Host " 按 Ctrl+C 停止所有服务" -ForegroundColor Yellow Write-Host "============================================" -ForegroundColor Cyan Write-Host "" # 等待用户中断 try { while ($true) { # 检查进程状态 if ($apiProcess.HasExited) { Write-Log "API 服务意外退出" "Red" break } if ($tradeProcess.HasExited) { Write-Log "自动交易程序意外退出" "Red" break } Start-Sleep -Seconds 1 } } finally { Write-Log "正在停止服务..." "Yellow" if (-not $apiProcess.HasExited) { Stop-Process -Id $apiProcess.Id -Force -ErrorAction SilentlyContinue Write-Log "API 服务已停止" "Green" } if (-not $tradeProcess.HasExited) { Stop-Process -Id $tradeProcess.Id -Force -ErrorAction SilentlyContinue Write-Log "自动交易程序已停止" "Green" } Write-Log "所有服务已停止" "Green" }