# TradingView Automation - Credential Usage

## Overview

You now have **two ways** to provide TradingView credentials:

### Method 1: Environment Variables (.env file) - Like the original
```bash
# In your .env file
TRADINGVIEW_EMAIL=your-email@example.com
TRADINGVIEW_PASSWORD=your-password
```

### Method 2: API Parameters - More flexible
```json
{
  "symbol": "SOLUSD",
  "timeframe": "5",
  "credentials": {
    "email": "your-email@example.com", 
    "password": "your-password"
  }
}
```

## Usage Examples

### 1. Using .env credentials (like original tradingview.ts)

```bash
# Set in .env file first
echo "TRADINGVIEW_EMAIL=your-email@example.com" >> .env
echo "TRADINGVIEW_PASSWORD=your-password" >> .env

# Then call API without credentials
curl -X POST http://localhost:3000/api/trading/automated-analysis \
  -H "Content-Type: application/json" \
  -d '{
    "symbol": "SOLUSD",
    "timeframe": "5"
  }'
```

### 2. Using API parameters (more secure for multi-user)

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

### 3. Programmatic usage

```typescript
import { enhancedScreenshotService } from './lib/enhanced-screenshot'

// Method 1: Using .env (no credentials needed)
const screenshots1 = await enhancedScreenshotService.captureWithLogin({
  symbol: 'SOLUSD',
  timeframe: '5'
  // credentials will be read from .env automatically
})

// Method 2: Explicit credentials
const screenshots2 = await enhancedScreenshotService.captureWithLogin({
  symbol: 'SOLUSD', 
  timeframe: '5',
  credentials: {
    email: 'user@example.com',
    password: 'password'
  }
})
```

## Key Differences from Original

| Feature | tradingview.ts (Original) | tradingview-automation.ts (New) |
|---------|---------------------------|----------------------------------|
| Framework | Puppeteer | Playwright |
| Credentials | Only .env | .env OR parameters |
| Usage | Complex layouts | Simple AI analysis |
| Video Recording | ✅ Yes | ❌ No |
| Code Size | 777 lines | ~400 lines |
| Docker Optimized | ✅ Yes | ✅ Yes |
| Debug Screenshots | ✅ Extensive | ✅ Basic |

## Priority Order for Credentials

1. **API parameters** (if provided)
2. **Environment variables** (.env file)
3. **Error** if neither is available

## Security Considerations

- **Environment variables**: Good for single-user setups
- **API parameters**: Better for multi-user applications
- **Never commit credentials** to git repositories
- Use `.env.local` for local development

## Migration from Original

If you're currently using the original `tradingview.ts`:

1. **Keep using .env**: No changes needed, just call the new API
2. **Switch to parameters**: More flexible, supports multiple users
3. **Hybrid approach**: Use .env as fallback, parameters for specific users
