diegobeyl/backtesting
2
1# Backtesting App V2 - Launcher Script
2# Inicia API y Streamlit con un solo comando
3
4param(
5 [switch]$NoBrowser
6)
7
8$ErrorActionPreference = "SilentlyContinue"
9$ProjectRoot = $PSScriptRoot
10
11Write-Host ""
12Write-Host "========================================"
13Write-Host " BACKTESTING APP V2 - LAUNCHER"
14Write-Host "========================================"
15Write-Host ""
16
17# Verificar que estamos en el directorio correcto
18if (-not (Test-Path "$ProjectRoot\.env")) {
19 Write-Host "ERROR: Archivo .env no encontrado" -ForegroundColor Red
20 Write-Host " Ejecuta primero: .\setup.ps1" -ForegroundColor Yellow
21 Write-Host ""
22 exit 1
23}
24
25# Verificar entorno virtual
26if (-not (Test-Path "$ProjectRoot\venv\Scripts\python.exe")) {
27 Write-Host "ERROR: Entorno virtual no encontrado" -ForegroundColor Red
28 Write-Host " Ejecuta primero: .\setup.ps1" -ForegroundColor Yellow
29 Write-Host ""
30 exit 1
31}
32
33Write-Host "OK - Verificaciones pasadas" -ForegroundColor Green
34Write-Host ""
35
36# Crear directorios necesarios
37New-Item -ItemType Directory -Force -Path "$ProjectRoot\logs", "$ProjectRoot\cache" -ErrorAction SilentlyContinue | Out-Null
38
39# Matar procesos previos si existen
40Write-Host "Verificando procesos existentes..." -ForegroundColor Yellow
41$apiProcess = Get-Process python -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like "*uvicorn*" }
42$streamlitProcess = Get-Process python -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like "*streamlit*" }
43
44if ($apiProcess) {
45 Write-Host " Deteniendo API anterior..." -ForegroundColor Yellow
46 Stop-Process -InputObject $apiProcess -Force -ErrorAction SilentlyContinue
47 Start-Sleep -Seconds 1
48}
49
50if ($streamlitProcess) {
51 Write-Host " Deteniendo Streamlit anterior..." -ForegroundColor Yellow
52 Stop-Process -InputObject $streamlitProcess -Force -ErrorAction SilentlyContinue
53 Start-Sleep -Seconds 1
54}
55
56Write-Host ""
57
58# Iniciar FastAPI
59Write-Host "Iniciando FastAPI Backend..." -ForegroundColor Cyan
60$pythonPath = "$ProjectRoot\venv\Scripts\python.exe"
61$apiCommand = "cd '$ProjectRoot'; & '$pythonPath' -m uvicorn api.main:app --host 0.0.0.0 --port 8000"
62
63Start-Process powershell -ArgumentList "-NoExit", "-Command", $apiCommand -WindowStyle Minimized
64Start-Sleep -Seconds 3
65
66# Verificar que API está corriendo
67try {
68 $health = Invoke-RestMethod -Uri http://localhost:8000/health -TimeoutSec 3 -ErrorAction Stop
69 Write-Host " OK - API activa (v$($health.version))" -ForegroundColor Green
70} catch {
71 Write-Host " NOTA - API iniciando..." -ForegroundColor Yellow
72}
73
74# Iniciar Streamlit
75Write-Host "Iniciando Streamlit UI..." -ForegroundColor Magenta
76$streamlitPath = "$ProjectRoot\backtesting_app\backtesting_app"
77$streamlitCommand = "cd '$streamlitPath'; & '$pythonPath' -m streamlit run app.py --server.port 8501"
78
79Start-Process powershell -ArgumentList "-NoExit", "-Command", $streamlitCommand -WindowStyle Minimized
80Start-Sleep -Seconds 5
81
82# Verificar que Streamlit está corriendo
83try {
84 $streamlit = Invoke-WebRequest -Uri http://localhost:8501 -UseBasicParsing -TimeoutSec 3 -ErrorAction Stop
85 Write-Host " OK - Streamlit activo" -ForegroundColor Green
86} catch {
87 Write-Host " NOTA - Streamlit iniciando..." -ForegroundColor Yellow
88}
89
90Write-Host ""
91Write-Host "========================================"
92Write-Host " OK - APLICACION INICIADA"
93Write-Host "========================================"
94Write-Host ""
95Write-Host "Servicios disponibles:" -ForegroundColor Yellow
96Write-Host ""
97Write-Host " Streamlit UI: http://localhost:8501" -ForegroundColor Cyan
98Write-Host " FastAPI: http://localhost:8000" -ForegroundColor Cyan
99Write-Host " API Docs: http://localhost:8000/docs" -ForegroundColor Cyan
100Write-Host " Health Check: http://localhost:8000/health" -ForegroundColor Cyan
101Write-Host ""
102Write-Host "Para detener servicios: .\stop.ps1" -ForegroundColor Yellow
103Write-Host ""
104
105# Abrir navegador si no se especificó -NoBrowser
106if (-not $NoBrowser) {
107 Write-Host "Abriendo navegador..." -ForegroundColor Cyan
108 Start-Sleep -Seconds 2
109 Start-Process "http://localhost:8501"
110}
111
112Write-Host "Listo para usar!" -ForegroundColor Green
113Write-Host ""
114 