The exact empirical convention
Enter periodic returns as percentages. Convert each return r into a loss L = −r and sort losses in ascending order. For confidence c and sample size N, VaR is the observation at zero-based index ceil(c × N) − 1. Expected shortfall averages the largest k = ceil((1 − c) × N) losses. Tail membership uses this count independently of ties at VaR, so repeated threshold values do not silently expand the tail.
A five-observation example
Suppose returns are −8%, −3%, 1%, 2%, and 4%. Ordered losses are −4%, −2%, −1%, 3%, and 8%. At 80% confidence, nearest-rank VaR is the fourth loss, or 3%. The tail count is one, so expected shortfall is 8%. At 60% confidence, the tail contains two losses and their average is 5.5%. These small examples illustrate the convention rather than a statistically reliable risk estimate.
What a negative loss estimate means
If every supplied return is positive, VaR and expected shortfall can both be negative: even the worst sampled outcomes were gains. The tool preserves that information instead of replacing it with zero. Results retain the input period and are not automatically scaled to a different horizon. A short or selectively chosen history can omit exchange failures, liquidity shocks, and changing exposures. This implementation is a descriptive empirical estimator, not a Basel capital calculation.