File size: 2,826 Bytes
b8d0772
 
a8f56ca
b8d0772
a8f56ca
b8d0772
 
 
8cad073
b8d0772
 
a8f56ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
title: Ticker Monitor Api
emoji: πŸ“‰
colorFrom: purple
colorTo: pink
sdk: docker
pinned: false
license: mit
short_description: SP500 & NASDAQ ticker monitoring API
---

# Stock Monitoring API πŸ“ˆ

A FastAPI-based REST API for monitoring S&P 500 and Nasdaq 100 stock tickers with real-time data updates.

## Features

- **Stock Data Management**: Query and update tickers for S&P 500 and Nasdaq 100 indices
- **Automatic Data Updates**: Background tasks to fetch latest stock data from Yahoo Finance  
- **RESTful API**: Clean, documented endpoints with OpenAPI/Swagger integration
- **MySQL Integration**: Persistent storage with async SQLAlchemy
- **Authentication**: Secure API key-based authentication
- **Health Monitoring**: Built-in health checks and version information

## API Endpoints

### Public Endpoints
- `GET /` - Health check and system information
- `GET /tickers` - List all available tickers with filtering options
- `GET /data/tickers/{ticker}` - Get historical data for a specific ticker

### Protected Endpoints (Require API Key)
- `POST /tickers/update` - Update ticker list from Wikipedia sources
- `POST /tickers/update-async` - Async ticker updates with task tracking  
- `POST /data/download-all` - Download latest stock data (bulk operation)
- `GET /tasks` - List background tasks
- `GET /tasks/{task_id}` - Get specific task status
- `DELETE /tasks/old` - Clean up old completed tasks

## Usage

### Authentication
Protected endpoints require an API key in the Authorization header:
```bash
Authorization: Bearer your_api_key_here
```

### Example Requests
```bash
# Get system health
curl https://your-space-name.hf.space/

# List S&P 500 tickers
curl "https://your-space-name.hf.space/tickers?is_sp500=true&limit=10"

# Get AAPL stock data (last 30 days)
curl "https://your-space-name.hf.space/data/tickers/AAPL?days=30"

# Update all stock data (requires API key)
curl -X POST "https://your-space-name.hf.space/data/download-all" \
  -H "Authorization: Bearer your_api_key"
```

## Configuration

Set these environment variables in your Hugging Face Space:

- `MYSQL_USER` - Database username
- `MYSQL_PASSWORD` - Database password  
- `MYSQL_HOST` - Database host
- `MYSQL_PORT` - Database port (default: 3306)
- `MYSQL_DB` - Database name
- `API_KEY` - Secure API key for protected endpoints

## Technical Details

- **Framework**: FastAPI with async/await support
- **Database**: MySQL with SQLAlchemy async ORM
- **Data Sources**: Wikipedia (ticker lists), Yahoo Finance (stock data)
- **Deployment**: Docker container optimized for Hugging Face Spaces
- **Python**: 3.12 with production-ready dependencies

## API Documentation

Once deployed, visit `/docs` for interactive Swagger documentation or `/redoc` for alternative API docs.

---

*Built for Hugging Face Spaces with ❀️*