A high-performance asynchronous API for European option pricing using Monte Carlo simulation.
- β‘ Async FastAPI for concurrent request handling
- π Monte Carlo Simulation for European option pricing
- π² Black-Scholes Model for analytical comparison
- π Greeks Calculation (Delta, Gamma, Vega, Theta, Rho)
- β Full CRUD Operations with JSON persistence
- π§ͺ Comprehensive Testing (unit, integration, property-based)
A high-performance asynchronous API for European option pricing using Monte Carlo simulation.
- Async FastAPI for concurrent request handling
- Monte Carlo for European option pricing
- Black-Scholes Model for analytical comparison
- Greeks Calculation (Delta, Gamma, Vega, Theta, Rho)
- Full CRUD Operations with JSON persistence
- Comprehensive Testing (unit, integration, property-based)
- Docker Ready for easy deployment
- Python 3.11+
- WSL2 (Ubuntu) or Linux/macOS
- Git
# Clone the repository
git clone https://github.com/yourusername/optionprisma.git
cd optionprisma
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtDevelopment mode (recommended):
fastapi dev app/main.pyAlternative (explicit control):
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Note:
fastapi devis a development-only command. For production, always useuvicorndirectly.
Visit:
- Interactive API Docs: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
import requests
# Create a simulation
response = requests.post(
"http://localhost:8000/simulations",
json={
"spot_price": 100,
"strike_price": 105,
"time_to_maturity": 1.0,
"volatility": 0.25,
"risk_free_rate": 0.05,
"option_type": "call",
"num_simulations": 100000
}
)
result = response.json()
print(f"Option Price: ${result['option_price']:.2f}")
print(f"Black-Scholes: {result['black_scholes_price']:.2f}")
print(f"Delta: {result['greeks']['delta']:.4f}")curl -X POST http://localhost:8000/simulations \
-H "Content-Type: application/json" \
-d '{
"spot_price": 100,
"strike_price": 105,
"time_to_maturity": 1.0,
"volatility": 0.25,
"risk_free_rate": 0.05,
"option_type": "call"
}'This project is licensed under the MIT License.
- Black-Scholes-Merton model for option pricing
- FastAPI documentation and community
- Quantitative finance resources
pytest --cov=app --cov-report=html
pytest tests/test_monte_carlo.py -v
pytest -k "test_call" -v
## π³ Docker
```bash
# Build image
docker build -t optionprisma:latest .
# Run container
docker run -d -p 8000:8000 --name optionprisma optionprisma:latest
# View logs
docker logs optionprisma
# Stop and remove
docker stop optionprisma
docker rm optionprisma
optionprisma/
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI application & routes
β βββ models.py # Pydantic schemas
β βββ monte_carlo.py # Monte Carlo simulation engine
β βββ black_scholes.py # Black-Scholes analytical pricing
β βββ persistence.py # JSON CRUD operations
β βββ config.py # Configuration
βββ tests/
β βββ __init__.py
β βββ test_monte_carlo.py # Unit tests for pricing logic
β βββ test_api.py # Integration tests for endpoints
β βββ test_persistence.py # Tests for JSON operations
βββ data/
β βββ results.json # Simulation results storage
βββ Dockerfile
βββ requirements.txt
βββ .gitignore
βββ README.md
- Backend: FastAPI, Uvicorn
- Validation: Pydantic
- Computation: NumPy, SciPy
- Testing: Pytest, Hypothesis
- Containerization: Docker
- Non-blocking I/O operations
- Concurrent request handling
- Async file operations with
aiofiles
- Monte Carlo simulation using Geometric Brownian Motion
- Black-Scholes closed-form solution
- Options Greeks (sensitivity analysis)
- Clean architecture (separation of concerns)
- Type hints and validation with Pydantic
- Comprehensive testing (unit, integration)
- Error handling and HTTP status codes
- Docker containerization
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Health check |
| POST | /simulations |
Create new simulation |
| GET | /simulations |
List all simulations |
| GET | /simulations/{id} |
Get specific simulation |
| DELETE | /simulations/{id} |
Delete simulation |
Use fastapi dev for the best development experience:
- Auto-reload on code changes
- Better error messages
- Automatic configuration
Use uvicorn directly for production deployments:
- Multiple workers for concurrency
- Full configuration control
- Better performance tuning
# Production with 4 workers
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
- Black-Scholes-Merton model for option pricing
- FastAPI documentation and community
- Quantitative finance resources