floats: port some functions from ta-lib

see https://github.com/markcheno/go-talib/blob/master/talib.go
This commit is contained in:
c9s 2022-08-26 16:25:31 +08:00
parent 52d245ecf1
commit 9a0988db35
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -65,6 +65,14 @@ func Average(arr []float64) float64 {
return s / float64(len(arr))
}
// Multiply multiplies two float series
func Multiply(inReal0 []float64, inReal1 []float64) []float64 {
outReal := make([]float64, len(inReal0))
for i := 0; i < len(inReal0); i++ {
outReal[i] = inReal0[i] * inReal1[i]
}
return outReal
}
// CrossOver returns true if series1 is crossing over series2.
//
@ -94,3 +102,61 @@ func CrossUnder(series1 []float64, series2 []float64) bool {
return series1[N-1] <= series2[N-1] && series1[N-2] > series2[N-2]
}
// MinMax - Lowest and highest values over a specified period
func MinMax(inReal []float64, inTimePeriod int) (outMin []float64, outMax []float64) {
outMin = make([]float64, len(inReal))
outMax = make([]float64, len(inReal))
nbInitialElementNeeded := inTimePeriod - 1
startIdx := nbInitialElementNeeded
outIdx := startIdx
today := startIdx
trailingIdx := startIdx - nbInitialElementNeeded
highestIdx := -1
highest := 0.0
lowestIdx := -1
lowest := 0.0
for today < len(inReal) {
tmpLow, tmpHigh := inReal[today], inReal[today]
if highestIdx < trailingIdx {
highestIdx = trailingIdx
highest = inReal[highestIdx]
i := highestIdx
i++
for i <= today {
tmpHigh = inReal[i]
if tmpHigh > highest {
highestIdx = i
highest = tmpHigh
}
i++
}
} else if tmpHigh >= highest {
highestIdx = today
highest = tmpHigh
}
if lowestIdx < trailingIdx {
lowestIdx = trailingIdx
lowest = inReal[lowestIdx]
i := lowestIdx
i++
for i <= today {
tmpLow = inReal[i]
if tmpLow < lowest {
lowestIdx = i
lowest = tmpLow
}
i++
}
} else if tmpLow <= lowest {
lowestIdx = today
lowest = tmpLow
}
outMax[outIdx] = highest
outMin[outIdx] = lowest
outIdx++
trailingIdx++
today++
}
return outMin, outMax
}