A comprehensive option pricing application built with Streamlit that implements multiple pricing models including Black-Scholes, Crank-Nicolson, FOBSM, and Neural Networks.
- European Options Pricing: Black-Scholes model with Monte Carlo simulations
- American Options Pricing: Crank-Nicolson and FOBSM numerical methods
- Neural Network Pricing: Deep learning approach for option valuation
- 3D Visualizations: Interactive surface plots and heatmaps
- Real-time Data: Integration with Alpha Vantage API for live stock data
- Sensitivity Analysis: Greeks calculation and parameter sensitivity
- Clone this repository
- Install the required dependencies:
pip install -r requirements.txt
- Set up your Alpha Vantage API key in Streamlit secrets (optional for demo mode)
Run the Streamlit application:
streamlit run app.py├── app.py # Main Streamlit application
├── pricing_models.py # Option pricing implementations
├── neural_networks.py # Neural network models and training
├── visualizations.py # Plotting and heatmap functions
├── surface_plots.py # 3D surface plotting utilities
├── DataFetcher.py # API data fetching functions
├── utils.py # Utility functions and session management
├── requirements.txt # Python dependencies
└── README.md # This file
The Black-Scholes model assumes that stock prices follow a geometric Brownian motion:
dS = μSdt + σSdW
Where:
S= stock priceμ= drift rate (expected return)σ= volatilitydW= Wiener process (random walk)
The option price V(S,t) satisfies:
∂V/∂t + (1/2)σ²S²(∂²V/∂S²) + rS(∂V/∂S) - rV = 0
Where:
V(S,t)= option value as function of stock price S and time tr= risk-free rateσ= volatility
European Call Option:
C = S₀N(d₁) - Ke^(-rT)N(d₂)
European Put Option:
P = Ke^(-rT)N(-d₂) - S₀N(-d₁)
Where:
d₁ = [ln(S₀/K) + (r + σ²/2)T] / (σ√T)
d₂ = d₁ - σ√T
S₀= current stock priceK= strike priceT= time to expirationN(x)= cumulative standard normal distributione= mathematical constant (≈2.71828)ln= natural logarithm
Delta (Δ): Price sensitivity to underlying asset price
Δ_call = N(d₁)
Δ_put = N(d₁) - 1
Gamma (Γ): Rate of change of delta
Γ = φ(d₁) / (S₀σ√T)
Theta (Θ): Time decay
Θ_call = -[S₀φ(d₁)σ/(2√T) + rKe^(-rT)N(d₂)]
Vega (ν): Sensitivity to volatility
ν = S₀φ(d₁)√T
Rho (ρ): Sensitivity to interest rate
ρ_call = KTe^(-rT)N(d₂)
ρ_put = -KTe^(-rT)N(-d₂)
Where φ(x) is the standard normal probability density function.
For American options, we solve the Black-Scholes PDE with early exercise constraints using finite differences.
Transform the PDE into a system of linear equations:
-aⱼVⱼ₋₁^(n+1) + (1+bⱼ)Vⱼ^(n+1) - cⱼVⱼ₊₁^(n+1) = aⱼVⱼ₋₁^n + (1-bⱼ)Vⱼ^n + cⱼVⱼ₊₁^n
Where:
aⱼ = (1/4)σ²j²Δt - (1/4)rjΔt
bⱼ = (1/2)σ²j²Δt + (1/2)rΔt
cⱼ = (1/4)σ²j²Δt + (1/4)rjΔt
j= spatial grid indexn= time step indexΔt= time step size
Call Option:
V(0,t) = 0(worthless if S=0)V(S_max,t) = S_max - Ke^(-r(T-t))(deep in-the-money)V(S,T) = max(S-K, 0)(payoff at expiration)
Put Option:
V(0,t) = Ke^(-r(T-t))(maximum value if S=0)V(S_max,t) = 0(worthless for large S)V(S,T) = max(K-S, 0)(payoff at expiration)
For American options, at each time step:
V(S,t) ≥ max(S-K, 0) [for calls]
V(S,t) ≥ max(K-S, 0) [for puts]
The Fractional Order Black-Scholes Model (FOBSM) extends the classical Black-Scholes framework by incorporating fractional calculus to model memory effects and long-range dependencies in financial markets. This implementation is inspired by the research presented in this paper.
The FOBSM replaces the standard second derivative with a fractional derivative of order α (0 < α ≤ 1):
∂V/∂t + (1/2)σ²S²(∂^α V/∂S^α) + rS(∂V/∂S) - rV = 0
Where:
V(S,t)= option value as function of stock price S and time t∂^α V/∂S^α= Caputo fractional derivative of order αα= fractional order parameter (0 < α ≤ 1)- All other parameters are the same as in classical Black-Scholes
The Caputo fractional derivative of order α for a function f(x) is defined as:
D^α f(x) = (1/Γ(n-α)) ∫₀^x f^(n)(ξ)/(x-ξ)^(α-n+1) dξ
Where:
Γ(x)= Gamma function: Γ(x) = ∫₀^∞ t^(x-1)e^(-t) dtn = ⌈α⌉= ceiling of α (smallest integer ≥ α)f^(n)(ξ)= nth derivative of f at point ξ
For the specific case in FOBSM (0 < α ≤ 1), this simplifies to:
∂^α V/∂S^α = (1/Γ(1-α)) ∫₀^S (∂V/∂ξ)/((S-ξ)^α) dξ
The fractional derivative is approximated using the Grünwald-Letnikov formula:
∂^α V/∂S^α ≈ h^(-α) Σₖ₌₀^n (-1)^k (α choose k) V(S-kh)
Where:
h= grid spacingn= number of grid points(α choose k)= generalized binomial coefficient:
(α choose k) = Γ(α+1)/(Γ(k+1)Γ(α-k+1))
| Parameter | Classical BS | FOBSM |
|---|---|---|
| Derivative Order | α = 1 (integer) | 0 < α ≤ 1 (fractional) |
| Market Memory | No memory | Memory effects |
| Price Evolution | Markovian | Non-Markovian |
| Mathematical Tool | Standard calculus | Fractional calculus |
- α = 1: Reduces to classical Black-Scholes (no memory)
- α < 1: Incorporates memory effects in market behavior
- Lower α values: More pronounced memory effects and long-range dependencies
- Market implications: Captures phenomena like volatility clustering, long-term correlations, and non-Gaussian price distributions
- Memory Effects: Models how past price movements influence current dynamics
- Market Anomalies: Better captures real market behavior than classical models
- Flexibility: Parameter α allows tuning for different market conditions
- Mathematical Rigor: Based on well-established fractional calculus theory
Note: This FOBSM implementation draws inspiration from the fractional calculus approach to option pricing as described in the referenced research paper.
The neural network implements a feedforward architecture:
Input Layer (5 neurons): [S, K, T, r, σ]
Hidden Layer 1 (64 neurons): ReLU activation
Hidden Layer 2 (32 neurons): ReLU activation
Output Layer (1 neuron): Option price
For a neural network with layers l = 1, 2, ..., L:
z^[l] = W^[l]a^[l-1] + b^[l]
a^[l] = g^[l](z^[l])
Where:
W^[l]= weight matrix for layer lb^[l]= bias vector for layer lg^[l]= activation function for layer la^[l]= activations for layer l
Mean Squared Error between predicted and theoretical prices:
L = (1/m) Σᵢ₌₁^m (ŷᵢ - yᵢ)²
Where:
m= number of training samplesŷᵢ= predicted option priceyᵢ= theoretical option price (from Black-Scholes)
Gradients computed using chain rule:
∂L/∂W^[l] = (1/m) ∂L/∂a^[L] × ∂a^[L]/∂z^[L] × ... × ∂z^[l]/∂W^[l]
Adam optimizer with learning rate scheduling:
mₜ = β₁mₜ₋₁ + (1-β₁)gₜ
vₜ = β₂vₜ₋₁ + (1-β₂)gₜ²
θₜ₊₁ = θₜ - α(m̂ₜ/(√v̂ₜ + ε))
Where:
gₜ= gradient at time tβ₁, β₂= exponential decay ratesα= learning rateε= small constant for numerical stability
Stock price evolution simulated as:
S(t+Δt) = S(t)exp((r - σ²/2)Δt + σ√Δt × Z)
Where Z ~ N(0,1) is a standard normal random variable.
For each random draw Z, also simulate with -Z to reduce variance:
S₁ = S₀exp((r - σ²/2)T + σ√T × Z)
S₂ = S₀exp((r - σ²/2)T + σ√T × (-Z))
Using Central Limit Theorem, 95% confidence interval:
CI = μ̂ ± 1.96 × (σ̂/√n)
Where:
μ̂= sample mean of option payoffsσ̂= sample standard deviationn= number of simulations
| Model | Type | Advantages | Use Case |
|---|---|---|---|
| Black-Scholes | Analytical | Fast, exact solution | European options |
| Crank-Nicolson | Numerical | Handles early exercise | American options |
| FOBSM | Fractional | Memory effects | Enhanced modeling |
| Neural Network | ML | Flexible, adaptive | Pattern recognition |
For real-time data fetching, you'll need an Alpha Vantage API key:
- Get a free key from Alpha Vantage
- Add it to
.streamlit/secrets.toml:[alphavantage] api_key = "your_api_key_here"
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This application is for educational and research purposes only. It should not be used for actual trading decisions without proper validation and risk management.