Skip to content

moogchi/NeuroTrader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NeuroTrader πŸš€πŸ“ˆ

An AI-powered stock price prediction platform that combines machine learning models with real-time market data to forecast stock movements. Built with Django REST Framework backend and React TypeScript frontend.

License Python Django React TypeScript

πŸ“‹ Table of Contents

✨ Features

  • πŸ“Š Real-time Stock Data: Fetches live stock prices from Alpha Vantage API
  • πŸ€– AI Predictions: XGBoost machine learning model with 50.82% accuracy (trained on 10 tickers)
  • πŸ“° News Sentiment Analysis: 128,000+ headlines analyzed for market sentiment
  • πŸ“ˆ Interactive Charts: Beautiful visualizations with 30-day historical and 7-day forecast data
  • 🎯 Confidence Scores: ML model confidence levels for each prediction (76% recall on "Up" days)
  • πŸ”„ Multi-Ticker Support: Track and predict multiple stocks (AAPL, GOOGL, MSFT, TSLA, NVDA, etc.)
  • ⚑ Fast API: RESTful API built with Django REST Framework
  • 🎨 Modern UI: Responsive design with Tailwind CSS and Radix UI components
  • 🐳 Docker Ready: Fully containerized with Docker Compose
  • πŸ“± Mobile Responsive: Works seamlessly on all device sizes
  • πŸ”’ Secure: Environment-based configuration, CORS protection, secure headers

🎬 Demo

# Start the application
./start.sh

# Open browser to http://localhost:3000

Enter any stock ticker (AAPL, GOOGL, MSFT, etc.) to see real-time predictions!

🎯 Model Performance

NeuroTrader uses a tuned XGBoost classifier trained on 10 diverse stocks with 128,000+ news headlines.

Key Metrics

  • Accuracy: 50.82% (statistically significant edge)
  • Precision (Up): 0.52
  • Recall (Up): 0.76 (successfully identifies 76% of upward movements)
  • Training Data: 10 tickers (AAPL, MSFT, GOOG, NVDA, AMZN, TSLA, META, KO, DIS, MCD)
  • Features: 9 engineered features (RSI, MACD, Moving Averages, Sentiment, etc.)

Why XGBoost?

After rigorous testing of multiple architectures (including LSTM models), XGBoost with engineered features proved superior:

  • βœ… Real predictive edge across multiple stocks
  • βœ… Robust generalization from multi-ticker training
  • βœ… Outperformed deep learning approaches on this problem
  • βœ… 76% recall on "Up" days provides actionable trading signals

πŸ“Š Read Full Model Performance Report β†’

πŸ›  Tech Stack

Backend

  • Django 5.2: Web framework
  • Django REST Framework 3.14: RESTful API development
  • PostgreSQL 15: Primary database
  • Gunicorn 21.2: Production WSGI server
  • WhiteNoise 6.6: Static file serving
  • Alpha Vantage API: Real-time stock market data
  • PyTorch: LSTM neural network models
  • XGBoost: Gradient boosting models
  • Pandas & NumPy: Data processing

Frontend

  • React 18.3: Modern UI library
  • TypeScript 5.x: Type-safe JavaScript
  • Vite 6.3: Lightning-fast build tool and dev server
  • Tailwind CSS 3.4: Utility-first CSS framework
  • Radix UI: Accessible component primitives
  • Recharts: Powerful charting library
  • Lucide React: Beautiful icon set
  • shadcn/ui: Re-usable component collection

Infrastructure

  • Docker & Docker Compose: Container orchestration
  • Nginx: Reverse proxy and load balancer
  • GitHub Actions: CI/CD pipeline (optional)

