# Docker Compose v2 Commands

This project uses Docker Compose v2. Here are the most common commands:

## Available npm Scripts

### Development Scripts
```bash
# Build and start for development (with override)
npm run docker:dev                    # docker compose up --build
npm run docker:dev:detached          # docker compose up -d --build

# Basic operations
npm run docker:build                  # docker compose build
npm run docker:up                     # docker compose up
npm run docker:up:build              # docker compose up --build
npm run docker:up:detached           # docker compose up -d
npm run docker:down                  # docker compose down
npm run docker:down:volumes          # docker compose down -v
```

### Production Scripts
```bash
# Production environment
npm run docker:prod:build            # Build for production
npm run docker:prod:up               # Start production stack
npm run docker:prod:down             # Stop production stack
npm run docker:prod:logs             # View production logs
npm run docker:prod:restart          # Restart production app
```

### Maintenance Scripts
```bash
# Monitoring and debugging
npm run docker:logs                  # View app logs
npm run docker:ps                    # Show running containers
npm run docker:exec                  # Access container shell
npm run docker:health                # Health check
npm run docker:restart               # Restart app service

# Cleanup and reset
npm run docker:clean                 # Stop and clean volumes + system prune
npm run docker:reset                 # Complete reset with no-cache rebuild
npm run docker:pull                  # Pull latest images

# Testing
npm run test:docker                  # Run automation tests
```

## Basic Commands

```bash
# Build and start containers
npm run docker:up:build
# or directly:
docker compose up --build

# Start containers (without building)
npm run docker:up
# or directly:
docker compose up -d

# Stop containers
npm run docker:down
# or directly:
docker compose down

# View logs
npm run docker:logs
# or directly:
docker compose logs -f app

# Execute commands in container
npm run docker:exec
# or directly:
docker compose exec app bash

# Restart just the app service
npm run docker:restart
# or directly:
docker compose restart app
```

## Environment-Specific Workflows

### Development Workflow
```bash
# 1. Start development environment (uses docker-compose.override.yml automatically)
npm run docker:dev

# 2. View logs in real-time
npm run docker:logs

# 3. Access container for debugging
npm run docker:exec

# 4. Stop when done
npm run docker:down
```

### Production Workflow
```bash
# 1. Build production images
npm run docker:prod:build

# 2. Start production stack
npm run docker:prod:up

# 3. Monitor production logs
npm run docker:prod:logs

# 4. Health check
npm run docker:health

# 5. Stop production stack
npm run docker:prod:down
```

## Docker Compose v2 vs v1

This project uses Docker Compose v2 (`docker compose`) instead of v1 (`docker-compose`):

- **v2**: `docker compose up`
- **v1**: `docker-compose up` (deprecated)

Docker Compose v2 is integrated into Docker Desktop and provides better performance and features.

# TradingView Automation in Docker

This document explains how to use the automated TradingView analysis system in a Docker container environment.

## Overview

The automation system:
1. **Logs into TradingView** using your credentials
2. **Navigates to the specified chart** (e.g., SOL/USD)
3. **Sets the timeframe** (5min, 15min, etc.)
4. **Takes a screenshot** of the chart
5. **Analyzes it with AI** (OpenAI GPT-4 Vision)
6. **Returns trading recommendations**

## Docker Setup

### 1. Environment Variables

Copy `.env.docker` to `.env.local` and configure:

```bash
# Required
OPENAI_API_KEY=your_openai_api_key

# Optional (can be passed via API)
TRADINGVIEW_EMAIL=your_email@example.com
TRADINGVIEW_PASSWORD=your_password
```

### 2. Start the Docker Container

```bash
docker compose up --build
```

This will:
- Install all dependencies including Playwright
- Set up Chromium browser for automation
- Start the Next.js application on port 3000

## Usage

### Method 1: API Endpoint

#### Health Check
```bash
curl -X GET http://localhost:3000/api/trading/automated-analysis
```

#### Run Analysis
```bash
curl -X POST http://localhost:3000/api/trading/automated-analysis \
  -H "Content-Type: application/json" \
  -d '{
    "symbol": "SOLUSD",
    "timeframe": "5",
    "credentials": {
      "email": "your-email@example.com",
      "password": "your-password"
    }
  }'
```

### Method 2: Web Interface

