Modern trading analytics application built with FastAPI, Next.js, and Supabase.
- Backend: FastAPI (Python) with async/await
- Frontend: Next.js 14 with TypeScript and Tailwind CSS
- Database: Supabase (PostgreSQL)
- Authentication: Supabase Auth
- Real-time: WebSocket integration
- Caching: Redis for performance
- Deployment: Docker containers
- New Data Detection System - Smart background data checking without page refreshes
- Options Calculation Guide - Detailed options P&L calculations
- P&L Analytics Plan - Comprehensive P&L analysis features
- Database Migrations - Database schema and migration process
- Enhanced Chains Documentation - Options chain analysis
- Roll Detection Logic - Rolled options detection
- Migration Quick Reference - Quick migration commands
- 📊 Real-time portfolio tracking
- 📈 Advanced options trading analysis
- 🎯 Multi-leg strategy visualization
- 📱 Mobile-responsive design
- ⚡ Real-time price updates
- 🔐 Secure authentication
- 📄 Export capabilities
- 🌙 Dark/light theme support
Before you begin, ensure you have the following installed:
-
Docker Desktop (Recommended for easiest setup)
- macOS: Download Docker Desktop for Mac
- Windows: Download Docker Desktop for Windows
- Linux: Install Docker Engine
-
Node.js 18+ (for frontend development)
- Download from nodejs.org
- Or install via nvm:
nvm install 18 nvm use 18
-
Python 3.11+ (for backend development)
- macOS:
brew install python@3.11 - Windows: Download from python.org
- Linux:
sudo apt install python3.11
- macOS:
-
Git
- macOS:
brew install gitor included with Xcode Command Line Tools - Windows: Download Git for Windows
- Linux:
sudo apt install git
- macOS:
This method runs everything in containers and requires minimal setup. Perfect for first-time setup!
Step-by-step setup from scratch:
-
Install Docker Desktop
- Download and install Docker Desktop for your OS (see Prerequisites above)
- Important: Start Docker Desktop and wait for it to be running (you'll see the Docker icon in your menu bar/system tray)
-
Clone the repository:
git clone https://github.com/krabhishek8260/trade-analytics.git cd trade-analytics -
Create environment files (optional but recommended):
Create
backend/.envwith minimal configuration:# Create the backend .env file cat > backend/.env << 'EOF' DATABASE_URL=postgresql://postgres:postgres@localhost:5432/tradeanalytics REDIS_URL=redis://localhost:6379 JWT_SECRET=local-dev-secret-key-change-in-production EOF
Create
frontend/.env.local(optional):# Create the frontend .env.local file cat > frontend/.env.local << 'EOF' NEXT_PUBLIC_API_URL=http://localhost:8000 EOF
Note: You can skip this step and the app will use defaults from
docker-compose.yml -
Start all services:
docker-compose up -d
This command will:
- Download required Docker images (first time only, ~5 minutes)
- Create PostgreSQL database container
- Create Redis cache container
- Build and start Backend API container
- Build and start Frontend container
First-time setup takes 5-10 minutes to download images and build containers.
-
Verify everything is running:
docker-compose ps
You should see all 4 services with status "Up" and "healthy":
tradeanalytics_postgrestradeanalytics_redistradeanalytics_backendtradeanalytics_frontend
-
Access the application:
- Frontend: http://localhost:3000 (Main application)
- Backend API: http://localhost:8000 (API endpoints)
- API Documentation: http://localhost:8000/docs (Interactive API docs)
-
View logs (if needed):
# View all logs docker-compose logs -f # View specific service logs docker logs tradeanalytics_frontend docker logs tradeanalytics_backend
-
Stop the services when done:
# Stop all services (preserves data) docker-compose down # Stop and remove all data (clean slate) docker-compose down -v
Troubleshooting:
- If ports are already in use, stop other services using ports 3000, 8000, 5432, or 6379
- If containers fail to start, check logs:
docker-compose logs - If you get "permission denied" errors on Linux, add your user to docker group:
sudo usermod -aG docker $USER
If you want to run services individually for development:
-
Clone the repository:
git clone https://github.com/krabhishek8260/trade-analytics.git cd trade-analytics -
Install dependencies:
Frontend:
cd frontend npm install cd ..
Backend:
cd backend pip install -r requirements.txt cd ..
-
Start required services (PostgreSQL and Redis):
# Start only database and cache services docker-compose up -d postgres redis -
Run the backend (in a new terminal):
cd backend uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 -
Run the frontend (in another terminal):
cd frontend npm run dev -
Access the application:
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
When using Docker Compose, PostgreSQL and Redis are automatically configured. You don't need to install them separately.
Default credentials (already configured in docker-compose.yml):
- PostgreSQL:
postgres:postgres@localhost:5432/tradeanalytics - Redis:
localhost:6379
Supabase provides authentication and additional database features. You can:
Option A: Skip Supabase (Use Demo Mode)
- The app works in demo mode without Supabase
- Skip the Supabase environment variables
- Authentication will use a demo user
Option B: Set up Supabase (For production-like setup)
-
Create a Supabase account (free tier available)
- Go to supabase.com
- Create a new account
- Create a new project
-
Get your Supabase credentials:
- Go to Project Settings → API
- Copy the
Project URL(this is yourSUPABASE_URL) - Copy the
anon publickey (this is yourSUPABASE_ANON_KEY)
-
Configure environment variables (see below)
This section explains how to get all the values needed for your environment files.
Create backend/.env with the following variables:
If using Docker (recommended):
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/tradeanalyticsThis is already configured in docker-compose.yml. No setup needed!
Format explained:
postgresql://- Database typepostgres:postgres- username:password@localhost:5432- host:port/tradeanalytics- database name
Option A: Using Docker (recommended):
REDIS_URL=redis://localhost:6379This is already configured in docker-compose.yml. No setup needed!
Option B: Local Redis Installation
If you want to run Redis directly on your machine instead of Docker:
-
Install Redis:
# macOS brew install redis # Ubuntu/Debian sudo apt-get install redis-server # Windows (via WSL or download from redis.io) # Or use: choco install redis-64
-
Start Redis:
# macOS (using Homebrew) brew services start redis # Or start manually redis-server # Ubuntu/Debian sudo systemctl start redis-server
-
Verify Redis is running:
redis-cli ping # Should return: PONG -
Use in your
.env:REDIS_URL=redis://localhost:6379
-
Stop Redis when done:
# macOS (using Homebrew) brew services stop redis # Ubuntu/Debian sudo systemctl stop redis-server
Format explained:
redis://- Protocollocalhost:6379- host:port (default Redis port)
For local development:
JWT_SECRET=local-dev-secret-key-change-in-productionThis can be any random string. For production, use a secure random string:
# Generate a secure secret (run this in terminal)
openssl rand -hex 32Option A: Skip Supabase (Demo Mode)
Simply don't include these variables. The app will work in demo mode.
Option B: Local Supabase (Recommended for Development)
Run Supabase locally using Docker. This is free and doesn't require a Supabase account.
-
Install Supabase CLI:
# macOS/Linux brew install supabase/tap/supabase # Or via npm (all platforms) npm install -g supabase
-
Initialize Supabase in your project:
# Run from project root supabase init -
Start local Supabase:
supabase start
This will:
- Download Supabase Docker images (~2GB first time)
- Start PostgreSQL, Auth, Storage, and other services
- Take 2-5 minutes on first run
-
Get your local credentials:
After
supabase startcompletes, you'll see output like:API URL: http://localhost:54321 anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0 service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Copy these values to your
.env:SUPABASE_URL=http://localhost:54321 SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
-
Access Supabase Studio (optional):
Open http://localhost:54323 in your browser to access the Supabase admin dashboard.
-
Stop local Supabase when done:
supabase stop
Useful commands:
supabase status # Check if Supabase is running
supabase stop # Stop all Supabase containers
supabase db reset # Reset database to initial stateOption C: Cloud Supabase (Production Setup)
Use the hosted Supabase service for production or if you prefer cloud setup.
-
Go to supabase.com and sign up/login
-
Create a new project:
- Click "New Project"
- Choose your organization (or create one)
- Enter project details:
- Name:
tradeanalytics(or any name you prefer) - Database Password: Choose a strong password (save this!)
- Region: Choose closest to you
- Name:
- Click "Create new project"
- Wait 2-3 minutes for setup to complete
-
Get your credentials:
- Once project is ready, go to Project Settings (gear icon in sidebar)
- Click API in the left menu
- You'll see:
Project URL:
https://xxxxxxxxxxxxx.supabase.coCopy this as your
SUPABASE_URLanon public key: (under "Project API keys")
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Copy this as your
SUPABASE_ANON_KEY -
Add to your
.envfile:SUPABASE_URL=https://xxxxxxxxxxxxx.supabase.co SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Minimal (Docker + Demo Mode):
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/tradeanalytics
REDIS_URL=redis://localhost:6379
JWT_SECRET=local-dev-secret-key-change-in-productionFull (with Supabase):
# Database & Cache (automatically configured by Docker)
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/tradeanalytics
REDIS_URL=redis://localhost:6379
# Security
JWT_SECRET=your-generated-secret-key-here
# Supabase Authentication
SUPABASE_URL=https://xxxxxxxxxxxxx.supabase.co
SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Create frontend/.env.local with the following variables:
For local development:
NEXT_PUBLIC_API_URL=http://localhost:8000This tells the frontend where to find your backend API.
Note: The NEXT_PUBLIC_ prefix is required for Next.js to expose this variable to the browser.
If you set up Supabase in the backend:
Use the same values you got from Supabase:
NEXT_PUBLIC_SUPABASE_URL=https://xxxxxxxxxxxxx.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...If you're using Demo Mode:
Skip these variables - they're not needed.
Minimal (Demo Mode):
NEXT_PUBLIC_API_URL=http://localhost:8000Full (with Supabase):
NEXT_PUBLIC_API_URL=http://localhost:8000
NEXT_PUBLIC_SUPABASE_URL=https://xxxxxxxxxxxxx.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...If you just want to run the app quickly without authentication:
backend/.env (minimal):
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/tradeanalytics
REDIS_URL=redis://localhost:6379
JWT_SECRET=local-dev-secret-keyfrontend/.env.local (minimal):
NEXT_PUBLIC_API_URL=http://localhost:8000This will run the app with:
- ✅ Local PostgreSQL database (via Docker)
- ✅ Local Redis cache (via Docker)
- ✅ Demo mode authentication (no Supabase needed)
cd backend
pip install -r requirements.txt
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000cd frontend
npm install
npm run devcd backend
alembic upgrade headtradeanalytics-v2/
├── backend/ # FastAPI backend
│ ├── app/
│ │ ├── api/ # API routes
│ │ ├── core/ # Configuration, security
│ │ ├── models/ # Pydantic models
│ │ ├── services/ # Business logic
│ │ ├── database/ # Database models
│ │ └── utils/ # Utilities
│ ├── tests/
│ ├── requirements.txt
│ └── Dockerfile
├── frontend/ # Next.js frontend
│ ├── src/
│ │ ├── app/ # App Router
│ │ ├── components/ # React components
│ │ ├── hooks/ # Custom hooks
│ │ ├── lib/ # Utilities
│ │ ├── types/ # TypeScript types
│ │ └── store/ # State management
│ ├── package.json
│ └── Dockerfile
├── database/ # Database migrations
├── docker-compose.yml # Local development
└── README.md
The API documentation is automatically generated and available at /docs when running the backend.
cd backend
pytestcd frontend
npm testIf you encounter 404 errors for static chunks like:
GET /_next/static/chunks/main-app.js 404
GET /_next/static/css/app/layout.css 404
Quick Fix:
cd frontend
npm run dev:cleanComplete Reset:
cd frontend
./dev-reset.shManual Cleanup:
cd frontend
# Stop all Next.js processes
pkill -f "next dev" 2>/dev/null || true
# Clear all caches
rm -rf .next
rm -rf node_modules/.cache
rm -rf .turbo
# Reinstall dependencies
npm install
# Start fresh
npm run devIf you see "Port 3000 is in use" errors:
# Find and kill processes using port 3000
lsof -ti:3000 | xargs kill -9
# Or use a different port
npm run dev -- -p 3001If you experience build or compilation issues:
cd frontend
# Clear all caches and node_modules
rm -rf .next node_modules package-lock.json
npm install
npm run devcd backend
# Check database status
docker-compose ps
# Restart database
docker-compose restart postgres
# Reset database (WARNING: This will delete all data)
docker-compose down -v
docker-compose up -d# Check if backend is running
curl http://localhost:8000/health
# Check API documentation
open http://localhost:8000/docscd backend
# Recreate virtual environment
rm -rf venv
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt# Check container logs
docker-compose logs
# Rebuild containers
docker-compose down
docker-compose build --no-cache
docker-compose up -d# Clear all volumes (WARNING: This will delete all data)
docker-compose down -v
docker volume prune -f
docker-compose up -d# Frontend
cd frontend
rm -rf node_modules package-lock.json
npm install
# Backend
cd backend
pip install -r requirements.txt# Fix file permissions
chmod +x frontend/dev-reset.sh
chmod +x backend/scripts/*.sh # if any scripts exist# Find and kill processes
lsof -ti:3000 | xargs kill -9 # Frontend
lsof -ti:8000 | xargs kill -9 # BackendThe project includes several helpful scripts for development:
Frontend Scripts:
npm run dev- Start development servernpm run dev:clean- Clear cache and restartnpm run dev:fresh- Complete reset with dependency reinstallnpm run dev:reset- Reset with Turbo mode./dev-reset.sh- Complete environment reset
Backend Scripts:
uvicorn app.main:app --reload- Start development serverpytest- Run testsalembic upgrade head- Apply database migrations
If you're still experiencing issues:
- Check the logs:
docker-compose logs -f - Verify environment variables are set correctly
- Ensure all dependencies are installed
- Try the complete reset procedures above
- Check the Issues page for known problems
The application can be deployed using Docker containers or on cloud platforms like Vercel (frontend) and Railway (backend).
MIT License
A rolled options chain will have multiple orders. The first order will have a │ │ sell to open/buy to open only. subsequent orders should have 2 legs. if the first │ │ order was sell to open call/put then the subsequenr order should a buy to clsoe │ │ sell/put(same sell or put) with same strike price. if the first order was buy to │ │ open then the subsequent order should have a sell to close(same sell or put). the │ │ last order in the chain can have just one oppsoite end. for sell to open chain │ │ the last should be buy to close and opposite for one with first order buy to open │ │ which should be sell to close