Skip to content

duongduc388222/sigma-option

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—Ώ Sigma Option

A professional Black-Scholes option pricing application with interactive P&L analysis and advanced visualization capabilities.

Python Streamlit License

🌐 Live Demo

Website: πŸ—Ώ Sigma Option babyyyyyyy!

The application is currently available for local deployment. Web hosting coming soon!


πŸ“Š Project Overview

Sigma Option is a full-stack quantitative finance application implementing the Black-Scholes-Merton model with an emphasis on practical trading analysis. Built with modern Python tools, it offers real-time option pricing, comprehensive Greek calculations, and professional-grade visualization.

🎯 Key Differentiators

  • πŸ’° P&L-First Approach: Immediate profit/loss visualization with customizable entry prices
  • 🎨 Interactive Heatmaps: Cell-based 10Γ—10 grids with clean value annotations
  • πŸ”„ Side-by-Side Comparison: Compare Call and Put options simultaneously
  • πŸ“ˆ Professional Visualizations: Publication-quality charts with color-coded insights
  • πŸ’Ύ Historical Tracking: SQLite database for calculation persistence and analysis

✨ Features

πŸš€ Latest Implementation (Phase 1: Interactive Heatmap Redesign)

Redesigned Heatmap Visualization

  • Cell-based Display: Replaced continuous contour plots with discrete 10Γ—10 cell grids
  • Value Annotations: Each cell displays its numerical value for precise analysis
  • Smart Color Coding:
    • P&L: Green (profit) β†’ Yellow (breakeven) β†’ Red (loss)
    • Prices: Green (high) β†’ Yellow (mid) β†’ Red (low)
  • Enhanced Readability: Clean cell boundaries with optimized text contrast

Purchase Price Input & P&L Analysis

  • Entry Price Control: Dedicated input field for option purchase price
  • Auto-Population: Automatically populated with calculated fair value
  • Manual Override: Modify entry price for "what-if" scenario analysis
  • Real-time P&L: Instant profit/loss calculation: Current Value - Purchase Price

P&L-First Display Logic

  • Default P&L View: Opens directly to profit/loss analysis
  • Dual Heatmap Comparison: Side-by-side Call and Put P&L scenarios
  • Toggle Functionality: Switch between "P&L View" and "Price View" modes
  • Comprehensive Metrics: Fair value, purchase price, P&L ($), and P&L (%) displayed prominently

Enhanced Color Scheme

  • Diverging Colormap: RdYlGn palette centered at zero for P&L
  • Visual Clarity: Profit zones (green) and loss zones (red) immediately identifiable
  • Breakeven Zones: Yellow/white regions near Β±$0.50 for marginal scenarios

πŸ› οΈ Core Capabilities

Option Pricing Engine

  • βœ… European call and put option valuation
  • βœ… Real-time fair value calculation
  • βœ… Put-call parity verification
  • βœ… Edge case handling (expiration, zero volatility)

The Greeks (Sensitivity Measures)

  • Delta (Ξ”): Sensitivity to underlying price changes
  • Gamma (Ξ“): Rate of change of Delta
  • Vega (Ξ½): Sensitivity to volatility changes
  • Theta (Θ): Time decay measurement
  • Rho (ρ): Sensitivity to interest rate changes

Advanced Visualizations

  1. P&L Analysis (Primary Tab)

    • Side-by-side Call/Put P&L heatmaps
    • Toggle between P&L and Price views
    • Customizable spot and volatility ranges
  2. Price Heatmaps

    • Single option detailed analysis
    • Spot price vs. volatility sensitivity
  3. Greeks Heatmaps

    • Visualize any Greek across parameter space
    • Delta, Gamma, Vega, Theta, Rho support
  4. Payoff Diagrams

    • Classic profit/loss at expiration
    • Breakeven point identification

Database & History

  • SQLite persistence layer
  • Calculation history tracking
  • Advanced filtering by option type
  • CSV export functionality
  • Statistical summaries

πŸš€ Quick Start Guide

Prerequisites

  • Python: 3.9 or higher
  • pip: Package manager (usually bundled with Python)
  • Git: For cloning the repository

Step 1: Clone the Repository

git clone https://github.com/yourusername/blackschole_option.git
cd blackschole_option

Step 2: Set Up Virtual Environment (Recommended)

macOS/Linux:

python3 -m venv venv
source venv/bin/activate

Windows:

python -m venv venv
venv\Scripts\activate

Step 3: Install Dependencies

pip install -r requirements.txt

Required packages:

  • numpy>=1.24.0 - Numerical computations
  • scipy>=1.10.0 - Statistical functions
  • pandas>=2.0.0 - Data manipulation
  • matplotlib>=3.7.0 - Plotting
  • seaborn>=0.12.0 - Statistical visualizations
  • streamlit>=1.28.0 - Web interface
  • pytest>=7.4.0 - Testing framework

Step 4: Run on Localhost

streamlit run app/streamlit_app.py

OR if you encounter path issues:

