A production-grade AI-powered paper trading platform that helps students learn investing using virtual money with real NSE market data.
StockWise is a full-stack paper trading simulator built for students who want to learn stock market investing without financial risk. Users start with a virtual balance and trade real NSE-listed stocks at real market prices. Every trade, every gain, every loss β all simulated with real data.
The platform goes beyond a basic simulator. It features a live AI chatbot (FinBot) powered by LLaMA 3.3 70B, post-trade ML behavioral analysis, a competitive leaderboard, daily market stories, and a real-time 3D trading interface built with Three.js and Anime.js.
Built as a hackathon project in 48 hours. Fully deployed and production-ready.
- JWT-based authentication with access and refresh token rotation
- Atomic user creation β one request simultaneously creates the user account, wallet (with starting virtual balance), and portfolio using Django's atomic transactions
- Zero race conditions on account creation β if any step fails, everything rolls back
- Live NSE stock prices fetched from yfinance and Finnhub APIs
- Three-layer caching with Redis β price data cached to avoid redundant API calls
- WebSocket streaming via Django Channels β frontend receives live price updates every 5 seconds
- Fallback mechanism between data sources β if one API fails, the other takes over
- Weekend/off-hours price simulation for continuous demo availability
- Supports three order types:
- Market Orders β execute immediately at current price
- Limit Orders β execute only when stock hits target price
- Stop-Loss Orders β automatically sell to prevent runaway losses
- Race condition prevention using
select_for_update()database locking β multiple users buying the same stock simultaneously never causes balance corruption - Full trade history with timestamps, P&L per trade, and order status tracking
- Celery-powered background worker processes pending limit and stop-loss orders continuously
- Daily portfolio snapshots stored for historical performance tracking
- Comprehensive analytics: win rate, average win, average loss, expectancy, profit factor
- Per-stock breakdown showing which tickers the user performs best and worst on
- Platform-wide leaderboard ranking users by portfolio performance
- P&L history charts with full trade-by-trade breakdown
- Powered by Groq API running LLaMA 3.3 70B β one of the fastest LLM inference providers
- Full conversation history maintained per user across sessions
- Real-time price enrichment β if you ask about TCS, FinBot automatically fetches the live price and includes it in context
- Multi-layer prompt injection protection:
- Layer 1: Pattern-based injection detection (blocks jailbreak attempts, persona switching, override commands)
- Layer 2: Finance topic validation β only finance-related queries reach the LLM
- Layer 3: System prompt hardening with explicit security rules baked into every request
- Automatic model fallback β if primary model fails, system tries backup models automatically
- FinBot refuses to discuss anything outside finance, markets, and investing β no exceptions
- Post-trade behavioral analysis triggered automatically after every completed trade
- Analyzes trading patterns including:
- Win rate and confidence intervals
- Profit factor (total wins / total losses)
- Expectancy β average expected return per trade
- Holding time analysis β compares how long the user holds winning vs losing positions
- Segmentation by time of day, day of week, market direction, and volatility regime
- Generates a full natural language AI report using Groq, personalized to the user's specific trading patterns
- Detects behavioral biases like panic selling, holding losers too long, and overtrading
- Celery Beat scheduler automatically generates daily market stories
- AI-written summaries of market conditions, sector performance, and notable stock movements
- Delivered fresh every trading day
StockWise Backend
βββ core/ # Django project settings, URLs, Celery config
βββ users/ # Auth, JWT, user management
βββ market/ # Price fetching, WebSocket, search, top movers
β βββ tasks.py # Celery tasks: price cache warming
β βββ consumers.py # Django Channels WebSocket consumer
βββ trading/ # Orders, portfolio, leaderboard, analytics
β βββ views.py # Buy, sell, portfolio, history, insights endpoints
β βββ tasks.py # Celery tasks: limit/stop-loss order processing
βββ chatbot/ # FinBot conversation management
βββ stories/ # Daily AI market stories
βββ services/
βββ chatbot_services.py # Groq integration, injection protection, ML prompts
βββ price_service.py # yfinance + Finnhub with Redis caching
Frontend (React/Vite)
β REST API calls (axios + JWT)
Django REST Framework
β Database operations
PostgreSQL (primary data store)
β Cache layer
Redis (price cache + Celery broker)
β Background tasks
Celery Worker (order processing, cache warming)
Celery Beat (scheduled stories, daily snapshots)
β External APIs
yfinance + Finnhub (market data)
Groq LLaMA 3.3 70B (AI responses)
| Technology | Purpose |
|---|---|
| Django 5.2 + DRF | REST API framework |
| PostgreSQL | Primary database |
| Redis | Caching + Celery message broker |
| Celery | Background task processing |
| Celery Beat | Scheduled tasks |
| Django Channels | WebSocket support |
| SimpleJWT | JWT authentication |
| yfinance | NSE market data |
| Finnhub API | Backup market data source |
| Groq (LLaMA 3.3 70B) | AI chatbot + ML report generation |
| Gunicorn | Production WSGI server |
| Railway | Backend deployment |
| Technology | Purpose |
|---|---|
| React 18 + TypeScript | UI framework |
| Vite | Build tool |
| Tailwind CSS | Styling |
| Three.js | 3D neural network animations |
| Anime.js | UI micro-animations |
| Recharts | Stock price charts |
| Axios | API communication |
| shadcn/ui | Component library |
| Netlify | Frontend deployment |
- Python 3.11+
- Node.js 18+
- PostgreSQL
- Redis
# Clone the repo
git clone https://github.com/yourusername/stockwise-backend.git
cd stockwise-backend
# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
source venv/bin/activate # Mac/Linux
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
cp .env.example .env
# Fill in your .env with the values below
# Run migrations
python manage.py migrate
# Create superuser (optional)
python manage.py createsuperuserSECRET_KEY=your_django_secret_key
DEBUG=True
DATABASE_URL=postgresql://user:password@localhost:5432/stockwise
REDIS_URL=redis://localhost:6379
GROQ_API_KEY=your_groq_api_key
FINNHUB_API_KEY=your_finnhub_api_key
ALLOWED_HOSTS=localhost,127.0.0.1
CORS_ALLOWED_ORIGINS=http://localhost:5173# Terminal 1 β Redis (start this first)
redis-server
# Terminal 2 β Django
python manage.py runserver
# Terminal 3 β Celery worker
celery -A core worker --pool=solo --loglevel=info
# Terminal 4 β Celery beat
celery -A core beat --loglevel=info# Clone the frontend repo
git clone https://github.com/yourusername/stockwise-frontend.git
cd stockwise-frontend
# Install dependencies
npm install
# Set environment variable
echo "VITE_API_URL=http://127.0.0.1:8000" > .env
# Start dev server
npm run devPOST /api/users/signup/ β Register new user
POST /api/users/login/ β Login, returns JWT tokens
POST /api/users/token/refresh/ β Refresh access token
GET /api/market/price/<ticker>/ β Live stock price
GET /api/market/search/?q=<query> β Search NSE stocks
GET /api/market/top-movers/ β Top gainers and losers
POST /api/trading/buy/ β Market buy order
POST /api/trading/sell/ β Market sell order
POST /api/trading/order/ β Place limit/stop-loss order
GET /api/trading/portfolio/ β Current holdings + summary
GET /api/trading/history/ β Trade history
GET /api/trading/orders/ β Pending orders
DELETE /api/trading/order/<id>/ β Cancel pending order
GET /api/trading/pnl-history/ β P&L over time
GET /api/trading/insights/ β ML trading analysis
POST /api/chatbot/message/ β Send message to FinBot
GET /api/stories/ β All market stories
GET /api/stories/today/ β Today's story
- JWT tokens with short expiry + automatic refresh rotation
select_for_update()database locking prevents race conditions in concurrent trades- Atomic database transactions ensure wallet/portfolio consistency
- Multi-layer AI prompt injection protection in FinBot
- CORS configured to only allow whitelisted frontend origins
- Environment variables for all secrets β nothing hardcoded
Veer β Backend Architecture, Django REST API, Database Design, AI/ML Integration, Deployment
Built at Paradox Hacks β 48 hours, fully deployed, production-ready.
MIT License β feel free to use, modify, and build on this.