Bhaskar-Guthula-137/AIStockMarketApp-NextJS15-MongoDB-Cloudflare-BetterAuth-Inngest

A modern, full-stack stock trading application built with Next.js 15, featuring real-time market data, personalized watchlists, AI-powered email automation, and comprehensive stock analytics. Deployed on Cloudflare Workers for global edge performance.

β˜… 2Forks 0TypeScriptGitHub β†—Compare

Project website β†—

better-authcloudflare-d1cloudflare-workersdrizzle-ormfinnhubgemini-apiinngestlucide-reactnextjsnodemailerreacjsreact-hook-formshadcn-uitradingviewtypescript

README

AI Trading App - Real-Time Stock Market Dashboard

A modern, full-stack stock trading application built with Next.js 15, featuring real-time market data, personalized watchlists, AI-powered email automation, and comprehensive stock analytics. Deployed on Cloudflare Workers for global edge performance.

πŸš€ Live Demo

Deployed URL: https://ai-trading-app.bhaskarg.workers.dev/

πŸ“Έ App Screenshots

Homepage Dashboard

Homepage Dashboard

The homepage features:

  • Market Overview with interactive charts and real-time stock data
  • Stock Heatmap showing market performance by industry
  • Top Stories with latest financial news
  • Market Data Widget displaying key financial metrics

Stock Detail Page

Stock Detail Page

The stock detail page provides:

  • Real-time Price Data with candlestick charts
  • Technical Analysis with AI-powered insights
  • Company Profile and financial metrics
  • Watchlist Management with one-click add/remove
  • Comprehensive Financials including P/E ratios, market cap, and more

✨ Key Features

πŸ” Authentication & User Management

  • Secure authentication with Better Auth
  • Email/password sign-up and sign-in
  • User profile management with investment preferences
  • Session management with Cloudflare D1

πŸ“Š Real-Time Market Data

  • Live stock prices and market data via Finnhub API
  • Interactive TradingView widgets for charts and analysis
  • Market overview with heatmaps and sector performance
  • Real-time price updates and percentage changes

⭐ Watchlist Management

  • Create and manage personalized stock watchlists
  • Add/remove stocks with one-click functionality
  • Real-time price tracking for watchlisted stocks
  • Market cap and performance metrics

πŸ” Stock Search & Discovery

  • Advanced stock search with autocomplete
  • Quick access to stock details
  • Filter by industry and sector
  • Search command palette for power users

πŸ“§ AI-Powered Email Automation

  • Welcome Emails: Personalized welcome messages using Gemini AI based on user investment profile
  • Daily News Summaries: Automated daily email digests with AI-summarized market news
  • Personalized content based on user watchlist and preferences
  • Scheduled cron jobs for automated delivery

🎨 Modern UI/UX

  • Dark theme optimized for financial data
  • Responsive design for all devices
  • Shadcn UI components for consistent design
  • Toast notifications for user feedback
  • Smooth animations and transitions

πŸ› οΈ Tech Stack

Frontend

  • Next.js 15 - React framework with App Router
  • React 19 - UI library
  • TypeScript - Type-safe development
  • Tailwind CSS 4 - Utility-first styling
  • Shadcn UI - Component library
  • TradingView Widgets - Financial charts and data visualization
  • React Hook Form - Form management
  • Lucide React - Icon library

Backend & Infrastructure

  • Cloudflare Workers - Edge computing platform
  • Cloudflare D1 - SQLite database at the edge
  • Better Auth - Authentication framework
  • Drizzle ORM - Type-safe database queries
  • OpenNext.js - Next.js adapter for Cloudflare

APIs & Services

  • Finnhub API - Real-time stock market data
  • Inngest - Background job processing and automation
  • Google Gemini AI - AI-powered content generation
  • Nodemailer - Email delivery service
  • TradingView - Financial widgets and charts

Development Tools

  • Bun - Fast JavaScript runtime and package manager
  • Wrangler - Cloudflare Workers CLI
  • TypeScript - Static type checking
  • ESLint - Code linting

πŸ—οΈ Architecture & Automation Flow

Application Flow Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        USER JOURNEY                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1. SIGN UP / SIGN IN
   β”‚
   β”œβ”€β–Ί Better Auth (Cloudflare D1)
   β”‚   └─► User Session Management
   β”‚
   └─► Inngest Event: app/user.created
       β”‚
       └─► sendSignUpEmail Function
           β”‚
           β”œβ”€β–Ί Step 1: Get User Profile
           β”‚   └─► Country, Investment Goals, Risk Tolerance
           β”‚
           β”œβ”€β–Ί Step 2: AI Content Generation
           β”‚   └─► Gemini AI generates personalized welcome message
           β”‚
           └─► Step 3: Send Welcome Email
               └─► Nodemailer delivers personalized email