python3 -m streamlit run app/streamlit_app.py

The application will automatically open in your default browser at:

  • Local URL: http://localhost:8501
  • Network URL: Your local IP (displayed in terminal)

Step 5: Using the Application

  1. Sidebar Inputs:

    • Select option type (Call/Put)
    • Enter purchase/entry price (auto-populated with fair value)
    • Set market parameters (S, K, T, Οƒ, r)
  2. Main Dashboard:

    • View fair value, purchase price, and P&L metrics
    • See moneyness status (ITM/ATM/OTM)
    • Check intrinsic and time value
  3. Sensitivity Analysis:

    • Navigate to "πŸ’° P&L Analysis" tab (default view)
    • Compare Call vs Put scenarios side-by-side
    • Toggle between P&L and Price views
    • Explore Greeks and payoff diagrams
  4. Database Operations:

    • Save calculations for future reference
    • View calculation history
    • Export to CSV

πŸ’Ύ SQL Database Implementation Guide

Automatic Initialization

The SQLite database is automatically created when you first run the application. No manual setup required!

Database Location: option_calculations.db (root directory)

Manual Database Setup (Optional)

If you need to manually initialize or reset the database:

# Using Python
python3 -c "from db.db_handler import get_handler; get_handler().initialize_db()"

Database Schema

Table: inputs

CREATE TABLE inputs (
    calculation_id TEXT PRIMARY KEY,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    spot_price REAL NOT NULL,
    strike_price REAL NOT NULL,
    time_to_maturity REAL NOT NULL,
    volatility REAL NOT NULL,
    risk_free_rate REAL NOT NULL,
    option_type TEXT NOT NULL CHECK(option_type IN ('call', 'put'))
);

Table: outputs

CREATE TABLE outputs (
    calculation_id TEXT PRIMARY KEY,
    option_price REAL NOT NULL,
    delta REAL NOT NULL,
    gamma REAL NOT NULL,
    vega REAL NOT NULL,
    theta REAL NOT NULL,
    rho REAL NOT NULL,
    FOREIGN KEY (calculation_id) REFERENCES inputs(calculation_id) ON DELETE CASCADE
);

Database Operations

Save a Calculation

from db.db_handler import get_handler

handler = get_handler()

inputs = {
    'spot_price': 100,
    'strike_price': 100,
    'time_to_maturity': 1.0,
    'volatility': 0.2,
    'risk_free_rate': 0.05,
    'option_type': 'call'
}

outputs = {
    'option_price': 10.45,
    'delta': 0.637,
    'gamma': 0.019,
    'vega': 0.375,
    'theta': -0.012,
    'rho': 0.532
}

calc_id = handler.save_calculation(inputs, outputs)
print(f"Saved with ID: {calc_id}")

Query History

# Get recent calculations
history = handler.get_calculation_history(limit=20)

# Filter by option type
call_history = handler.get_calculation_history(limit=50, option_type='call')

# Get as pandas DataFrame
df = handler.get_calculation_history_df(limit=100)

Database Statistics

stats = handler.get_statistics()
print(f"Total calculations: {stats['total_calculations']}")
print(f"Call options: {stats['call_count']}")
print(f"Put options: {stats['put_count']}")
print(f"Average price: ${stats['avg_option_price']:.2f}")

Database Management

View database contents (requires SQLite CLI):

sqlite3 option_calculations.db
.tables
SELECT * FROM inputs LIMIT 5;
.exit

Backup database:

cp option_calculations.db option_calculations_backup_$(date +%Y%m%d).db

Reset database (deletes all data):

rm option_calculations.db
# Database will be recreated on next app launch

πŸ“ Usage Examples

Web Interface Workflow

1. Open http://localhost:8501
2. Select "Call" option type
3. Enter parameters:
   - Spot Price: $100
   - Strike Price: $100
   - Time to Maturity: 1.0 years
   - Volatility: 20%
   - Risk-Free Rate: 5%
4. Set purchase price: $10.45 (auto-filled)
5. View results:
   - Fair Value: $10.45
   - P&L: $0.00 (0%)
6. Adjust spot price to $105 β†’ See updated P&L
7. Navigate to "P&L Analysis" tab
8. Compare Call vs Put scenarios
9. Save calculation to database

Programmatic Usage

Basic Option Pricing

from core.black_scholes import BlackScholesModel

model = BlackScholesModel()

# Price a call option
call = model.call_price(S=100, K=100, T=1.0, sigma=0.2, r=0.05)
print(f"Call: ${call:.2f}")  # Call: $10.45

# Price a put option
put = model.put_price(S=100, K=100, T=1.0, sigma=0.2, r=0.05)
print(f"Put: ${put:.2f}")    # Put: $5.57

Calculate Greeks

greeks = model.greeks(S=100, K=100, T=1.0, sigma=0.2, r=0.05, option_type='call')

for name, value in greeks.items():
    print(f"{name.capitalize()}: {value:.4f}")

Generate Heatmaps

from visuals.heatmap import OptionVisualizer

