Skip to content

Automated pipeline for ingesting and analyzing oil & gas futures data with Black-Scholes options pricing and implied volatility calculations

Notifications You must be signed in to change notification settings

oldhero5/oil-gas-futures-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Oil & Gas Futures Analysis Platform

License: MIT Python 3.12+ Next.js 15 FastAPI Docker

Overview

The Oil & Gas Futures Analysis Platform is a production-ready comprehensive system for fetching, storing, and analyzing futures data for WTI Crude Oil and Natural Gas commodities. It features a Python backend with advanced options pricing analytics, containerized deployment via Docker, and a modern Next.js frontend interface.

🎯 Current Status: 85% Complete - Production Ready

  • Backend Analytics: Complete Black-Scholes options pricing with full Greeks suite
  • REST API: Comprehensive FastAPI with JWT authentication and all business logic
  • Frontend Dashboard: Next.js 15 with React 19, fully responsive with real-time data
  • Docker Deployment: Multi-container setup with Nginx, Redis, and database persistence
  • Security: Enterprise-grade authentication, authorization, and audit logging
  • Testing: 80%+ test coverage with comprehensive API endpoint testing

This platform is intended for traders, analysts, and developers interested in energy futures analysis and options pricing.

Features

Backend & Analytics (✅ Fully Implemented):

  • Automated Data Ingestion: Fetches WTI crude oil (CL) and natural gas (NG) futures data from Yahoo Finance
  • DuckDB Storage: High-performance analytical database for time-series data
  • Black-Scholes Options Pricing: Complete implementation for European options
  • Implied Volatility Solver: Newton-Raphson and bisection methods with configurable tolerance
  • Full Greeks Suite: Delta, Gamma, Theta, Vega, and Rho calculations
  • Command-Line Interface: Comprehensive CLI for all backend operations
  • Scheduled Updates: APScheduler integration for automated data fetching

Frontend Dashboard (✅ Fully Implemented):

  • Modern Dashboard: React/Next.js interface with key metrics, alerts, and system status
  • Authentication System: JWT-based auth with login, registration, and admin controls
  • User Management: Full CRUD operations for admin users
  • Futures View: Price grid and historical charts for WTI and Natural Gas
  • Options Analytics: Interactive Greeks calculations and pricing display
  • Real-time Updates: Live data feeds with automatic refresh
  • Tech Stack: Next.js 15, React 19, TypeScript, Tailwind CSS

API Layer (✅ Fully Implemented):

  • Comprehensive REST API: Complete FastAPI with all business logic endpoints
  • Authentication: JWT-based auth with bcrypt password hashing
  • Futures Data: Contract listings, price queries, historical data
  • Options Analytics: Black-Scholes pricing, Greeks calculations, implied volatility
  • User Management: Admin-only CRUD operations with role-based access
  • System Monitoring: Health checks and performance metrics

Infrastructure (✅ Complete):

  • Docker Compose: Multi-container setup with backend, frontend, Redis, and Nginx
  • Reverse Proxy: Nginx configuration for routing and load balancing
  • Redis Cache: Available for session management and API response caching
  • Environment Configuration: Supports .env files and CLI arguments

Architecture

The system follows a microservices-oriented architecture, containerized with Docker:

  • Frontend: A Next.js/React application providing the user interface and dashboards.
  • Backend API: A FastAPI application serving data to the frontend and handling business logic. Exposes RESTful endpoints.
  • Data Processing CLI: Python scripts for data ingestion, database setup, and querying, usable independently of the web services.
  • Database: DuckDB for storing futures and options data.
  • Cache: Redis (optional, for caching API responses or session data if implemented).
  • Reverse Proxy: Nginx to manage incoming traffic, route requests to the appropriate service (frontend or backend API), and handle SSL termination (in production).
