diff --git a/formula/hhvbars.go b/formula/hhvbars.go new file mode 100644 index 0000000000000000000000000000000000000000..c27f4fa15ac2662747d3339547f7341c87fc4c7e --- /dev/null +++ b/formula/hhvbars.go @@ -0,0 +1,16 @@ +package formula + +import "gitee.com/quant1x/pandas/stat" + +// HHVBARS 求上一高点到当前的周期数. +// +// 用法: +// HHVBARS(X,N):求N周期内X最高值到当前周期数,N=0表示从第一个有效值开始统计 +// 例如: +// HHVBARS(HIGH,0)求得历史新高到到当前的周期数 +func HHVBARS(S stat.Series, N any) stat.Series { + x := S.Rolling(N).Apply(func(X stat.Series, W stat.DType) stat.DType { + return stat.Any2DType(X.Reverse().ArgMax()) + }) + return x +} diff --git a/formula/hhvbars_test.go b/formula/hhvbars_test.go new file mode 100644 index 0000000000000000000000000000000000000000..9d949d85bf2d4c4a31bd35cdce85b5adbc127972 --- /dev/null +++ b/formula/hhvbars_test.go @@ -0,0 +1,13 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas/stat" + "testing" +) + +func TestHHVBARS(t *testing.T) { + n1 := []float32{1.1, 2.2, 1.3, 1.4} + s1 := stat.NewSeries[float32](n1...) + fmt.Println(HHVBARS(s1, 2)) +}