The Commodities Quantitative Risk Engine (CQRE) is a robust backend system designed to analyze real-time and historical data of WTI and Brent crude oil prices. The system employs advanced financial models such as Black-Scholes Option Pricing and Mean Reversion Spread Analysis to generate actionable trading signals and evaluate strategy performance.
This project demonstrates:
- Integration of real-time data processing using Kafka.
- Advanced financial modeling with Python.
- API-driven architecture for analytical insights.
- Scalable backend setup for commodities trading.
-
Real-Time Data Analysis:
- Fetches live WTI and Brent prices through a Kafka producer.
- Processes the prices via a Kafka consumer to calculate spreads and generate trading signals.
-
Financial Models:
- Black-Scholes Option Pricing: Calculates the fair value of options based on WTI/Brent prices.
- Mean Reversion Analysis: Detects price deviations and generates buy/sell signals.
-
APIs:
- Historical Spread Data: Fetch stored spread data from PostgreSQL.
- Black-Scholes Pricing: API to calculate the price of options.
- Mean Reversion: API to use the mean reversion strategy.
-
Data Storage:
- Stores real-time price and spread data in a PostgreSQL database for historical analysis.
- Programming Language: Python
- Message Broker: Apache Kafka
- Database: PostgreSQL
- Backend Framework: Flask
- Data Models:
- Black-Scholes Option Pricing for financial analysis.
- Mean Reversion Spread Analysis for trading signals.
- Endpoint:
GET /api/historical-spreads
- Description: Fetch historical WTI and Brent price spreads.
- Response:
[ {"timestamp": "2024-12-08T10:00:00", "wti_price": 75.32, "brent_price": 78.54, "spread": 3.22}, {"timestamp": "2024-12-08T10:05:00", "wti_price": 76.10, "brent_price": 79.80, "spread": 3.70} ]
- Endpoint:
POST /api/option-pricing
- Description: Calculate the fair value of an option using Black-Scholes.
- Request:
{ "option_type": "call", "S": 75.0, "K": 80.0, "T": 0.5, "r": 0.02, "sigma": 0.3 }
- Response:
{ "price": 5.34 }
- Kafka Producer:
- Produces random or real-time WTI and Brent prices every 1 second.
- Kafka Consumer:
- Consumes prices, calculates spreads, stores data in PostgreSQL, and generates trading signals.
- API Layer:
- Exposes historical data, Mean Reversion and Black-Scholes calculations via Flask endpoints.
- Python: Version 3.11 or higher.
- Kafka: Apache Kafka should be installed and running on
localhost:9092
. - PostgreSQL: Database should be running on
localhost:5432
. - Python Packages:
- Install dependencies from
requirements.txt
:pip install -r requirements.txt
- Install dependencies from
-
Install Python 3.11 or higher.
-
Install PostgreSQL and create a database:
CREATE DATABASE cqre; CREATE USER cqre WITH PASSWORD 'cqre'; GRANT ALL PRIVILEGES ON DATABASE cqre TO cqre;
-
Start Kafka:
kafka-server-start.sh config/server.properties
Create a .env
file or export the following:
DATABASE_URL=postgresql://cqre:cqre@localhost:5432/cqre
KAFKA_BOOTSTRAP_SERVERS=localhost:9092