#!/bin/bash set -e # Exit on any error echo "🚀 Starting AI-Trader..." # Validate required environment variables echo "🔍 Validating environment variables..." MISSING_VARS=() if [ -z "$OPENAI_API_KEY" ]; then MISSING_VARS+=("OPENAI_API_KEY") fi if [ -z "$ALPHAADVANTAGE_API_KEY" ]; then MISSING_VARS+=("ALPHAADVANTAGE_API_KEY") fi if [ -z "$JINA_API_KEY" ]; then MISSING_VARS+=("JINA_API_KEY") fi if [ ${#MISSING_VARS[@]} -gt 0 ]; then echo "" echo "❌ ERROR: Missing required environment variables:" for var in "${MISSING_VARS[@]}"; do echo " - $var" done echo "" echo "Please set these variables in your .env file:" echo " 1. Copy .env.example to .env" echo " 2. Edit .env and add your API keys" echo " 3. Restart the container" echo "" echo "See docs/DOCKER.md for more information." exit 1 fi echo "✅ Environment variables validated" # Step 1: Data preparation echo "📊 Checking price data..." if [ -f "/app/data/merged.jsonl" ] && [ -s "/app/data/merged.jsonl" ]; then echo "✅ Using existing price data ($(wc -l < /app/data/merged.jsonl) stocks)" echo " To refresh data, delete /app/data/merged.jsonl and restart" else echo "📊 Fetching and merging price data..." # Run script from /app/scripts but output to /app/data # Note: get_daily_price.py now automatically calls merge_jsonl.py after fetching cd /app/data python /app/scripts/get_daily_price.py cd /app fi # Step 2: Start MCP services in background echo "🔧 Starting MCP services..." cd /app python agent_tools/start_mcp_services.py & MCP_PID=$! # Step 3: Wait for services to initialize echo "⏳ Waiting for MCP services to start..." sleep 3 # Step 4: Run trading agent with config file echo "🤖 Starting trading agent..." # Smart config selection: custom_config.json takes precedence if it exists if [ -f "configs/custom_config.json" ]; then CONFIG_FILE="configs/custom_config.json" echo "✅ Using custom configuration: configs/custom_config.json" elif [ -n "$1" ]; then CONFIG_FILE="$1" echo "✅ Using specified configuration: $CONFIG_FILE" else CONFIG_FILE="configs/default_config.json" echo "✅ Using default configuration: configs/default_config.json" fi python main.py "$CONFIG_FILE" # Cleanup on exit trap "echo '🛑 Stopping MCP services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT