A high-performance FastAPI application for calculating and serving technical indicators for stock data with tiered subscription access.
- Technical Indicators: SMA, EMA, RSI, MACD, Bollinger Bands
- Tiered Access: Free, Pro, Premium subscription levels
- Authentication: JWT and API Key authentication
- Rate Limiting: Tier-based request limiting
- Caching: Redis-based caching for performance
- High Performance: Polars for efficient data processing
- Containerized: Docker and Docker Compose setup
- Testing: Comprehensive test suite
- Documentation: Auto-generated OpenAPI/Swagger docs
| Tier | Requests/Day | Indicators | Data Range |
|---|---|---|---|
| Free | 50 | SMA, EMA | Last 3 months |
| Pro | 500 | SMA, EMA, RSI, MACD | Last 1 year |
| Premium | Unlimited | All indicators | Full 3 years |
- Framework: FastAPI
- Language: Python 3.12+
- Data Processing: Polars (primary), Pandas (fallback)
- Database: PostgreSQL
- Caching: Redis
- Authentication: JWT + API Keys
- Server: Uvicorn
- Testing: pytest
- Containerization: Docker
kalpi-tech-api/
βββ app/
β βββ main.py # FastAPI application
β βββ models/ # Pydantic models
β βββ indicators/ # Technical indicator calculations
β βββ services/ # Business logic (data, cache, rate limiting)
β βββ routers/ # API endpoints
β βββ database/ # Database models and connection
β βββ core/ # Configuration and utilities
β βββ auth/ # Authentication logic
βββ data/ # Stock data storage
βββ tests/ # Test suite
βββ docker-compose.yml # Development environment
βββ Dockerfile # Container definition
βββ README.md # This file
- Python 3.12+
- UV (recommended) or pip
- Docker and Docker Compose (optional)
- PostgreSQL (if running locally)
- Redis (if running locally)
git clone <repository-url>
cd kalpi-tech-api
# Copy environment file
cp .env.example .env
# Edit .env with your configuration# Install dependencies
uv sync
# Run the application
uv run uvicorn app.main:app --reload# Start all services
docker-compose up --build
# Run in background
docker-compose up -d --build# Install dependencies
pip install -r requirements.txt
# Start PostgreSQL and Redis
# (Configure connection strings in .env)
# Run the application
uvicorn app.main:app --reloadOnce running, visit:
- Interactive docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health check: http://localhost:8000/health
- Register a user:
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "password123"
}'- Login to get token:
curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=testuser&password=password123"- Use token in requests:
curl -X GET "http://localhost:8000/api/v1/indicators/sma?symbol=AAPL&window=20" \
-H "Authorization: Bearer <your-token>"- Create API key (requires JWT token):
curl -X POST "http://localhost:8000/api/v1/auth/api-key" \
-H "Authorization: Bearer <your-token>"- Use API key in requests:
curl -X GET "http://localhost:8000/api/v1/indicators/sma?symbol=AAPL&window=20&api_key=<your-api-key>"curl -X GET "http://localhost:8000/api/v1/indicators/sma?symbol=AAPL&window=20&start_date=2023-01-01&end_date=2023-12-31" \
-H "Authorization: Bearer <token>"curl -X GET "http://localhost:8000/api/v1/indicators/ema?symbol=AAPL&window=20" \
-H "Authorization: Bearer <token>"curl -X GET "http://localhost:8000/api/v1/indicators/rsi?symbol=AAPL&period=14" \
-H "Authorization: Bearer <token>"curl -X GET "http://localhost:8000/api/v1/indicators/macd?symbol=AAPL&fast_period=12&slow_period=26&signal_period=9" \
-H "Authorization: Bearer <token>"curl -X GET "http://localhost:8000/api/v1/indicators/bollinger_bands?symbol=AAPL&period=20&std_dev=2.0" \
-H "Authorization: Bearer <token>"Run the test suite:
# Using UV
uv run pytest
# Using pip
pytest
# With coverage
pytest --cov=app --cov-report=htmlKey environment variables:
# Security
SECRET_KEY=your-secret-key-change-this-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Database
DATABASE_URL=postgresql://user:password@localhost/kalpi_db
# Redis
REDIS_URL=redis://localhost:6379
CACHE_EXPIRE_MINUTES=30
# Data
DATA_FILE_PATH=data/stocks_ohlc_data.parquet
# Rate Limiting
RATE_LIMIT_FREE=50
RATE_LIMIT_PRO=500
RATE_LIMIT_PREMIUM=
# Data Access (days)
DATA_LIMIT_FREE=90
DATA_LIMIT_PRO=365
DATA_LIMIT_PREMIUM=
# Debug
DEBUG=false# Build and run with Docker Compose
docker-compose -f docker-compose.yml up -d
# Scale the API service
docker-compose up -d --scale api=3-
Security:
- Change
SECRET_KEYin production - Use environment variables for secrets
- Enable HTTPS
- Configure CORS properly
- Change
-
Database:
- Use managed PostgreSQL service
- Configure connection pooling
- Set up database backups
-
Caching:
- Use managed Redis service
- Configure Redis persistence
- Set up Redis clustering for high availability
-
Monitoring:
- Add health checks
- Set up logging aggregation
- Configure metrics collection
-
Database connection failed:
- Check PostgreSQL is running
- Verify connection string in
.env - Check database exists
-
Redis connection failed:
- Check Redis is running
- Verify Redis URL in
.env - Check Redis is accepting connections
-
Data file not found:
- Ensure
stocks_ohlc_data.parquetexists indata/directory - Check file permissions
- Verify file path in configuration
- Ensure
-
Rate limiting not working:
- Check Redis connection
- Verify user authentication
- Check rate limit configuration
Enable debug mode for detailed logging:
DEBUG=truePOST /api/v1/auth/register- Register new userPOST /api/v1/auth/login- Login userGET /api/v1/auth/me- Get current user infoPOST /api/v1/auth/api-key- Create API keyGET /api/v1/auth/api-keys- List API keys
GET /api/v1/indicators/sma- Simple Moving AverageGET /api/v1/indicators/ema- Exponential Moving AverageGET /api/v1/indicators/rsi- Relative Strength Index (Pro+)GET /api/v1/indicators/macd- MACD (Pro+)GET /api/v1/indicators/bollinger_bands- Bollinger Bands (Premium)
GET /health- Health checkGET /data-info- Data informationGET /- API information
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run the test suite
- Submit a pull request
This project is licensed under the MIT License.