|
|
||
|---|---|---|
| .. | ||
| __pycache__ | ||
| scripts | ||
| services | ||
| utils | ||
| EXAMPLE_OUTPUT.json | ||
| INSTITUTIONAL_ANALYTICS_README.md | ||
| README.md | ||
| TESTING.md | ||
| db.py | ||
| main.py | ||
| requirements.txt | ||
| start.bat | ||
| start.sh | ||
README.md
Python Options Flow Processing Service
This service extracts complex SQL logic into Python/pandas for better maintainability and easier testing.
Architecture
- FastAPI: Modern async web framework
- pandas: Data processing and analytics
- asyncpg: Async PostgreSQL driver
- Replaces: Complex 593-line SQL queries with modular Python code
Setup
- Install dependencies:
cd backend/python_service
pip install -r requirements.txt
- Configure environment:
cp .env.example .env
# Edit .env with your database credentials
- Run the service:
python main.py
# Or with uvicorn:
uvicorn main:app --reload --port 8010
API Endpoints
GET /health
Health check endpoint
GET /api/options-flow
Get processed options flow data
Query Parameters:
start_date(optional): Start date (YYYY-MM-DD), defaults to yesterdayend_date(optional): End date (YYYY-MM-DD), defaults to todaymin_premium(optional): Minimum premium filter, defaults to 80000tol_pct(optional): Tape alignment tolerance, defaults to 0.20
Example:
curl http://localhost:8010/api/options-flow?start_date=2024-01-01&end_date=2024-01-02
GET /api/options-flow/stats
Get flow statistics
Query Parameters:
symbol(optional): Filter by symbol
Integration with Node.js
The Node.js API layer can call this service via HTTP:
const response = await fetch('http://localhost:8010/api/options-flow?start_date=2024-01-01');
const data = await response.json();
Benefits
- Maintainability: Complex SQL logic broken into Python functions
- Testability: Each processing step can be unit tested
- Flexibility: Easy to add new analytics or modify existing ones
- Performance: Can leverage pandas vectorization and parallel processing
- Debugging: Easier to debug Python code than complex SQL
Migration Path
- ✅ Python service created
- ⏳ Node.js routes updated to call Python service
- ⏳ Additional SQL scripts migrated
- ⏳ Full testing and validation