Skip to content

quantumiodb/ipo

Repository files navigation

IPO Data Integration Implementation

概述 (Overview)

已完成美股和港股IPO数据源的自动同步系统集成,支持从 Finnhub API 获取美股IPO数据,从 HKEX FINI API 获取港股IPO数据,并自动插入到数据库中。

This implementation provides automated IPO data synchronization from two major sources:

  • Finnhub API: US market IPO data
  • HKEX FINI API: Hong Kong market IPO data

🚀 Key Features

Data Sources Integration

  1. Finnhub API (US Market)

    • Real-time IPO calendar data
    • 60 API calls per minute (free tier)
    • Price ranges, share offerings, and listing dates
    • Automatic status mapping
  2. HKEX FINI API (Hong Kong Market)

    • Digital IPO settlement platform
    • T+2 settlement timeline
    • OAuth2 authentication
    • End-to-end settlement process data

Data Synchronization Features

  • Intelligent Deduplication: Prevents duplicate entries based on symbol+market
  • Data Validation: Comprehensive validation before database insertion
  • Rate Limiting: Built-in rate limiting for API calls
  • Error Handling: Robust error handling with detailed logging
  • Incremental Updates: Only updates records when data has actually changed

📁 File Structure

src/
├── lib/
│   ├── external-apis.ts          # API service classes and data transformers
│   └── data-sync.ts              # Main synchronization service
├── app/api/sync/
│   ├── route.ts                  # General sync endpoints
│   ├── finnhub/route.ts          # US market specific endpoints
│   ├── hkex/route.ts             # HK market specific endpoints
│   └── scheduled/route.ts        # Scheduled sync endpoint
├── app/sync/
│   └── page.tsx                  # Data sync management page
└── components/
    └── DataSyncManager.tsx       # Frontend sync management component

🔧 API Endpoints

Sync Management

  • GET /api/sync - Get synchronization status
  • POST /api/sync - Trigger full sync (both markets)
  • POST /api/sync/finnhub - Sync US IPOs only
  • POST /api/sync/hkex - Sync Hong Kong IPOs only
  • POST /api/sync/scheduled - Endpoint for scheduled tasks

Response Format

{
  "success": true,
  "data": {
    "us": {
      "success": true,
      "processed": 25,
      "added": 5,
      "updated": 3,
      "skipped": 17,
      "errors": []
    },
    "hk": {
      "success": true,
      "processed": 12,
      "added": 2,
      "updated": 1,
      "skipped": 9,
      "errors": []
    }
  },
  "message": "Data synchronization completed",
  "timestamp": "2025-08-23T02:45:00.000Z"
}

⚙️ Configuration

Environment Variables

Add these to your .env file:

# Finnhub API Configuration
FINNHUB_API_KEY="your-finnhub-api-key"
FINNHUB_BASE_URL="https://finnhub.io/api/v1"

# HKEX FINI API Configuration  
HKEX_FINI_CLIENT_ID="your-hkex-client-id"
HKEX_FINI_CLIENT_SECRET="your-hkex-client-secret"
HKEX_FINI_BASE_URL="https://api.hkex.com.hk/fini"

# Scheduled Tasks Security
CRON_SECRET_TOKEN="your-secret-cron-token"

API Keys Setup

Finnhub API

  1. Visit finnhub.io
  2. Create a free account
  3. Get your API key from the dashboard
  4. Free tier: 60 calls/minute, sufficient for IPO calendar data

HKEX FINI API

  1. Visit HKEX FINI Platform
  2. Complete registration process
  3. Obtain client credentials (requires business registration)
  4. Download API documentation from HKEX website

📊 Data Mapping

Finnhub to Database

{
  symbol: finnhubData.symbol,
  companyName: finnhubData.name,
  market: 'US',
  expectedPrice: parsedPrice,
  priceRange: finnhubData.price,
  sharesOffered: finnhubData.numberOfShares,
  ipoDate: new Date(finnhubData.date),
  status: mapFinnhubStatus(finnhubData.status)
}

