1 Star 0 Fork 4

王布衣/ta-lib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
peak_v1.go 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-04-10 15:30 . 调整waves用法
package linear
import "math"
// PeakDetect Finds the local maxima and minima ("peaks") in the vector
//
// Returns four arrays -> min indices, min values, max indices, max values
//
// A point is considered a maximum peak if it has the maximal
// value, and was preceded (to the left) by a value lower by DELTA.
func PeakDetect(data []float64, delta float64) ([]int, []float64, []int, []float64) {
minx := make([]int, 0, 0)
maxx := make([]int, 0, 0)
minvals := make([]float64, 0, 0)
maxvals := make([]float64, 0, 0)
xvals := make([]int, len(data), len(data))
for i := 0; i < len(data); i++ {
xvals[i] = i
}
if delta < 0 {
return minx, minvals, maxx, maxvals
}
mn := math.Inf(1)
mx := math.Inf(-1)
mnpos := int(math.NaN())
mxpos := int(math.NaN())
lookForMax := true
for i, val := range data {
if val > mx {
mx = val
mxpos = i
}
if val < mn {
mn = val
mnpos = i
}
if lookForMax {
if val < __lookahead(mx, -delta) {
maxx = append(maxx, mxpos)
maxvals = append(maxvals, mx)
mn = val
mnpos = i
lookForMax = false
}
} else {
if val > __lookahead(mn, +delta) {
minx = append(minx, mnpos)
minvals = append(minvals, mn)
mx = val
mxpos = i
lookForMax = true
}
}
}
return minx, minvals, maxx, maxvals
}
// 评估delta
func __lookahead(x, delta float64) float64 {
// 按比例
return x * (1 + delta)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quant1x/ta-lib.git
git@gitee.com:quant1x/ta-lib.git
quant1x
ta-lib
ta-lib
v0.7.11

搜索帮助