Documentation Index
Fetch the complete documentation index at: https://mintlify.com/joicodev/polymarket-bot/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The EWMA (Exponentially Weighted Moving Average) volatility estimator produces a real-time, per-second volatility estimate from streaming price ticks. It is the core input to the Black-Scholes probability model.Mathematical Foundation
Volatility Definition
Volatility (σ) is the standard deviation of log returns:EWMA Variance Recursion
The EWMA estimator updates variance incrementally with each new tick: Where:- λ = decay factor ∈ (0, 1) (default: 0.94)
- r_t = log return between consecutive ticks
- Δt = time delta between ticks (in seconds)
- r_t² / Δt = normalized squared return (variance per second)
Decay Factor λ = 0.94: This gives approximately 94% weight to the previous variance and 6% weight to the new observation. Higher λ → smoother, slower adaptation. Lower λ → more reactive, noisier.
Per-Second Normalization
Crucial detail: we divide r² by Δt to express variance in per-second units: This ensures σ is directly compatible with the Black-Scholes formula, which expects time in seconds.Implementation
Constructor
volatility.js
Update Algorithm
volatility.js
Getters
volatility.js
getMeanSigma(): Used by the anomalous regime detector. If current volatility exceeds 3× the mean of the last 100 values, the engine abstains from predicting.
Edge Cases
1. First Tick
On the first tick, no return can be computed. The estimator stores the price and returns 0:2. Second Tick (Initialization)
The first return seeds the variance:3. Zero Time Delta
If two ticks share the same timestamp (or arrive out of order), dt is clamped to 1ms:Intuition
Why Exponential Weighting?
Simple moving average (SMA) gives equal weight to all observations in the window: EWMA gives exponentially decaying weight to older observations: Benefits:- Memory efficiency: O(1) state (no tick buffer required)
- Recency bias: Recent volatility matters more than ancient history
- Continuous adaptation: No window boundary effects
Effective Window Length
The “effective” lookback period for λ = 0.94: This means the estimator “forgets” 95% of a tick’s influence after ~32 subsequent ticks.Usage Example
Integration with Engine
The prediction engine feeds each tick into the volatility estimator:predictor.js
predictor.js
Time Scale Conversion
Why Per-Second?
The Black-Scholes formula expects time remaining in seconds and volatility in matching units: If T is in seconds, σ must be per-second volatility.Alternative Time Units
If you prefer annualized volatility (common in finance): Where 31,536,000 = seconds per year. For hourly volatility:Calibration
Choosing λ
The decay factor controls the trade-off between stability and responsiveness:| λ | Effective Window | Behavior |
|---|---|---|
| 0.90 | ~19 ticks | Very reactive, noisy |
| 0.94 | ~32 ticks | Default — balanced |
| 0.97 | ~65 ticks | Smooth, slow adaptation |
| 0.99 | ~199 ticks | Very stable, insensitive to shocks |
Empirical Tuning: λ = 0.94 was selected through backtesting on Polymarket tick data. For markets with different microstructure (e.g., high-frequency vs. low-frequency ticks), you may need to re-calibrate.
Warm-Up Period
The estimator requires a minimum number of ticks before producing reliable estimates. The engine uses:State Management
Reset
volatility.js
When to reset: Only reset volatility on market close or context switch. The engine’s
resetMomentum() method preserves volatility state across interval boundaries, allowing the EWMA to accumulate long-term regime information.Advanced Topics
GARCH vs. EWMA
GARCH (Generalized Autoregressive Conditional Heteroskedasticity) is a more sophisticated volatility model: EWMA is a special case of GARCH(1,1) with:- ω = 0 (no long-term mean reversion)
- α = 1 - λ
- β = λ
RiskMetrics™ Standard
The λ = 0.94 default comes from J.P. Morgan’s RiskMetrics™ methodology (1996), which popularized EWMA for daily returns. For intraday tick data, the optimal λ may differ.Realized Volatility
An alternative to EWMA is realized volatility (sum of squared returns over a fixed window): This is unbiased but requires storing all returns in the window (O(N) memory vs. O(1) for EWMA).Performance Characteristics
- Time Complexity: O(1) per update
- Space Complexity: O(1) core state + O(100) history buffer
- Numerical Stability: Log returns prevent overflow; variance is always non-negative
References
- J.P. Morgan (1996). RiskMetrics™ Technical Document (4th ed.).
- Tsay, R. S. (2010). Analysis of Financial Time Series (3rd ed.). Wiley.
- Engle, R. F. (1982). “Autoregressive Conditional Heteroscedasticity with Estimates of the Variance of United Kingdom Inflation.” Econometrica, 50(4), 987-1007.
Next Steps
Black-Scholes Model
How volatility feeds into the probability calculation
Prediction Engine
Abstention logic using volatility thresholds