Local Volatility
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5
indicator('Local Volatility')
//The traditional calculation of volatility involves computing the standard deviation of returns,
// which is based on the mean return. However, when the asset price exhibits a trending behavior,
// the mean return could be significantly different from zero, and changing the length of the time
// window used for the calculation could result in artificially high volatility values. This is because
// more returns would be further away from the mean, leading to a larger sum of squared deviations.
// To address this issue, our Local Volatility measure computes the standard deviation of the
// differences between consecutive asset prices, rather than their returns. This provides a measure of
// how much the price changes from one tick to the next, irrespective of the overall trend.
// ref: https://arxiv.org/abs/2308.14235

int length_01 = input(10)
int length_02 = input(15)
int length_03 = input(50)
slv (src, t) => math.sqrt(ta.sma(math.pow(src - src[1], 2.0), t))
clv (src) => math.sqrt(ta.cum(math.pow(src - src[1], 2.0) / (bar_index + 1)))

plot(slv(close, length_01), 'short therm', #ff0000)
plot(slv(close, length_02), 'short therm', #ff4800)
plot(slv(close, length_03), 'short therm', #ff9900)
plot(clv(close), 'long therm', #0080ff)