viz = OptionVisualizer()

params = {
    'spot_price': 100,
    'strike_price': 100,
    'time_to_maturity': 1.0,
    'volatility': 0.2,
    'risk_free_rate': 0.05,
    'option_type': 'call'
}

# P&L heatmap
fig = viz.plot_pnl_heatmap(
    entry_price=10.45,
    base_params=params,
    show_percentage=False
)
fig.savefig('pnl_analysis.png', dpi=300, bbox_inches='tight')

πŸ—‚οΈ Project Structure

blackschole_option/
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── black_scholes.py          # Pricing engine & Greeks
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── streamlit_app.py          # Web interface (Sigma Option UI)
β”œβ”€β”€ visuals/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── heatmap.py                # Heatmap visualizations (10Γ—10 grids)
β”œβ”€β”€ db/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ schema.sql                # SQLite schema
β”‚   └── db_handler.py             # Database operations
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_black_scholes.py     # 41 unit tests
β”‚   └── test_db_handler.py        # 36 unit tests
β”œβ”€β”€ requirements.txt               # Python dependencies
β”œβ”€β”€ milestones.md                  # Implementation roadmap
β”œβ”€β”€ README.md                      # This file
└── option_calculations.db         # SQLite database (auto-created)

πŸ§ͺ Testing

Run the complete test suite:

# All tests
pytest

# With coverage report
pytest --cov=core --cov=db --cov=visuals tests/

# Verbose output
pytest -v

# Specific test file
pytest tests/test_black_scholes.py

Test Coverage Summary:

  • Total Tests: 77 passing
  • Black-Scholes Model: 41 tests (pricing, Greeks, edge cases, validation)
  • Database Handler: 36 tests (CRUD, constraints, queries, statistics)

πŸ“ Mathematical Background

Black-Scholes Formula

Call Option:

C = Sβ‚€N(d₁) - Ke^(-rT)N(dβ‚‚)

Put Option:

P = Ke^(-rT)N(-dβ‚‚) - Sβ‚€N(-d₁)

Where:

d₁ = [ln(Sβ‚€/K) + (r + σ²/2)T] / (ΟƒβˆšT)
dβ‚‚ = d₁ - ΟƒβˆšT
  • Sβ‚€: Current spot price
  • K: Strike price
  • T: Time to maturity (years)
  • Οƒ: Volatility (annualized)
  • r: Risk-free rate (annualized)
  • N(x): Cumulative standard normal distribution

Model Assumptions

  1. European-style exercise only
  2. No dividends during option life
  3. Constant volatility and risk-free rate
  4. Log-normal distribution of stock prices
  5. Frictionless markets (no transaction costs/taxes)
  6. Continuous trading possible
  7. Unlimited borrowing/lending at risk-free rate

πŸ› οΈ Technical Stack

Category Technology
Core Language Python 3.9+
Scientific Computing NumPy, SciPy
Data Analysis Pandas
Visualization Matplotlib, Seaborn
Web Framework Streamlit
Database SQLite3
Testing pytest, pytest-cov
Version Control Git

🚧 Roadmap & Future Enhancements

Phase 2: Advanced Features (Planned)

  • Real-time market data integration (Yahoo Finance API)
  • Implied volatility surface visualization
  • Historical volatility calculation
  • Multi-leg option strategies (spreads, straddles, strangles)

Phase 3: Infrastructure (Planned)

  • Docker containerization
  • REST API (FastAPI)
  • Cloud deployment (AWS/Heroku/Railway)
  • User authentication system
  • WebSocket for real-time updates

Phase 4: Advanced Models (Future)

  • American option pricing (Binomial tree)
  • Monte Carlo simulation engine
  • Exotic options (Asian, Barrier, Lookback)
  • Volatility smile/skew modeling

🌐 Deployment

Coming Soon: Web Hosting

The application will be deployed to a public URL for easy access without local installation. Planned hosting options:

  • Streamlit Cloud (Primary)
  • Heroku (Alternative)
  • Railway (Alternative)

Current Status: Local deployment only Expected Launch: [TBD]

Check back soon for the live demo link!


πŸ“œ License

This project is licensed under the MIT License. See LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Duong Hong Duc Grinnell College

This project demonstrates:

  • Quantitative finance modeling
  • Full-stack development skills
  • Data visualization expertise
  • Software engineering best practices
  • Database design and management

πŸ™ Acknowledgments

  • Hull, J. C. (2017). Options, Futures, and Other Derivatives
  • Black, F., & Scholes, M. (1973). The Pricing of Options and Corporate Liabilities
  • Merton, R. C. (1973). Theory of Rational Option Pricing

πŸ“š References

  1. Black-Scholes Model - Wikipedia
  2. Option Greeks - Investopedia
  3. Put-Call Parity - CFI
  4. Streamlit Documentation

πŸ—Ώ Sigma Option
Professional Black-Scholes option pricing with interactive P&L analysis
Built with ❀️ for quantitative finance enthusiasts

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages