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>
28 lines
674 B
Go
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
|
|
}
|