From d151b0a7ecbda97b13abfdb5cfdc61f8b40ff0c5 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 9 Feb 2023 15:40:04 +0800 Subject: [PATCH] =?UTF-8?q?#I6CYP1=20=E5=AE=9E=E7=8E=B0=E4=BA=86LAST?= =?UTF-8?q?=E5=87=BD=E6=95=B0,=20A=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96,=20B=E4=B8=8D=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/README.md | 2 +- formula/last.go | 35 +++++++++++++++++++++++++++++++++++ formula/last_test.go | 14 ++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 formula/last.go create mode 100644 formula/last_test.go diff --git a/formula/README.md b/formula/README.md index dc1bf4e..63c49ab 100644 --- a/formula/README.md +++ b/formula/README.md @@ -30,7 +30,7 @@ formula | 0 | AVEDEV | 平均绝对差,序列与其平均值的绝对差的平均值 | AVEDEV(CLOSE,5) | [X] | [X] | | 0 | SLOPE | S序列N周期回线性回归斜率 | SLOPE(CLOSE,5) | [X] | [X] | | 0 | FORCAST | S序列N周期回线性回归后的预测值 | FORCAST(CLOSE,5) | [X] | [X] | -| 0 | LAST | 从前A日到前B日一直满足S条件,要求A>B & A>0 & B>=0 | LAST(CLOSE>REF(CLOSE,1),LOW,HIGH) | [X] | [X] | +| 0 | LAST | 从前A日到前B日一直满足S条件,要求A>B & A>0 & B>=0 | LAST(CLOSE>REF(CLOSE,1),LOW,HIGH) | [√] | [√x] | | 1 | COUNT | COUNT(CLOSE>O,N),最近N天满足S的天数True的天数 | COUNT(CLOSE>LOW,5) | [√] | [√] | | 1 | EVERY | EVERY(CLOSE>O,5),最近N天是否都是True | EVERY(CLOSE>LOW,5) | [X] | [X] | | 1 | EXIST | EXIST(CLOSE>O,5),最近N天是否都是True | EXIST(CLOSE>LOW,5) | [X] | [X] | diff --git a/formula/last.go b/formula/last.go new file mode 100644 index 0000000..62762f3 --- /dev/null +++ b/formula/last.go @@ -0,0 +1,35 @@ +package formula + +import ( + "gitee.com/quant1x/pandas" + "gitee.com/quant1x/pandas/stat" +) + +// LAST LAST(X,A,B):持续存在. +// +// A 支持序列化, B不支持 +// 例如: +// LAST(CLOSE>OPEN,10,5) +// 表示从前10日到前5日内一直阳线 +// 若A为0,表示从第一天开始,B为0,表示到最后日止 +func LAST(X pandas.Series, A, B int) pandas.Series { + s := X.Rolling(A + 1).Apply(func(S pandas.Series, N stat.DType) stat.DType { + s := S.DTypes() + s = stat.Reverse(s) + T := s[B:] + n := len(T) + for _, v := range T { + if v != 0 { + n-- + } + } + if n == 0 { + return 1 + } else { + return 0 + } + }) + d := s.Values().([]stat.DType) + stat.Fill(d, 1, true) + return s +} diff --git a/formula/last_test.go b/formula/last_test.go new file mode 100644 index 0000000..9e64bc2 --- /dev/null +++ b/formula/last_test.go @@ -0,0 +1,14 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas" + "testing" +) + +func TestLAST(t *testing.T) { + f0 := []float64{1, 2, 3, 4, 5, 6, 0, 8, 9, 10, 11, 12} + i0 := CompareGt(f0, 10) + s0 := pandas.NewSeriesWithoutType("f0", i0) + fmt.Println(LAST(s0, 10, 5)) +} -- Gitee