HKEX to Database

{
  symbol: hkexData.symbol,
  companyName: hkexData.companyName,  
  market: 'HK',
  expectedPrice: hkexData.offerPrice,
  sharesOffered: hkexData.sharesOffered,
  ipoDate: new Date(hkexData.listingDate),
  status: mapHkexStatus(hkexData.status),
  sector: hkexData.sector
}

🔄 Automated Scheduling

Manual Triggers

  • Use the web interface at /sync
  • Call API endpoints directly
  • Trigger via curl or external scripts

External Cron Setup

# Daily sync at 9 AM
0 9 * * * curl -X POST \
  -H "Authorization: Bearer YOUR_CRON_TOKEN" \
  http://localhost:3000/api/sync/scheduled

# US market sync every 4 hours during weekdays
0 */4 * * 1-5 curl -X POST \
  -H "Authorization: Bearer YOUR_CRON_TOKEN" \
  http://localhost:3000/api/sync/finnhub

Cloud Deployment Options

  • Vercel: Use Vercel Cron Jobs
  • Netlify: Use Netlify Scheduled Functions
  • AWS: Use EventBridge with Lambda
  • Railway: Use Railway Cron
  • Heroku: Use Heroku Scheduler

🎯 Usage Examples

Frontend Integration

// Trigger full sync
const response = await fetch('/api/sync', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' }
});

// Sync specific market
const usSync = await fetch('/api/sync/finnhub', {
  method: 'POST'
});

External API Calls

# Get sync status
curl http://localhost:3000/api/sync

# Trigger US IPO sync
curl -X POST http://localhost:3000/api/sync/finnhub

# Trigger HK IPO sync  
curl -X POST http://localhost:3000/api/sync/hkex

🛡️ Security Features

  • API Authentication: Bearer tokens for scheduled endpoints
  • Rate Limiting: Built-in protection against API abuse
  • Input Validation: All external data validated before database insertion
  • Error Boundaries: Graceful handling of API failures
  • Logging: Comprehensive logging for debugging and monitoring

📈 Monitoring & Debugging

Sync Status Monitoring

  • View sync history in the web interface
  • Check API endpoint responses
  • Monitor database record counts
  • Review error logs

Common Issues

  1. API Key Issues: Check environment variables
  2. Rate Limits: Monitor API call frequency
  3. Network Issues: Check API endpoint availability
  4. Data Validation: Review error logs for validation failures

🔮 Future Enhancements

Planned Features

  • Real-time Webhooks: Immediate updates when new IPOs are announced
  • Email Notifications: Alert users about new IPO listings
  • Data Analytics: Advanced metrics and reporting
  • Mobile App: React Native or Flutter integration
  • AI Insights: ML-powered IPO performance predictions

Scaling Considerations

  • Database Optimization: Indexing for large datasets
  • Caching: Redis for frequently accessed data
  • Load Balancing: Multiple API instances
  • Microservices: Separate sync services

💡 Best Practices

Data Quality

  • Regular data validation checks
  • Monitoring for stale data
  • Cross-validation between sources
  • Manual review for significant discrepancies

Performance

  • Batch processing for large datasets
  • Async processing for non-blocking operations
  • Database connection pooling
  • API response caching

Reliability

  • Retry logic for failed API calls
  • Backup data sources
  • Regular health checks
  • Monitoring and alerting

🎉 Implementation Complete!

The IPO data integration system is now fully functional with: ✅ Finnhub API Integration - US market IPO data ✅ HKEX FINI API Integration - Hong Kong market IPO data
Automated Synchronization - Smart deduplication and updates ✅ Web Interface - Easy management and monitoring ✅ API Endpoints - Programmatic access and control ✅ Scheduled Tasks - Automated daily updates ✅ Comprehensive Documentation - Setup and usage guides

Ready for production deployment and can be extended with additional features as needed!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors