Files
fn_registry/functions/finance/max_drawdown.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

28 lines
674 B
Go

package finance
// MaxDrawdown calcula el maximo drawdown de una serie de valores (e.g. equity curve).
// Retorna la magnitud del drawdown (valor positivo entre 0 y 1 como fraccion del pico),
// y los indices de inicio (pico) y fin (valle) del peor drawdown.
// Si la serie tiene menos de 2 elementos, retorna 0, 0, 0.
func MaxDrawdown(values []float64) (maxDD float64, start, end int) {
n := len(values)
if n < 2 {
return 0, 0, 0
}
peakIdx := 0
peak := values[0]
for i := 1; i < n; i++ {
if values[i] > peak {
peak = values[i]
peakIdx = i
}
dd := (peak - values[i]) / peak
if dd > maxDD {
maxDD = dd
start = peakIdx
end = i
}
}
return
}