# AutoTrade FlatTrade

NIFTY options algo trading SaaS built with **Laravel 12** + MySQL (PHP **8.2+**), integrated with [FlatTrade Pi API v2](https://pi.flattrade.in/docs).

## Architecture

| Component | Where it runs |
|-----------|----------------|
| Web app (dashboard, admin, auth) | Shared hosting or local |
| MySQL database | XAMPP (dev) / hosting DB (prod) |
| WebSocket worker | VPS with static IP |
| Bot scanner + monitor | VPS with static IP |

## Shared hosting deploy (PHP 8.2)

Upload the project, point the web root to `public/`, then on the server:

```bash
composer install --no-dev --optimize-autoloader
cp .env.example .env   # configure DB + APP_URL
php artisan key:generate
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

Build frontend assets locally (`npm run build`) and upload `public/build/` — most shared hosts cannot run Node.

## Local setup (Mac + XAMPP)

See [LOCAL_SETUP.md](LOCAL_SETUP.md)

```bash
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate:fresh --seed
npm install && npm run build
php artisan serve
```

**Logins:** `trader@autotrade.local` / `admin@autotrade.local` — password: `password`

## VPS workers (production)

Install Supervisor and use [deploy/supervisor.conf](deploy/supervisor.conf):

```bash
php artisan flattrade:websocket --mock   # dev/mock ticks
php artisan bot:scan --once              # entry scan cycle
php artisan bot:monitor --once           # exit monitor cycle
php artisan market:sync                  # instrument + greeks sync
```

Cron on shared hosting (web only):

```cron
* * * * * cd /path/to/app && php artisan schedule:run >> /dev/null 2>&1
```

## FlatTrade credentials model

| Who | Credentials used for |
|-----|-------------------|
| **Admin** | Shared WebSocket market feed, option chain, Greeks (all users see this data) |
| **Each user** | Their own orders, balance, positions, P&L sync |

Both admin and users add API Key + Secret on **FlatTrade Account** page, then complete OAuth daily.

`.env` `FLATTRADE_API_KEY` is optional fallback for dev mock only — production uses per-account credentials.

```env
FLATTRADE_MOCK=false
FLATTRADE_API_KEY=your_api_key
FLATTRADE_API_SECRET=your_api_secret
FLATTRADE_REDIRECT_URI=https://yourdomain.com/callback
```

Register the redirect URL and VPS static IP in FlatTrade API settings.

## Build progress

Track completion in [BUILD_CHECKLIST.md](BUILD_CHECKLIST.md).

## Testing

```bash
php artisan test
```
