Documentation

Driftwood API Guide

Introduction

Welcome to Driftwood! This API allows you to programmatically generate thousands of stock price trajectories to model equity risk, calculate Value-at-Risk (VaR), and estimate options pricing directly in your own applications. Under the hood, Driftwood runs a Monte Carlo simulation engine powered by Geometric Brownian Motion (GBM) using historical stock drift and volatility.

Our API is entirely stateless and does not require any authentication keys. You can plug it directly into your dashboards, financial planning tools, options calculators, or research notebooks to bring interactive probability models to your users.

The Simulator Endpoint

Send a POST request to /v1/simulate to run a simulation.

Request URL:
http://localhost:8000/v1/simulate
Request Body parameters:
  • ticker (string): The stock symbol in uppercase (e.g. "AAPL").
  • days (number): How many future trading days to simulate (7 to 252 days).
  • simulations (number): How many random paths to simulate (10 to 1000 paths).

Ways to Use the API

1. In your Terminal (cURL)

Perfect for quick testing inside any terminal.

curl -X POST http://localhost:8000/v1/simulate \
  -H "Content-Type: application/json" \
  -d '{"ticker": "AAPL", "days": 30, "simulations": 100}'

2. In JavaScript / TypeScript

Easily call the API directly from your web application.

fetch("http://localhost:8000/v1/simulate", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    ticker: "AAPL",
    days: 30,
    simulations: 100
  })
})
.then(res => res.json())
.then(data => {
  console.log("Current Price:", data.current_price);
  console.log("P50 (Median) Path:", data.percentiles.p50);
});

3. In Python

Ideal for data science scripts, Google Colab notebooks, or Django/Flask backends.

import urllib.request
import json

url = "http://localhost:8000/v1/simulate"
payload = {"ticker": "AAPL", "days": 30, "simulations": 100}

req = urllib.request.Request(
    url,
    data=json.dumps(payload).encode("utf-8"),
    headers={"Content-Type": "application/json"},
    method="POST"
)

with urllib.request.urlopen(req) as response:
    data = json.loads(response.read().decode("utf-8"))
    print(f"Median final price: {data['metrics']['p50_final']}")

4. Embedding the Interactive Widget

Embed a fully interactive, live simulation chart directly into any blog, article, or site.

<iframe
  src="http://localhost:3000/?ticker=AAPL&days=30&sims=100&embed=true"
  width="100%"
  height="420"
  frameborder="0"
  style="border: 1px solid #e2e8f0;"
></iframe>

Response JSON Structure

The API returns a clean JSON object containing:

  • ticker: The ticker symbol.
  • current_price: The last recorded closing price.
  • percentiles: Arrays for the P10 (Bearish), P50 (Median), and P90 (Bullish) price paths.
  • paths: A list of 100 sample simulated raw paths for background visual rendering.
  • metrics: Summary stats including mean_final, volatility_annual, and prob_profit (probability that the price ends higher than the starting price).

Rate Limits & Client-Side Caching

To guarantee high availability and protect upstream resources, the Driftwood API enforces a rate limit of 100 requests per 5 seconds per IP address. If you exceed this rate, the API will return an HTTP 429 Too Many Requests response.

We strongly recommend caching simulation data in your own clients or backend proxy layers. Since stock trajectories rely on daily historical prices, running multiple identical simulations within the same day is redundant.

Caching Strategies

  • Local Storage (Browser): For client-only apps, cache simulation payloads in localStorage or sessionStorage using a composite key like driftwood:{ticker}:{days}:{sims}. Set an expiration of 1-4 hours.
  • Redis / Memcached (Backend): If calling Driftwood from your own server, cache the JSON payloads in a key-value store using the same composite key. We recommend a 5-minute to 1-hour Time-to-Live (TTL).
  • In-Memory Caches: For Python/Node backends, use in-memory cache helpers like Python's functools.lru_cache or Node's memory-cache to prevent redundant outgoing HTTP requests.