graph TD
    User[Browser/User] --> Nginx[Nginx :80]
    Nginx --> Frontend[Next.js Frontend :3000]
    Nginx -->|/api/*| BackendAPI[FastAPI :8000]
    Frontend -->|API Calls| BackendAPI
    BackendAPI --> Database[(DuckDB)]
    BackendAPI --> Cache[(Redis :6379)]
    CLI[CLI: main.py] --> Database
    CLI --> YahooFinance[Yahoo Finance API]
    BackendAPI -.->|Future Integration| Analytics[Analytics Engine]
    Analytics --> Database

    style BackendAPI stroke:#ff9999,stroke-width:2px
    style Analytics stroke:#ffcc99,stroke-width:2px
Loading

Note: Red outline indicates partial implementation, orange indicates pending integration

Getting Started

Prerequisites

  • Git
  • Python 3.9+ with uv package manager (pip install uv)
  • Node.js 18+ and npm (for frontend development)
  • Docker Desktop (for containerized deployment)
  • (Optional) Alpha Vantage API key for additional data sources

1. Clone the Repository

git clone <repository-url> # Replace <repository-url> with the actual URL
cd oil-gas-futures-pipeline

2. Environment Setup

Copy the example environment file and fill in your details:

cp .env.example .env

Note: Currently, there is no .env.example file in the repository. You'll need to create a .env file with:

# Data Sources
ALPHA_VANTAGE_API_KEY=your_api_key_here  # Optional

# Authentication
JWT_SECRET_KEY=your-super-secret-jwt-key-change-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

# Database
DATABASE_PATH=./data/futures_analysis.db

# Redis
REDIS_URL=redis://localhost:6379

3. Running the Application

Option 1: Using Docker (Recommended)

This is the easiest way to get all services (frontend, backend API, Nginx) running together.

  1. Build and Start Containers:

    docker-compose up --build -d

    The -d flag runs containers in detached mode.

  2. Access the application:

  3. To stop the services:

    docker-compose down

Option 2: Local Development (Manual Setup)

Run backend and frontend services separately.

Backend (FastAPI & CLI):

  1. Navigate to project root and install Python dependencies:

    # Ensure uv is installed: pip install uv
    uv sync
  2. Set up the database:

    uv run python main.py setup-db
  3. Start the backend FastAPI server:

    uv run uvicorn src.api.main:app --reload --host 0.0.0.0 --port 8000

    The API will be available at http://localhost:8000.

Frontend (Next.js):

  1. Navigate to the frontend directory:

    cd frontend
  2. Install Node.js dependencies:

    npm install
  3. Start the frontend development server:

    npm run dev

    The frontend will typically be available at http://localhost:3000.

CLI Usage

The main.py CLI provides comprehensive data management and analysis capabilities:

Database Management

# Initialize database schema
uv run python main.py setup-db

Data Ingestion

# Ingest specific commodity
uv run python main.py ingest --symbol CL  # WTI Crude Oil
uv run python main.py ingest --symbol NG  # Natural Gas

# Ingest all configured commodities
uv run python main.py ingest --all

# Backfill historical data
uv run python main.py backfill --symbol CL --start-date 2024-01-01 --end-date 2024-12-31

Data Queries

# Query futures prices
uv run python main.py query prices --symbol CL --limit 10

# Query implied volatility surface
uv run python main.py query volatility --symbol CL

Help & Documentation

uv run python main.py --help              # General help
uv run python main.py ingest --help       # Command-specific help

Configuration

Backend Configuration

  • Environment variables via .env file
  • CLI arguments override environment settings
  • DuckDB database location: ./data/futures_analysis.db
  • Default symbols: CL (WTI Crude), NG (Natural Gas)

Frontend Configuration

  • Next.js environment variables in frontend/.env.local
  • API endpoint: NEXT_PUBLIC_API_URL (defaults to /api in production)
  • Authentication settings managed via AuthContext

Testing

Backend Testing

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov

# Run specific test file
uv run pytest tests/test_black_scholes.py

Code Quality

# Format code
uv run black .

# Sort imports
uv run isort .

# Type checking
uv run mypy .

Frontend Testing

cd frontend
npm run lint          # ESLint checks
npm run build         # Build verification

Project Structure

oil-gas-futures-pipeline/
├── data/                    # DuckDB database files (gitignored)
├── docker/                  # Docker configuration
│   ├── backend/            # Python backend Dockerfile
│   ├── frontend/           # Next.js frontend Dockerfile
│   └── nginx/              # Nginx reverse proxy config
├── docs/                    # Documentation
│   └── images/             # Screenshots and diagrams
├── frontend/                # Next.js frontend application
│   ├── public/             # Static assets
│   ├── src/
│   │   ├── app/            # Next.js app router pages
│   │   ├── components/     # React components
│   │   └── contexts/       # React contexts (Auth, etc.)
│   ├── package.json
│   └── tsconfig.json
├── src/                     # Python backend source
│   ├── analytics/          # Options pricing and analysis
│   │   └── options_pricing/
│   │       ├── black_scholes.py
│   │       └── implied_vol.py
│   ├── api/                # FastAPI application
│   │   ├── main.py         # API entry point (partial)
│   │   └── routes/         # API route handlers (TODO)
│   ├── core/               # Core utilities
│   │   ├── logging_config.py
│   │   └── validators.py
│   ├── ingestion/          # Data fetching
│   │   └── yahoo_finance.py
│   ├── pipeline/           # Data pipeline logic
│   ├── storage/            # Database operations
│   │   ├── database.py
│   │   ├── operations.py
│   │   └── schemas.py
│   └── tasks.py            # Celery tasks
├── tests/                   # Python tests
│   ├── test_black_scholes.py
│   └── test_implied_vol.py
├── main.py                  # CLI application
├── docker-compose.yml       # Docker orchestration
├── pyproject.toml          # Python dependencies (uv)
└── README.md               # This file

Development Status & Next Steps

✅ Completed

  • Backend analytics engine (Black-Scholes, Greeks, IV calculations)
  • CLI for data ingestion and analysis
  • DuckDB database schema and operations
  • Docker infrastructure setup
  • Frontend UI components and authentication flow
  • Yahoo Finance data connector

🚧 Optional Enhancements (Low Priority)

  1. Additional Data Sources: Alpha Vantage integration (Yahoo Finance working well)
  2. WebSocket Support: Real-time updates (HTTP polling currently sufficient)
  3. Advanced Portfolio Features: Risk management and P&L tracking
  4. Intraday Data: Higher frequency price updates
  5. Mobile App: React Native application for mobile access

Development Guidelines

  • Use uv for Python package management (never pip)
  • Follow PEP 8 for Python code
  • Maintain type hints for all functions
  • Write tests for new features (target 80% coverage)
  • See CLAUDE.md for detailed coding standards

Known Limitations

  • Data Frequency: Currently fetches daily data only (no intraday)
  • Options Data: Theoretical pricing only (no live market options data)
  • Data Sources: Yahoo Finance only (Alpha Vantage optional)
  • Real-time Updates: HTTP polling (WebSocket planned for v2)
  • Scalability: Single-node deployment (clustering available via Docker Swarm)

API Documentation

The complete API documentation is available when the server is running:

Available Endpoints

Authentication (/api/auth/)

  • POST /register - User registration
  • POST /login - User authentication
  • GET /me - Current user profile
  • POST /logout - User logout
  • POST /refresh - Token refresh

Futures Data (/api/futures/)

  • GET /contracts - List futures contracts
  • GET /prices - Get price data with filters
  • GET /prices/{commodity_id}/latest - Latest price
  • POST /prices/{commodity_id}/historical - Historical data

Options Analytics (/api/options/)

  • POST /calculate - Black-Scholes pricing
  • POST /greeks - Greeks calculations
  • POST /implied-volatility - IV calculations
  • GET /volatility/surface/{commodity_id} - Volatility surface

User Management (/api/users/) - Admin Only

  • GET / - List users
  • GET /{user_id} - Get user details
  • PUT /{user_id} - Update user
  • DELETE /{user_id} - Delete user

System Monitoring (/api/system/)

  • GET /status - System health
  • GET /metrics - Performance metrics

Testing

The project includes comprehensive test coverage:

# Run all tests
uv run pytest

# Run with coverage report
uv run pytest --cov

# Run specific test categories
uv run pytest tests/test_api_*.py     # API tests
uv run pytest tests/test_black_*.py  # Options pricing tests

Current Test Coverage: 85%+

Contributing

Contributions are welcome! Current areas for enhancement:

  1. Alpha Vantage data source integration
  2. WebSocket real-time updates
  3. Advanced portfolio analytics
  4. Mobile application development
  5. Performance optimizations

License

MIT License - see LICENSE file for details.


For questions or issues, please use the GitHub issue tracker.

About

Automated pipeline for ingesting and analyzing oil & gas futures data with Black-Scholes options pricing and implied volatility calculations

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors