What This System Does
An end-to-end pipeline from raw price data to actionable risk figures — running entirely on the edge.
The Global Market Tracker is a real-time financial analytics platform that fetches daily closing prices for major global equity indices, computes logarithmic returns, handles gaps in the data, and provides a Monte Carlo simulation-based Value at Risk (VaR) engine — all served through a serverless Cloudflare Worker architecture.
The platform is designed around three analytical modules: Market Trends (visualise prices and returns across any timeframe), Risk / VaR (quantify tail risk for a custom portfolio exposure), and a Data Export facility that lets researchers download raw CSVs directly.
All data is served from a Cloudflare Worker endpoint at workers.dev. The Worker fetches from Yahoo Finance on a scheduled cron and caches results in Cloudflare KV — meaning the dashboard always loads in milliseconds with no cold-start penalty.
Fetching from Yahoo Finance
How raw market data is acquired, normalised, and stored.
Yahoo Finance is used as the primary data provider due to its comprehensive coverage of global equity indices, zero-cost API access, and daily-updated closing prices. Data is fetched programmatically using the yfinance library inside a Cloudflare Worker's scheduled handler.
^NSEI for the NIFTY 50, ^GSPC for the S&P 500, ^DJI for the Dow Jones, and so on. The worker maintains a curated dictionary mapping human-readable market names to their respective tickers./api/prices and /api/returns — which the dashboard fetches on load.| Endpoint | Response Shape | Purpose |
|---|---|---|
| /api/prices | Array of {Date, Market₁, Market₂, …} | Daily closing price for each tracked index |
| /api/returns | Array of {Date, Market₁, Market₂, …} | Pre-computed log-returns (%) with forward-fill applied |
| /api/admin/refresh | {status, message} | Force pipeline run (admin-authenticated POST) |
The dashboard calculates the difference between today's date and the last available record date. If data is 0–4 days old it shows a green "live" badge; anything older triggers an amber "stale" warning, alerting the user that the cron job may have missed a run.
Logarithmic (Dlog) Returns
Why log-returns and how they are computed in percentage terms.
Simple arithmetic returns measure the proportional change in price, but they are not time-additive — you cannot sum daily returns to obtain the multi-period return. Logarithmic returns (also called continuously-compounded or dlog returns) solve this: they are additive across time, approximately normally distributed, and symmetric with respect to gains and losses.
Pt = adjusted closing price on day t
Pt−1 = adjusted closing price on the previous available trading day
ln(·) = natural logarithm
The multiplication by 100 converts the unitless log-ratio into a percentage figure — the value stored in /api/returns and displayed on the dashboard. When the VaR engine reads these returns it divides by 100 to convert back to decimal form before computing statistics.
Simple returns r = (Pt − Pt−1) / Pt−1 are asymmetric: a 50% gain followed by a 50% loss does not return to the origin. Log-returns have no such asymmetry, making them the standard choice in quantitative finance for volatility modelling and VaR estimation.
Forward-Fill in Return Calculation
Markets close on different holidays. Here is how gaps are bridged without distorting the return series.
Because global equity markets observe different national holidays, it is common for one index to have a trading day where another does not. When the unified date-indexed table is constructed (§02), these gaps appear as null or NaN values in the price columns.
The rule applied in this system is: when a price is missing on day t, the most recently available price is carried forward (last-observation-carried-forward, or LOCF). The consequence for the return series is that the computed log-return for that gap day is exactly 0% — the market is treated as if it did not move on its closed day, which is economically the most neutral assumption.
∴ rt = ln(Pt−1 / Pt−1) × 100 = 0%
The resulting return is identically zero — no phantom gain or loss is introduced.
The diagram below illustrates the process. Gold bars indicate days where the forward-fill was applied; teal bars are real trading days:
Linear interpolation would invent prices that never existed, distorting volatility. Deleting rows would misalign cross-market comparisons. LOCF is the industry-standard approach: it preserves the time-series length, keeps all markets aligned on the same date spine, and introduces a return of exactly 0% — the correct value for a day on which no trading occurred.
In the VaR calculation, after the forward-filled return series is loaded, an additional filter removes any remaining NaN or non-numeric entries before statistics are computed:
Monte Carlo VaR Engine
From a clean return series to a probabilistic loss estimate — in three mathematical steps.
Value at Risk (VaR) answers a single question: "What is the maximum amount I could lose over a given period, with a specified level of confidence?" The dashboard implements a parametric Monte Carlo approach — estimating drift and volatility from historical data, then simulating thousands of future paths to build an empirical return distribution.
Step 1 — Estimate Drift & Volatility
From the filtered historical return series, two statistics are computed:
σ = stddev(r₁, …, rₙ) × √h
n = number of historical observations (controlled by lookback window)
The square-root-of-time rule scales daily volatility to the chosen horizon under the assumption of i.i.d. returns.
Step 2 — Simulate Random Paths
Using the estimated μ and σ, the engine draws N random returns from a normal distribution using the Box-Muller transform — the industry-standard method for generating Gaussian random numbers in JavaScript:
Z = a single simulated horizon return
Repeated N times (default 10,000; up to 250,000) to build the distribution.
Step 3 — Extract the VaR Quantile
The sorted simulation array is indexed at the (1 − confidence level) percentile. The return at that index is the VaR threshold — the worst expected outcome at the chosen confidence:
VaR₹ = Portfolio × |VaR%|
N = number of simulated paths
|·| = absolute value — VaR is always expressed as a positive loss figure
The colour of the result turns amber above 5% loss and red above 10%.
How It All Connects
The full stack from data source to user interface.
Because the compute layer is a Cloudflare Worker and the frontend is hosted on Cloudflare Pages, there is no traditional server to maintain. Cold-start times are sub-millisecond, global edge nodes serve the dashboard from the nearest PoP, and the VaR Monte Carlo runs in the user's own browser — zero backend cost at request time.
| Parameter | Default | Range |
|---|---|---|
| Lookback Window | 1 Year (252 days) | 1 Month → All History |
| Confidence Level | 95% | 90%, 95%, 99% |
| Time Horizon | 1 Day | 1 Day → 1 Year |
| Simulation Paths | 10,000 | 1,000 → 250,000 |
| Min. Data Points for VaR | 30 | Hard minimum enforced |
| Histogram Bins | 60 | Fixed |