2. DASHBOARD INTERACTION
   β”‚
   β”œβ”€β–Ί Market Overview Widget
   β”‚   └─► TradingView Widget (Real-time data)
   β”‚
   β”œβ”€β–Ί Stock Heatmap
   β”‚   └─► TradingView Widget (Sector performance)
   β”‚
   β”œβ”€β–Ί Top Stories
   β”‚   └─► Finnhub API (Market news)
   β”‚
   └─► Market Data Table
       └─► Finnhub API (Stock quotes)

3. STOCK SEARCH & DETAILS
   β”‚
   β”œβ”€β–Ί Search Command
   β”‚   └─► Finnhub API (Symbol search)
   β”‚
   └─► Stock Detail Page
       β”‚
       β”œβ”€β–Ί Symbol Info Widget
       β”œβ”€β–Ί Candlestick Chart
       β”œβ”€β–Ί Technical Analysis
       β”œβ”€β–Ί Company Profile
       └─► Financials Data
           └─► All via TradingView & Finnhub APIs

4. WATCHLIST MANAGEMENT
   β”‚
   β”œβ”€β–Ί Add to Watchlist
   β”‚   └─► Cloudflare D1 (Store watchlist item)
   β”‚
   └─► Remove from Watchlist
       └─► Cloudflare D1 (Delete watchlist item)

5. AUTOMATED DAILY NEWS (Cron: 12:00 PM Daily)
   β”‚
   └─► sendDailyNewsSummary Function
       β”‚
       β”œβ”€β–Ί Step 1: Get All Users
       β”‚   └─► Query Cloudflare D1
       β”‚
       β”œβ”€β–Ί Step 2: Fetch User Watchlists
       β”‚   └─► Get symbols for each user
       β”‚
       β”œβ”€β–Ί Step 3: Fetch News Articles
       β”‚   β”œβ”€β–Ί For each user:
       β”‚   β”‚   β”œβ”€β–Ί Get news for watchlist symbols
       β”‚   β”‚   └─► Fallback to general market news
       β”‚   └─► Limit to 6 articles per user
       β”‚
       β”œβ”€β–Ί Step 4: AI News Summarization
       β”‚   └─► Gemini AI summarizes articles per user
       β”‚
       └─► Step 5: Send Email Digests
           └─► Nodemailer sends personalized summaries

Automation & Background Jobs

1. Welcome Email Automation

User Sign Up
    β”‚
    β–Ό
Inngest Event Triggered
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  sendSignUpEmail Function           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β€’ Extract user profile data        β”‚
β”‚  β€’ Generate AI prompt               β”‚
β”‚  β€’ Call Gemini AI API               β”‚
β”‚  β€’ Generate personalized intro      β”‚
β”‚  β€’ Send welcome email via Nodemailerβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2. Daily News Summary Automation

Cron Schedule: 0 12 * * * (12:00 PM Daily)
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  sendDailyNewsSummary Function               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Step 1: Get All Users                       β”‚
β”‚    └─► Query D1 database                    β”‚
β”‚                                            β”‚
β”‚  Step 2: Fetch User-Specific News           β”‚
β”‚    β”œβ”€β–Ί For each user:                       β”‚
β”‚    β”‚   β”œβ”€β–Ί Get watchlist symbols            β”‚
β”‚    β”‚   β”œβ”€β–Ί Fetch news for symbols           β”‚
β”‚    β”‚   └─► Fallback to general news          β”‚
β”‚    └─► Limit to 6 articles                  β”‚
β”‚                                            β”‚
β”‚  Step 3: AI Summarization                   β”‚
β”‚    β”œβ”€β–Ί For each user's articles:            β”‚
β”‚    β”‚   β”œβ”€β–Ί Generate summary prompt          β”‚
β”‚    β”‚   β”œβ”€β–Ί Call Gemini AI API                β”‚
β”‚    β”‚   └─► Generate news summary              β”‚
β”‚    └─► Store summaries                      β”‚
β”‚                                            β”‚
β”‚  Step 4: Send Email Digests                 β”‚
β”‚    └─► Send personalized emails via         β”‚
β”‚        Nodemailer                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Flow Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Browser    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Cloudflare Workers (Edge)    β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚   β”‚   Next.js App Router     β”‚ β”‚
β”‚   β”‚   β€’ Server Components    β”‚ β”‚
β”‚   β”‚   β€’ Server Actions       β”‚ β”‚
β”‚   β”‚   β€’ API Routes           β”‚ β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚                                 β”‚
       β–Ό                                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Cloudflare D1β”‚              β”‚  External APIs   β”‚