1. Navigate to `http://localhost:3000`
2. Use the "Automated Trading Panel" component
3. Enter your TradingView credentials
4. Select symbol and timeframe
5. Click "Start Automated Analysis"

### Method 3: Test Script

Run the included test script:
```bash
./test-docker-automation.sh
```

## Supported Symbols

- `SOLUSD` (Solana)
- `BTCUSD` (Bitcoin)
- `ETHUSD` (Ethereum)
- Any symbol available on TradingView

## Supported Timeframes

- `1` (1 minute)
- `5` (5 minutes)
- `15` (15 minutes)
- `30` (30 minutes)
- `60` (1 hour)

## Response Format

```json
{
  "success": true,
  "data": {
    "screenshots": ["SOLUSD_5_1234567890_ai.png"],
    "analysis": {
      "summary": "Market showing bearish sentiment...",
      "marketSentiment": "BEARISH",
      "recommendation": "SELL",
      "confidence": 85,
      "reasoning": "Price rejected from VWAP resistance...",
      "entry": {
        "price": 149.50,
        "buffer": "±0.20",
        "rationale": "Entry on rejection from VWAP"
      },
      "stopLoss": {
        "price": 151.00,
        "rationale": "Above VWAP resistance"
      },
      "takeProfits": {
        "tp1": { "price": 148.00, "description": "Previous support" },
        "tp2": { "price": 146.50, "description": "Extended target" }
      },
      "riskToReward": "1:2.5",
      "confirmationTrigger": "Bearish engulfing candle on VWAP rejection",
      "indicatorAnalysis": {
        "rsi": "RSI at 68, approaching overbought",
        "vwap": "Price rejected from VWAP resistance",
        "obv": "OBV showing divergence"
      }
    },
    "symbol": "SOLUSD",
    "timeframe": "5",
    "timestamp": "2025-07-11T13:10:00.000Z"
  }
}
```

## Docker Configuration Details

### Browser Settings
- **Headless Mode**: Enabled for Docker
- **Chromium Path**: `/usr/bin/chromium`
- **No Sandbox**: Required for container security
- **Disabled GPU**: For stability in containers

### Security Considerations
- Credentials are only used for the session
- Screenshots are stored locally in the container
- Browser data is not persisted between runs

### Performance Notes
- Initial startup may take 30-60 seconds
- Each analysis takes approximately 45-90 seconds
- Screenshots are automatically cleaned up

## Troubleshooting

### Common Issues

1. **Login Failed**
   - Check TradingView credentials
   - Verify captcha handling (may need manual intervention)
   - Check if account has 2FA enabled

2. **Screenshot Failed**
   - Ensure chart loaded completely
   - Check network connectivity
   - Verify symbol exists on TradingView

3. **AI Analysis Failed**
   - Check OpenAI API key
   - Verify screenshot was captured
   - Check OpenAI quota/billing

### Debug Mode

To enable debug screenshots:
```bash
# Add to docker-compose.yml environment
- DEBUG_SCREENSHOTS=true
```

This will save additional debug screenshots during the process.

### Logs

View container logs:
```bash
docker compose logs -f app
```

## Integration Examples

### With Trading Bot
```typescript
import { enhancedScreenshotService } from './lib/enhanced-screenshot'
import { aiAnalysisService } from './lib/ai-analysis'

const analysis = await enhancedScreenshotService.captureWithLogin({
  symbol: 'SOLUSD',
  timeframe: '5',
  credentials: { email: 'user@example.com', password: 'password' }
})

if (analysis.length > 0) {
  const aiResult = await aiAnalysisService.analyzeScreenshot(analysis[0])
  
  if (aiResult?.recommendation === 'BUY') {
    // Execute buy order
  }
}
```

### Batch Analysis
```typescript
const configs = [
  { symbol: 'SOLUSD', timeframe: '5' },
  { symbol: 'BTCUSD', timeframe: '15' },
  { symbol: 'ETHUSD', timeframe: '5' }
]

const results = await enhancedScreenshotService.captureBatch(
  configs, 
  credentials
)
```

## Limitations

- Requires TradingView account
- Rate limited by TradingView's anti-bot measures
- Captcha may require manual intervention
- Screenshot quality depends on chart layout
- AI analysis accuracy varies with market conditions

## Support

For issues or questions:
1. Check the logs first
2. Verify all environment variables are set
3. Test with the health check endpoint
4. Review the debug screenshots if available
