0xAlpha

Last updated on 2022.6.12 @Austin

This article introduces an online algorithm of realized vol which is suitable for DeFi use-cases. In computer science, an online algorithm is one that can process its input piece-by-piece in a serial fashion, i.e., in the order that the input is fed to the algorithm, without having the entire input available from the start. The “online” property of the algorithm is critical for DeFi use-cases since there is no way to hold the entire input in DeFi scenarios.

With the booming of AMM, there have been a number of great solutions for on-chain spot trading, e.g., Uniswap. One of the most amazing benefits by AMM is that nowadays even “long-tail” tokens can easily have their marketplaces — liquidity was never so accessible. Every day, tokens worth billions of dollars are traded on these decentralized spot trading platforms. These trading activities push the prices up and down and mathematically generate volatilities for each of the trading pairs. However, there has been no on-chain mechanism to record these volatilities. Volatilities, being the mathematical measure of market risks, are the core subject of advanced finance. Without an effective on-chain mechanism to provide volatilities, it is almost a mission impossible to build advanced DeFi functionalities. This is the motivation for us to introduce the online algorithm in this paper.

The algorithms are based on the following assumptions:

  1. Black-Scholes assumptions (geometric Brownian motion, etc.) for vol calculation; (Please note this assumption is only for the purpose of calculating Black-Scholes vols — it does not mean that we think the stochastic processes of the trading prices are GBM.)
  2. Zero-drift (a common assumption for vol calculation).

Let’s start with the classical scenarios of the rolling calculation of realized vols.

The classical algorithm

First, let’s assume a price time series at time points: $t_1, t_2, ...t_s$, with a regular time interval $T$ (e.g., 1min, 5min, 1day). Such regular-interval time series are usually not from real trades but the close prices of candlestick charts.

Vol calculation will be based on the log-returns $r_i=\ln P_i-\ln P_{i-1}\approx (\frac{P_i}{P_{i-1}}-1)$.The regular MA-style calculation of vol is based on the rolling variance of last N $r_i$, which is simply the MA of $r_i^2$ under the zero-drift assumption.

$$ V_s=\frac{1}{N}\sum^s_{i=s-N+1}r_i^2 $$

And the annualized volatility is:

$$ \sigma^2=V_s\cdot\frac{1year}{T} $$

Please note this is an offline algorithm, which is not suitable for on-chain scenarios.

The online algorithm

Replacing the MA of $r_i^2$ with EMA leads to the EMA-version of vol, and makes the algorithm online.

$$ V_s=\lambda r_s^2+(1-\lambda)V_{s-1} $$

However, this algorithm only works for candlestick-style time series as it assumes a regular interval for $r_i$. It is still not good for DeFi scenarios, in which the time interval is usually irregular because prices are usually fed as triggered by external actions, in random manners.

Therefore, we need an algorithm to deal with irregular intervals.