Files
fn_registry/functions/finance/annualized_volatility.go
T
egutierrez 113c6dfd71 feat: 15 funciones finance — indicadores, riesgo e IO de mercado
11 funciones puras con implementación real:
SMA, EMA, RSI, BollingerBands, VWAP, LogReturn, AnnualizedVolatility,
SharpeRatio, MaxDrawdown, NormalizeOHLCV, TickToOHLCV

4 funciones impuras (stubs):
FetchOHLCV, StreamTicks, WriteOHLCVToParquet, LoadOHLCVFromDuckDB

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 02:23:31 +01:00

26 lines
662 B
Go

package finance
import "math"
// AnnualizedVolatility calcula la volatilidad anualizada a partir de una serie de retornos.
// periodsPerYear indica cuantos periodos hay en un anio (e.g. 252 para retornos diarios).
// Retorna stddev(returns) * sqrt(periodsPerYear).
func AnnualizedVolatility(returns []float64, periodsPerYear float64) float64 {
n := len(returns)
if n < 2 {
return 0
}
var sum float64
for _, r := range returns {
sum += r
}
mean := sum / float64(n)
var variance float64
for _, r := range returns {
diff := r - mean
variance += diff * diff
}
variance /= float64(n - 1)
return math.Sqrt(variance) * math.Sqrt(periodsPerYear)
}