β”‚ (SQLite DB)  β”‚              β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              β”‚              β”‚ β€’ Finnhub API    β”‚
β”‚ β€’ Users      β”‚              β”‚ β€’ TradingView    β”‚
β”‚ β€’ Sessions   β”‚              β”‚ β€’ Gemini AI      β”‚
β”‚ β€’ Watchlist  β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Inngest        β”‚
β”‚   (Background    β”‚
β”‚    Jobs)         β”‚
β”‚                  β”‚
β”‚ β€’ Email Jobs     β”‚
β”‚ β€’ Cron Jobs      β”‚
β”‚ β€’ AI Processing  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Installation & Setup

Prerequisites

  • Node.js 20+ or Bun
  • Cloudflare account
  • Finnhub API key
  • Google Gemini API key
  • Inngest account

Environment Variables

Create a .dev.vars file:

BETTER_AUTH_SECRET=your-secret-key
BETTER_AUTH_URL=http://localhost:3000
FINNHUB_API_KEY=your-finnhub-key
GEMINI_API_KEY=your-gemini-key
INNGEST_EVENT_KEY=your-inngest-key
INNGEST_SIGNING_KEY=your-signing-key
NODEMAILER_EMAIL=your-email
NODEMAILER_PASSWORD=your-app-password

Local Development

# Install dependencies
bun install

# Set up Cloudflare D1 database
npx wrangler d1 create ticker_d1

# Run migrations
npx wrangler d1 execute ticker_d1 --local --file=./migrations/0001_better-auth-tables.sql
npx wrangler d1 execute ticker_d1 --local --file=./migrations/0002_watchlist-table.sql

# Start development server
bun run dev

# In another terminal, start Inngest dev server
npx inngest-cli dev

Deployment

# Build and deploy to Cloudflare
bun run deploy

πŸ—„οΈ Database Schema

Users Table

  • User authentication and profile data
  • Investment preferences (goals, risk tolerance, industry)

Sessions Table

  • User session management
  • Token-based authentication

Watchlist Table

  • User watchlist items
  • Stock symbols and company names
  • Timestamps for tracking

πŸ”„ API Integration

Finnhub API

  • Real-time stock quotes
  • Company profiles
  • Market news
  • Symbol search

TradingView Widgets

  • Interactive charts
  • Technical analysis
  • Market overview
  • Stock heatmaps

Google Gemini AI

  • Personalized email content generation
  • News summarization
  • User preference analysis

🚦 Project Structure

ai-trading-app/
β”œβ”€β”€ app/                    # Next.js App Router
β”‚   β”œβ”€β”€ (auth)/            # Authentication routes
β”‚   β”œβ”€β”€ (root)/            # Protected routes
β”‚   └── api/               # API routes
β”œβ”€β”€ components/            # React components
β”‚   β”œβ”€β”€ ui/               # Shadcn UI components
β”‚   └── forms/            # Form components
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ actions/          # Server actions
β”‚   β”œβ”€β”€ better-auth/      # Auth configuration
β”‚   β”œβ”€β”€ inngest/          # Background jobs
β”‚   └── nodemailer/       # Email service
β”œβ”€β”€ hooks/                # React hooks
β”œβ”€β”€ schemas/              # Drizzle schemas
β”œβ”€β”€ migrations/           # Database migrations
└── public/              # Static assets

🎯 Key Achievements

  • βœ… Edge Deployment: Full-stack app deployed on Cloudflare Workers for global edge performance
  • βœ… Real-Time Data: Live stock prices and market data integration
  • βœ… AI Automation: Automated email system with AI-powered personalization
  • βœ… Type Safety: End-to-end TypeScript with Drizzle ORM
  • βœ… Modern Auth: Secure authentication with Better Auth
  • βœ… Responsive Design: Mobile-first responsive UI
  • βœ… Performance: Optimized with Next.js 15 and edge computing

πŸ“ License

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

πŸ‘€ Author

Bhaskar Guthula

πŸ™ Acknowledgments

  • TradingView for financial widgets
  • Finnhub for market data API
  • Cloudflare for edge infrastructure
  • Inngest for background job processing
  • Shadcn UI for component library

Built with ❀️ using Next.js, Cloudflare Workers, and modern web technologies

Contributors

Bhaskar-Guthula-137bhaskar-aadhan

Issues