driftwood
Stateless Monte Carlo simulation API for stock price paths
The Core Problem
In equity risk management, quantitative analysts run thousands of simulated future paths to estimate risk profiles (like Value-at-Risk) or price options.
Typically, this requires heavy database architectures to sync historical data, combined with complex MATLAB or Python scripts. For developers building consumer-facing fintech portals or simple internal dashboards, setting up and running these simulation scripts on a request-by-request basis is slow and hard to scale.
The Solution: Stateless Monte Carlo API
Driftwood solves this by offering a stateless, high-performance API endpoint that executes stock trajectory risk models in milliseconds.
When a user sends a POST request with a ticker name (e.g., ticker: 'AAPL'), Driftwood:
- Fetches historical price streams on-the-fly (caching results to avoid rate limits).
- Calibrates drift ($\mu$) and historical daily volatility ($\sigma$) dynamically over the historical period.
- Generates thousands of independent trajectories using vectorized NumPy operations under a **Geometric Brownian Motion (GBM)** model.
The backend returns the exact percentile boundaries (P10, P50, P90) along with aggregate metrics (volatility, profit probability) formatted ready for frontend graphing libraries.
The Mathematical Model
The asset price $S_t$ is modeled as a stochastic process governed by Geometric Brownian Motion:
Where $dW_t$ is a Wiener process (random walk). Discretizing this over time steps $\Delta t$, the price calculation formula becomes:
Where $\epsilon$ is a random variable drawn from a standard normal distribution. By vectorizing the generation of $\epsilon$ matrices in NumPy, Driftwood runs 1,000 simulations over 90 days in under 4 milliseconds.
System Architecture & Security
Driftwood is built on **FastAPI** and packaged in lightweight **Docker** containers. To support open-source deployment, it features:
- Dual-Layer Rate Limiting — enforced at both the Nginx reverse-proxy layer and an internal FastAPI client-IP middleware to prevent resource abuse.
- In-Memory Cache — caches historical price downloads for 15 minutes to stay well within upstream data provider limits.
- Stateless Compute — No database persistence required. Scalable horizontally across container clusters.