113c6dfd71
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>
26 lines
662 B
Go
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)
|
|
}
|