Stock Dip Recovery Analyzer - ML-Powered Trading Analysis Platform
Comprehensive full-stack application for analyzing historical stock performance to understand dip recovery patterns using machine learning, real-time market data, advanced technical analysis, and predictive modeling with 95%+ data coverage and 300+ epoch training.
Status: completed · 2025-10-05
Overview
The Stock Dip Recovery Analyzer is a comprehensive full-stack application that analyzes historical stock performance to understand dip recovery patterns using machine learning and real-time market data. Features advanced dip detection (0.5% to 30%), recovery analysis, pattern recognition, risk assessment, and real-time monitoring with WebSocket streaming, achieving 95%+ data coverage and production-ready ML models.
Technologies
React 19, TypeScript, Node.js, Express.js, Prisma ORM, SQLite, TensorFlow.js, WebSocket, Alpaca Markets API, Recharts, TailwindCSS, Vite, Socket.io, Winston, Redis, Real-time Streaming, Machine Learning, Technical Indicators, Data Visualization, API Rate Limiting, Caching, Error Handling, Responsive Design, Dark/Light Theme, Keyboard Shortcuts, Accessibility, Performance Optimization
- Dips Detected
- 377+ AAPL, 214+ TSLA
- Data Coverage
- 95%+
- ML Training Epochs
- 300+
- Cache Hit Rate
- 95%+
Stock Dip Recovery Analyzer
A comprehensive full-stack application for analyzing historical stock performance to understand dip recovery patterns using machine learning and real-time market data. The application features advanced technical analysis, predictive modeling, and comprehensive data persistence with intelligent caching.
🎯 Project Status
✅ Backend Migration Complete: Successfully migrated from browser-based to server-based analysis with full data persistence, intelligent caching, and real-time capabilities. The application now provides enterprise-grade performance with comprehensive analysis features.
📊 Key Achievements:
- 377 dips detected for AAPL and 214 dips for TSLA in comprehensive testing
- 95%+ data coverage with smart caching and incremental updates
- Multi-timeframe support: 1-minute to 1-day intervals
- Real-time analysis with WebSocket streaming
- Production-ready ML models with 300+ epoch training
- Comprehensive API: RESTful endpoints with Swagger documentation
🚀 What It Does
This application analyzes stock price dips and recovery patterns using historical market data to help traders and investors understand:
- Dip Detection: Identifies price drops of various percentages (0.5% to 30%)
- Recovery Analysis: Tracks how long it takes for stocks to recover from dips
- Pattern Recognition: Uses machine learning to predict recovery times
- Risk Assessment: Provides statistical analysis of recovery patterns
- Real-time Monitoring: Live tracking of stock movements and dip events
- Performance Optimization: Intelligent data management and caching
🎯 Why It's Useful
- Data-Driven Decisions: Make informed trading decisions based on historical patterns
- Risk Management: Understand typical recovery times for different dip magnitudes
- Market Timing: Identify optimal entry/exit points based on recovery patterns
- Performance Insights: Compare recovery performance across different stocks and timeframes
- Real-time Alerts: Get notified of significant price movements and dip events
- Educational Tool: Learn about market behavior through comprehensive analysis
Features
Core Analysis Features
- Real Market Data: Fetches actual historical stock data from Alpaca Markets
- Advanced Dip Detection: Identifies stock price dips of various percentages (0.5% to 30%)
- Comprehensive Recovery Analysis: Tracks recovery times with statistical analysis
- Multi-timeframe Support: Analysis across 1-minute to 1-day intervals
- Interactive Charts: Visual representation with Recharts and real-time updates
- Popular Symbols: Quick access to commonly traded stocks (AAPL, MSFT, GOOGL, etc.)
Machine Learning & AI
- TensorFlow.js Models: Advanced predictive analytics for recovery patterns
- Custom Training: Train models with configurable parameters (epochs, learning rate, architecture)
- Model Management: Save, load, and compare different ML models
- Production-Ready Training: 300+ epoch training with realistic convergence
- Feature Engineering: Technical indicators (RSI, MACD, Bollinger Bands, Volume)
- Real-time Predictions: Live prediction updates with confidence intervals
Data Management & Performance
- Smart Caching: 95%+ data coverage with intelligent cache management
- Data Persistence: SQLite database with Prisma ORM for reliable storage
- Incremental Updates: Only fetch missing data ranges to optimize API usage
- Rate Limiting: Built-in API rate limiting and request queuing
- Memory Optimization: Efficient data handling for large datasets
- Data Compression: Optimized storage for historical data
Real-time Capabilities
- WebSocket Streaming: Live data updates and real-time analysis
- Push Notifications: Alerts for significant price movements and dip events
- Live Monitoring: Real-time tracking of multiple stocks simultaneously
- Event Streaming: Instant updates for dip detection and recovery events
Advanced Analytics
- Technical Indicators: RSI, MACD, Bollinger Bands, Volume analysis
- Statistical Analysis: Comprehensive recovery statistics and pattern analysis
- Risk Assessment: Advanced risk metrics and probability analysis
- Performance Benchmarking: Compare recovery patterns across different stocks
- Data Visualization: Interactive charts with multiple timeframes and indicators
User Experience
- Responsive Design: Fully responsive mobile and desktop interface
- Dark/Light Theme: Theme switching with system preference detection
- Keyboard Shortcuts: Power user shortcuts for efficient navigation
- Error Handling: Comprehensive error boundaries and user feedback
- Loading States: Smooth loading indicators and progress tracking
- Accessibility: WCAG compliant interface with keyboard navigation
Prerequisites
Before setting up the application, ensure you have the following installed:
- Node.js (version 18.0 or higher recommended)
- npm (version 8.0 or higher)
- Git (for cloning the repository)
Required API Credentials
- Alpaca Markets API: Free account required for real-time stock data
- Sign up at Alpaca Markets
- Navigate to API Dashboard to get your API credentials
- Both paper trading and live trading accounts work (paper trading recommended for development)
Installation & Setup
1. Clone the Repository
```bash git clone https://github.com/your-username/stock-dip-recovery-analyzer.git cd stock-dip-recovery-analyzer ```
2. Install Dependencies
Install frontend dependencies: ```bash npm install ```
Install backend dependencies: ```bash cd backend npm install cd .. ```
3. Environment Configuration
Frontend Environment
Create a `.env.local` file in the root directory:
```env
Alpaca API Configuration
VITE_ALPACA_API_KEY_ID=your_alpaca_api_key_id VITE_ALPACA_API_SECRET_KEY=your_alpaca_api_secret_key
Backend API Configuration
VITE_BACKEND_URL=http://localhost:3486 VITE_WEBSOCKET_URL=ws://localhost:3486
Development Configuration
VITE_NODE_ENV=development VITE_DEBUG_MODE=true ```
Backend Environment
Create a `.env` file in the `backend` directory:
```env
Database Configuration
DATABASE_URL="file:./dev.db"
API Configuration
PORT=3486 NODE_ENV=development
CORS Configuration
FRONTEND_URL=http://localhost:5173
Alpaca API Configuration (Backend)
ALPACA_API_KEY_ID=your_alpaca_api_key_id ALPACA_API_SECRET_KEY=your_alpaca_api_secret_key ALPACA_BASE_URL=https://data.alpaca.markets
ML Model Configuration
ML_MODEL_PATH=./models TRAINING_DATA_RETENTION_DAYS=30
Logging Configuration
LOG_LEVEL=info LOG_FILE_PATH=./logs ```
4. Database Setup
The application uses Prisma with SQLite for development:
```bash cd backend
Generate Prisma client
npx prisma generate
Run database migrations
npx prisma migrate dev
Optional: Seed the database with sample data
npm run seed
cd .. ```
5. Start the Application
Option A: Full Stack Development
Start both frontend and backend simultaneously:
```bash npm run dev:full ```
Option B: Separate Terminal Windows
Terminal 1 - Backend Server: ```bash cd backend npm run dev ```
Terminal 2 - Frontend Development Server: ```bash npm run dev ```
6. Access the Application
- Frontend: http://localhost:5173
- Backend API: http://localhost:3486
- API Documentation: http://localhost:3486/docs (if Swagger is enabled)
Development Workflow
Project Structure
``` stock-dip-recovery-analyzer/ ├── src/ # Frontend source code │ ├── components/ # React components │ ├── services/ # API and business logic services │ ├── types.ts # TypeScript type definitions │ └── constants.ts # Application constants ├── backend/ # Backend source code │ ├── src/ # Backend TypeScript source │ ├── prisma/ # Database schema and migrations │ └── tests/ # Backend tests ├── docs/ # Documentation ├── public/ # Static assets └── tests/ # Frontend tests ```
Available Scripts
Frontend Scripts
```bash npm run dev # Start development server npm run build # Build for production npm run preview # Preview production build npm run test # Run tests npm run test:ui # Run tests with UI npm run test:coverage # Run tests with coverage report npm run lint # Run ESLint npm run type-check # Run TypeScript compiler check ```
Backend Scripts
```bash cd backend npm run dev # Start development server with hot reload npm run build # Build TypeScript to JavaScript npm run start # Start production server npm run test # Run backend tests npm run test:watch # Run tests in watch mode npm run db:migrate # Run Prisma migrations npm run db:studio # Open Prisma Studio npm run seed # Seed database with sample data ```
Usage Guide
Getting Started with Stock Analysis
Basic Stock Analysis Workflow
- Select Stock Symbol: Enter any valid stock symbol (AAPL, MSFT, GOOGL, TSLA, etc.)
- Configure Analysis Period: Choose from 1 month to 2 years of historical data
- Set Dip Percentages: Configure which dip percentages to analyze (0.5% to 30%)
- Choose Timeframe: Select analysis granularity (1 minute to 1 day intervals)
- Run Analysis: Click "Analyze Stock Performance" to start comprehensive analysis
- Review Results: Examine interactive charts, statistical analysis, and recovery patterns
Advanced Analysis Features
Machine Learning Predictions
- Enable ML Mode: Toggle ML predictions in the settings panel
- Custom Model Training:
- Select training parameters (epochs: 50-1000, learning rate: 0.0001-0.01)
- Choose network architecture (32-256 neurons per layer)
- Set batch size for optimal training performance
- Model Management:
- Save trained models for later use
- Compare model performance across different stocks
- Load pre-trained models for instant predictions
- Real-time Predictions: View live predictions with confidence intervals
Multi-Timeframe Analysis
- Timeframe Selection: Analyze across different intervals simultaneously
- 1-minute: High-frequency trading patterns
- 5-minute: Short-term momentum analysis
- 15-minute: Intraday trend analysis
- 1-hour: Daily trading patterns
- 1-day: Long-term trend analysis
- Cross-timeframe Comparison: Compare dip patterns across different timeframes
- Adaptive Analysis: Automatically adjust analysis parameters based on timeframe
Technical Indicators Integration
- RSI (Relative Strength Index): Momentum oscillator for overbought/oversold conditions
- MACD (Moving Average Convergence Divergence): Trend-following momentum indicator
- Bollinger Bands: Volatility-based support/resistance levels
- Volume Analysis: Trading volume correlation with price movements
- Custom Indicator Parameters: Adjust indicator settings for different analysis needs
Real-time Monitoring & Alerts
Live Data Streaming
- WebSocket Connections: Real-time price updates and analysis
- Live Charts: Auto-updating charts with streaming data
- Multi-symbol Monitoring: Track multiple stocks simultaneously
- Real-time Dip Detection: Instant alerts when dips occur
Alert System
- Dip Alerts: Get notified when stocks experience specified dip percentages
- Recovery Alerts: Notifications when stocks recover from dips
- Price Alerts: Set custom price thresholds for notifications
- Volume Alerts: Unusual volume activity notifications
- Custom Alert Rules: Create complex alert conditions
Data Management
Smart Data Handling
- Intelligent Caching: 95%+ cache hit rates for improved performance
- Incremental Updates: Only fetch missing data to optimize API usage
- Data Compression: Efficient storage of large historical datasets
- Gap Analysis: Identify and fill data gaps automatically
Export & Import
- Analysis Export: Export analysis results to CSV/JSON formats
- Chart Export: Save charts as images for reports
- Model Export: Share trained ML models with other users
- Data Backup: Comprehensive data backup and restore functionality
Performance Optimization
Memory Management
- Efficient Data Structures: Optimized memory usage for large datasets
- Garbage Collection: Automatic cleanup of unused data
- Virtual Scrolling: Handle thousands of data points smoothly
- Progressive Loading: Load data on-demand for better performance
API Rate Limiting
- Request Queuing: Intelligent request queuing to avoid API limits
- Exponential Backoff: Smart retry logic for failed requests
- Usage Monitoring: Track and optimize API usage patterns
- Cost Optimization: Minimize API costs while maximizing data coverage
API Integration
Alpaca Market Data API
The application integrates with Alpaca's market data API for real-time and historical stock data:
Base URL: `https://data.alpaca.markets/v2\`
Key Endpoints Used:
- `/stocks/bars` - Historical OHLCV data
- `/stocks/bars/latest` - Latest price data
- `/stocks/quotes/latest` - Latest bid/ask quotes
- `/stocks/trades/latest` - Latest trade information
Authentication:
```bash curl --request GET \ --url 'https://data.alpaca.markets/v2/stocks/bars/latest?symbols=AAPL&feed=sip' \ --header 'APCA-API-KEY-ID: YOUR_KEY_ID' \ --header 'APCA-API-SECRET-KEY: YOUR_SECRET_KEY' \ --header 'accept: application/json' ```
Backend API Endpoints
The backend provides comprehensive RESTful APIs with real-time capabilities:
Analysis Endpoints
- `GET /api/analysis/:symbol` - Get comprehensive stock analysis with dip detection
- `POST /api/analysis/:symbol/analyze` - Trigger new analysis with custom parameters
- `GET /api/analysis/:symbol/dips` - Get detailed dip events with filtering
- `GET /api/analysis/:symbol/status` - Get analysis cache and processing status
- `DELETE /api/analysis/:symbol/cache` - Clear cached analysis results
- `POST /api/analysis/batch` - Batch analysis for multiple symbols
Data Management Endpoints
- `GET /api/data/:symbol` - Get historical stock data with date range filtering
- `POST /api/data/:symbol/fetch` - Fetch new data from external APIs
- `GET /api/data/:symbol/coverage` - Check data coverage and gaps
- `GET /api/data/system/api-usage` - Monitor API usage and rate limiting
Machine Learning Endpoints
- `GET /api/ml/models` - List all trained ML models with metadata
- `POST /api/ml/models` - Create new ML model with custom configuration
- `GET /api/ml/models/:id` - Get specific model details and performance
- `POST /api/ml/train` - Train new model with configurable parameters
- `POST /api/ml/predict` - Real-time predictions using trained models
- `DELETE /api/ml/models/:id` - Delete specific model
Real-time & Notification Endpoints
- `GET /api/realtime/status` - WebSocket connection status
- `POST /api/realtime/subscribe` - Subscribe to real-time updates
- `GET /api/notifications` - Get notification history
- `POST /api/notifications/test` - Test notification system
System & Health Endpoints
- `GET /health` - Application health check
- `GET /api/symbols` - Available stock symbols and metadata
- `GET /docs` - Interactive API documentation (Swagger)
Authentication & Rate Limiting
All endpoints include:
- Request rate limiting (200 requests/minute default)
- Response compression
- CORS support
- Comprehensive error handling
- Request/response logging
Technical Architecture
Frontend Architecture
- React 19: Latest React features with concurrent rendering and Suspense
- TypeScript: Full type safety with strict configuration
- Vite: Lightning-fast build tool with HMR and optimized production builds
- TailwindCSS: Utility-first CSS framework with custom design system
- Recharts: High-performance composable charting library
- Socket.io Client: Real-time bidirectional communication
- React Context: Advanced state management with custom hooks
- Error Boundaries: Comprehensive error handling and user feedback
Backend Architecture
- Node.js + TypeScript: Type-safe server-side development with latest ES modules
- Express.js: Robust web application framework with middleware support
- Prisma ORM: Type-safe database toolkit with migration support
- SQLite: Production-ready database with WAL mode for concurrent access
- Socket.io Server: WebSocket server for real-time bidirectional updates
- Winston: Enterprise-grade logging with multiple transports
- Redis: High-performance caching layer for session and data management
- Compression: Response compression for optimized bandwidth usage
- CORS & Helmet: Security middleware with configurable policies
Machine Learning Stack
- TensorFlow.js: Advanced ML capabilities with WebGL acceleration
- Custom Training Engine: Configurable neural networks with multiple architectures
- Feature Engineering: Technical indicators and market data preprocessing
- Model Persistence: Save/load trained models with metadata tracking
- Real-time Inference: Live predictions with confidence scoring
- Training Monitoring: Progress tracking with adaptive learning rates
Data Processing Pipeline
- Data Ingestion: Intelligent fetching from Alpaca API with rate limiting
- Data Validation: Comprehensive validation and quality checks
- Technical Analysis: Real-time calculation of indicators and patterns
- Dip Detection: Advanced algorithms for identifying price movements
- Feature Engineering: Transform raw data into ML-ready features
- Model Training: Distributed training with progress monitoring
- Prediction Engine: Real-time inference with confidence intervals
- Result Caching: Multi-layer caching with configurable TTL
- Real-time Streaming: WebSocket updates for live analysis
Performance Optimizations
- Smart Caching: 95%+ cache hit rates with intelligent invalidation
- Incremental Updates: Only fetch missing data ranges
- Memory Management: Efficient data structures and garbage collection
- Database Indexing: Optimized queries with composite indexes
- Compression: Gzip compression for all API responses
- Connection Pooling: Database connection optimization
- Rate Limiting: API request queuing and throttling
Troubleshooting
Common Issues & Solutions
Backend Connection Errors
``` Error: ECONNREFUSED connecting to backend Error: WebSocket connection failed Error: Failed to fetch from backend API ``` Solutions:
- Check Backend Status: Ensure backend server is running on port 3486: ```bash cd backend && npm run dev ```
- Verify Environment Variables: Check that `.env` file in backend directory has correct configuration
- Check Port Availability: Ensure port 3486 is not blocked by firewall or other applications
- Network Issues: Verify frontend can reach backend (check CORS settings)
API Authentication Errors
``` Error: Invalid API credentials Error: API key authentication failed Error: 401 Unauthorized ``` Solutions:
- Verify Alpaca Credentials: Double-check API key and secret in both `.env.local` and `backend/.env`
- Check Permissions: Ensure your Alpaca account has market data permissions
- Environment Selection: Verify you're using the correct environment (paper vs live trading)
- Key Format: Ensure API keys don't have extra spaces or special characters
- Account Status: Check if your Alpaca account is active and in good standing
Database Connection Issues
``` Error: Cannot connect to database Error: Prisma migration failed Error: Database is locked ``` Solutions: ```bash cd backend
Reset and recreate database
npx prisma migrate reset --force npx prisma generate npx prisma migrate dev
Alternative: Direct database reset
npx prisma db push --force-reset ```
Memory Issues with Large Datasets
``` Error: JavaScript heap out of memory Error: Out of memory during analysis Error: Cannot allocate memory for dataset ``` Solutions:
- Reduce Analysis Scope: Use shorter time periods or fewer dip percentages
- Enable Data Compression: Configure data compression in settings
- Use Data Virtualization: Enable virtual scrolling for large datasets
- Increase Node Memory: Run with increased memory allocation: ```bash NODE_OPTIONS="--max-old-space-size=4096" npm run dev ```
- Clear Cache: Clear analysis cache to free up memory
Machine Learning Training Issues
``` Error: Training failed to converge Error: Model training timeout Error: Insufficient training data ``` Solutions:
- Adjust Training Parameters: Reduce epochs or increase learning rate
- Check Data Quality: Ensure sufficient historical data for training
- Model Architecture: Try simpler network architecture for limited data
- Training Data: Verify dip events exist for the selected stock and timeframe
- Memory Allocation: Ensure sufficient memory for TensorFlow.js operations
Real-time Connection Issues
``` Error: WebSocket connection dropped Error: Real-time updates not working Error: Connection timeout ``` Solutions:
- Check Network: Verify stable internet connection
- Backend Status: Ensure WebSocket server is running
- Browser Support: Check if browser supports WebSocket connections
- Firewall Settings: Allow WebSocket connections on port 3486
- Reconnect Logic: The application will automatically attempt reconnection
Performance Issues
``` Error: Analysis taking too long Error: UI freezing during analysis Error: Slow chart rendering ``` Solutions:
- Enable Caching: Use cached results when available
- Reduce Data Range: Analyze shorter time periods
- Optimize Timeframe: Use higher timeframe intervals for large datasets
- Browser Performance: Close unnecessary tabs and extensions
- Hardware Acceleration: Enable GPU acceleration in browser settings
Data Quality Issues
``` Error: Insufficient data points Error: Data gaps detected Error: Invalid price data ``` Solutions:
- Check Symbol: Verify the stock symbol is valid and actively traded
- Extend Time Range: Use longer analysis periods to capture more data
- Market Hours: Ensure analysis includes market trading hours
- Data Source: Verify Alpaca API is functioning correctly
- Gap Filling: Enable automatic gap filling in settings
Development Tips
- Use TypeScript: Take advantage of full type safety throughout the application
- Monitor Performance: Use browser DevTools and React DevTools for performance analysis
- Test Thoroughly: Run both unit and integration tests before deployment
- Follow Code Style: Use the provided ESLint and Prettier configurations
- Documentation: Keep documentation updated with code changes
- Memory Profiling: Use Node.js memory profiling tools for optimization
- Database Monitoring: Use Prisma Studio to monitor database performance
- API Monitoring: Monitor API usage to stay within rate limits
Getting Help
For additional support:
- Check Logs: Review backend logs in the console for detailed error messages
- Database Inspection: Use `npx prisma studio` to inspect database state
- API Testing: Test individual endpoints using the Swagger documentation at `/docs`
- Community: Check GitHub issues for similar problems and solutions
- Debug Mode: Enable debug mode in environment variables for detailed logging
Contributing
- Fork the repository
- Create a feature branch: `git checkout -b feature/your-feature-name`
- Make your changes and add tests
- Run the test suite: `npm test`
- Commit your changes: `git commit -m 'Add some feature'`
- Push to the branch: `git push origin feature/your-feature-name`
- Submit a pull request
Development Guidelines
- Follow the existing code style and patterns
- Add tests for new features
- Update documentation for API changes
- Use semantic commit messages
- Ensure all CI checks pass
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support and questions:
- Create an issue on GitHub
- Check the documentation folder for detailed guides
- Review the troubleshooting section
Changelog
See CHANGELOG.md for version history and updates.