Skip to content

HunarBhatia/Paradox_Hacks_Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

StockWise πŸ“ˆ

A production-grade AI-powered paper trading platform that helps students learn investing using virtual money with real NSE market data.

Django React TypeScript PostgreSQL Redis Groq


🎯 What is StockWise?

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.


✨ Features

πŸ” Authentication & Wallets

  • 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

πŸ“Š Real-Time Market Data

  • 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

⚑ Trading Engine

  • 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

πŸ’Ό Portfolio Analytics

  • 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

πŸ€– FinBot β€” AI Trading Assistant

  • 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

🧠 Trading Insights

  • 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

πŸ“° Daily Market Stories

  • 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

πŸ—οΈ Architecture

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

Data Flow

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)

πŸ› οΈ Tech Stack

Backend

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

Frontend

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

πŸ”§ Running Locally

Prerequisites

  • Python 3.11+
  • Node.js 18+
  • PostgreSQL
  • Redis

Backend Setup

# 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 createsuperuser

Environment Variables

SECRET_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

Start the Backend (4 terminals)

# 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

Frontend Setup

# 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 dev

πŸ“‘ API Endpoints

Authentication

POST /api/users/signup/         β€” Register new user
POST /api/users/login/          β€” Login, returns JWT tokens
POST /api/users/token/refresh/  β€” Refresh access token

Market Data

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

Trading

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

Chatbot

POST /api/chatbot/message/        β€” Send message to FinBot

Stories

GET /api/stories/                 β€” All market stories
GET /api/stories/today/           β€” Today's story

πŸ”’ Security Highlights

  • 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

πŸ‘¨β€πŸ’» Built By

Veer β€” Backend Architecture, Django REST API, Database Design, AI/ML Integration, Deployment

Built at Paradox Hacks β€” 48 hours, fully deployed, production-ready.


πŸ“„ License

MIT License β€” feel free to use, modify, and build on this.

Releases

Packages

Contributors

Languages