πŸ— Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    User Browser                          β”‚
β”‚                  http://localhost                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Nginx Reverse Proxy                     β”‚
β”‚                     (Port 80)                            β”‚
β”‚  Routes:                                                 β”‚
β”‚    /          β†’ React Frontend (localhost:3000)          β”‚
β”‚    /api/*     β†’ Django Backend (web:8000)                β”‚
β”‚    /admin/*   β†’ Django Admin (web:8000)                  β”‚
β”‚    /static/*  β†’ Static Files (web:8000)                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚                            β”‚
           β–Ό                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  React Frontend  β”‚         β”‚   Django Backend     β”‚
β”‚  Vite Dev Server β”‚         β”‚   Gunicorn WSGI      β”‚
β”‚   localhost:3000 β”‚         β”‚   Container: web     β”‚
β”‚                  β”‚         β”‚   Port: 8000         β”‚
β”‚  β€’ TypeScript    β”‚         β”‚                      β”‚
β”‚  β€’ Tailwind CSS  β”‚         β”‚  β€’ REST API          β”‚
β”‚  β€’ Recharts      β”‚         β”‚  β€’ ML Models         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚
                                        β–Ό
                             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                             β”‚   PostgreSQL DB      β”‚
                             β”‚   Container: db      β”‚
                             β”‚   Port: 5432         β”‚
                             β”‚                      β”‚
                             β”‚  β€’ Stock data cache  β”‚
                             β”‚  β€’ User sessions     β”‚
                             β”‚  β€’ Admin data        β”‚
                             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚
                                        β–Ό
                             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                             β”‚  Alpha Vantage API   β”‚
                             β”‚  (External Service)  β”‚
                             β”‚                      β”‚
                             β”‚  β€’ Real stock prices β”‚
                             β”‚  β€’ Historical data   β”‚
                             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Prerequisites

Installation Guides

Linux (Ubuntu/Debian)

# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER

# Install Docker Compose
sudo apt-get update
sudo apt-get install docker-compose-plugin

# Install Node.js (via nvm)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install 18

macOS

# Install via Homebrew
brew install --cask docker
brew install node@18

Windows

πŸš€ Quick Start

First Time Setup

Option 1: Automated Setup (Recommended)

# Clone the repository
git clone https://github.com/moogchi/NeuroTrader.git
cd NeuroTrader

# Run the setup script
chmod +x setup.sh
./setup.sh

# Edit .env to add your Alpha Vantage API key
nano .env
# Add: ALPHA_VANTAGE_API_KEY=your_actual_key_here

# Restart backend to load API key
sudo docker compose restart web

# Start the frontend
cd frontend
npm run dev

Open your browser to http://localhost:3000 πŸŽ‰

Option 2: Manual Setup

# Clone the repository
git clone https://github.com/moogchi/NeuroTrader.git
cd NeuroTrader

# 1. Create environment file
cp .env.example .env

# 2. Edit .env and add your API key
nano .env
# Add: ALPHA_VANTAGE_API_KEY=your_actual_key_here

# 3. Make scripts executable
chmod +x init-db.sh setup.sh

# 4. Start backend services (PostgreSQL + Django + Nginx)
sudo docker compose down -v  # Clean start
sudo docker compose up --build -d

# 5. Wait for services to be ready
sleep 10
sudo docker compose ps

# 6. Install frontend dependencies
cd frontend
npm install

# 7. Start frontend development server
npm run dev

Subsequent Starts (After First Setup)

# Start backend
sudo docker compose up -d

# Start frontend (in another terminal)
cd frontend && npm run dev

Option 3: One-Command Start (Legacy)

# After initial setup, use the start script
./start.sh

Open your browser to http://localhost:3000 πŸŽ‰

Option 4: Manual Docker Setup

# Start backend services (PostgreSQL + Django + Nginx)
sudo docker compose up -d

# In another terminal, start frontend
cd frontend
npm install
npm run dev

Access:

πŸ’» Development Setup

Backend Development

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your configuration

# Run database migrations
python manage.py migrate

# Create superuser
python manage.py createsuperuser

# Collect static files
python manage.py collectstatic --noinput

# Run development server
python manage.py runserver

Frontend Development

cd frontend

# Install dependencies
npm install

# Start dev server with hot reload
npm run dev

# Build for production
npm run build

# Preview production build
npm run preview

Database Access

# Access PostgreSQL container
sudo docker compose exec db psql -U neurotrader_user -d neurotrader_db

# Common commands
\dt          # List tables
\d+ tablename # Describe table
\q           # Quit

πŸ“‘ API Documentation

Base URL

http://localhost/api/

Endpoints

1. Health Check

GET /api/health/

Response:

{
  "status": "healthy",
  "timestamp": "2025-11-02T12:00:00.000Z",
  "service": "NeuroTrader API"
}

2. Get Stock Prediction

GET /api/prediction/?ticker=AAPL

Parameters:

  • ticker (string, required): Stock ticker symbol (e.g., AAPL, GOOGL, MSFT)

Response:

{
  "ticker": "AAPL",
  "current_price": 228.87,
  "predicted_price": 235.20,
  "prediction_change": 2.77,
  "confidence": 87.5,
  "historical_data": [
    {
      "date": "2025-10-01",
      "price": 225.30
    },
    ...
  ],
  "forecast_data": [
    {
      "date": "2025-11-03",
      "price": 232.10,
      "confidence": 0.87
    },
    ...
  ],
  "last_updated": "2025-11-02T12:00:00.000Z"
}

Supported Tickers:

  • AAPL (Apple)
  • GOOGL (Google)
  • MSFT (Microsoft)
  • AMZN (Amazon)
  • TSLA (Tesla)
  • NVDA (NVIDIA)
  • META (Meta)
  • And many more...

API Rate Limits

Alpha Vantage free tier: 25 requests/day, 5 requests/minute

If the API limit is reached, the backend falls back to realistic mock data.

πŸ”§ Environment Variables

Backend (.env)

# Django Settings
SECRET_KEY=your-secret-key-here-change-in-production
DEBUG=True  # Set to False in production
ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0

# Database
DB_NAME=neurotrader_db
DB_USER=neurotrader_user
DB_PASSWORD=strongpassword123
DB_HOST=db  # Use 'localhost' if not using Docker
DB_PORT=5432

# Alpha Vantage API
ALPHA_VANTAGE_API_KEY=YOUR_API_KEY_HERE

# CORS (comma-separated)
CORS_ALLOWED_ORIGINS=http://localhost,http://localhost:3000,http://127.0.0.1:3000

Frontend (.env.local - optional)

# API Base URL (defaults to http://localhost/api)
VITE_API_URL=http://localhost/api

πŸ“ Project Structure

NeuroTrader/
β”œβ”€β”€ frontend/                    # React TypeScript frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/                # API client and types
β”‚   β”‚   β”‚   └── client.ts       # API integration
β”‚   β”‚   β”œβ”€β”€ components/         # React components
β”‚   β”‚   β”‚   β”œβ”€β”€ Header.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ PredictionCard.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ PriceChart.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ ForecastTable.tsx
β”‚   β”‚   β”‚   └── ui/             # Reusable UI components
β”‚   β”‚   β”œβ”€β”€ hooks/              # Custom React hooks
β”‚   β”‚   β”‚   └── usePrediction.ts
β”‚   β”‚   β”œβ”€β”€ App.tsx             # Main app component
β”‚   β”‚   └── main.tsx            # Entry point
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ vite.config.ts
β”‚   β”œβ”€β”€ tailwind.config.js
β”‚   └── nginx-host.conf         # Nginx proxy config
β”‚
β”œβ”€β”€ neurotrader_project/        # Django project settings
β”‚   β”œβ”€β”€ settings.py             # Django configuration
β”‚   β”œβ”€β”€ urls.py                 # URL routing
β”‚   β”œβ”€β”€ wsgi.py                 # WSGI entry point
β”‚   └── asgi.py                 # ASGI entry point
β”‚
β”œβ”€β”€ predictor/                   # Django app for predictions
β”‚   β”œβ”€β”€ views.py                # API endpoints
β”‚   β”œβ”€β”€ models.py               # Database models
β”‚   β”œβ”€β”€ admin.py                # Django admin config
β”‚   └── migrations/             # Database migrations
β”‚
β”œβ”€β”€ src/                         # ML models and data fetching
β”‚   β”œβ”€β”€ data_c/                 # Data collection scripts
β”‚   β”‚   β”œβ”€β”€ get_price_history.py
β”‚   β”‚   β”œβ”€β”€ get_news_data.py
β”‚   β”‚   └── get_news_data_all.py
β”‚   └── backtest/               # Backtesting utilities
β”‚       └── backtest.py
β”‚
β”œβ”€β”€ notebooks/                   # Jupyter notebooks
β”‚   β”œβ”€β”€ 01-initial-exploration.ipynb
β”‚   └── 02-multi-ticker-model.ipynb  # LSTM model training
β”‚
β”œβ”€β”€ docker-compose.yml           # Docker orchestration
β”œβ”€β”€ Dockerfile.backend           # Backend Docker image
β”œβ”€β”€ entrypoint.sh                # Container startup script
β”œβ”€β”€ requirements.txt             # Python dependencies
β”œβ”€β”€ manage.py                    # Django CLI
β”œβ”€β”€ .env                         # Environment variables
β”œβ”€β”€ .dockerignore               # Docker ignore rules
β”œβ”€β”€ start.sh                     # Quick start script
└── README.md                    # This file

🚒 Deployment

See DEPLOYMENT.md for detailed deployment instructions including:

  • Production configuration
  • Nginx setup
  • SSL/HTTPS configuration
  • Cloud deployment (AWS, Azure, GCP)
  • Docker Swarm / Kubernetes
  • CI/CD pipelines
  • Monitoring and logging

Quick Production Checklist

# 1. Update environment variables
DEBUG=False
SECRET_KEY=<generate-secure-key>
ALLOWED_HOSTS=yourdomain.com

# 2. Use production database
# Update DB_HOST, DB_PASSWORD with production values

# 3. Set up SSL/HTTPS
# Configure Nginx with Let's Encrypt certificates

# 4. Build production images
docker compose -f docker-compose.prod.yml build

# 5. Deploy
docker compose -f docker-compose.prod.yml up -d

# 6. Run migrations
docker compose exec web python manage.py migrate

# 7. Collect static files
docker compose exec web python manage.py collectstatic --noinput

πŸ› Troubleshooting

Database Issues

Error: role "neurotrader_user" does not exist or database "neurotrader_db" does not exist

This happens on fresh clones when the database user wasn't created. Fix it with a clean rebuild:

# Stop all containers and remove volumes
sudo docker compose down -v

# Ensure init script is executable
chmod +x init-db.sh

# Rebuild with fresh database (this will auto-create the user)
sudo docker compose up --build -d

# Verify database is healthy
sudo docker compose ps

The init-db.sh script will automatically create the database user on first startup.

Database connection errors:

# Wait for database to be ready
sudo docker compose exec web python manage.py migrate

# Check database is healthy
sudo docker compose ps

# If still failing, check logs
sudo docker compose logs db

Docker Issues

Container won't start:

# Check logs
sudo docker compose logs web
sudo docker compose logs db

# Restart services
sudo docker compose restart

# Clean rebuild
sudo docker compose down -v
sudo docker compose up --build

Frontend Issues

Blank page or API errors:

# Check if backend is running
curl http://localhost/api/health/

# Restart frontend
cd frontend
npm run dev

Build errors:

# Clear node_modules and reinstall
rm -rf node_modules package-lock.json
npm install

API Issues

"API key required" errors:

  1. Get free API key: https://www.alphavantage.co/support/#api-key
  2. Add to .env: ALPHA_VANTAGE_API_KEY=your_key_here
  3. Restart backend: sudo docker compose restart web

Rate limit exceeded:

  • Alpha Vantage free tier: 25 requests/day
  • App falls back to mock data automatically
  • Consider upgrading to paid tier for production

πŸ§ͺ Testing

# Backend tests
python manage.py test

# Frontend tests
cd frontend
npm test

# E2E tests
npm run test:e2e

🀝 Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow PEP 8 for Python code
  • Use ESLint/Prettier for TypeScript/React
  • Write tests for new features
  • Update documentation
  • Keep commits atomic and well-described

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“ž Support


Built with ❀️ by the NeuroTrader Team

⭐ Star this repo if you find it useful!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors