From be90928692a8ea2531026800261fedb312dd355f Mon Sep 17 00:00:00 2001 From: wangfeng Date: Fri, 10 Feb 2023 11:53:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AE=80=E5=8C=96=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/avedev_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/formula/avedev_test.go b/formula/avedev_test.go index 4959cad..532b3ab 100644 --- a/formula/avedev_test.go +++ b/formula/avedev_test.go @@ -7,9 +7,7 @@ import ( ) func TestAVEDEV(t *testing.T) { - x := []float64{0.0, 0.1, 0.2, 0.3, 0.5, 0.8, 1.0} y := []float64{1.0, 0.41, 0.50, 0.61, 0.91, 2.02, 2.46} - X := pandas.NewSeriesWithoutType("x", x) Y := pandas.NewSeriesWithoutType("y", y) fmt.Println(AVEDEV(Y, 5)) @@ -25,6 +23,4 @@ func TestAVEDEV(t *testing.T) { C := pandas.NewSeriesWithoutType("c1", c1) df = pandas.NewDataFrame(C) fmt.Println(df) - - _ = X } -- Gitee From b29788ef30937b57f3473a68b1931546fd5eb7d3 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Fri, 10 Feb 2023 11:57:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?#I6CYOZ=20=E5=AE=9E=E7=8E=B0FORCAST?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/README.md | 2 +- formula/forcast.go | 23 +++++++++++++++++++++++ formula/forcast_test.go | 28 ++++++++++++++++++++++++++++ stat/polynomial_test.go | 9 +++++++-- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 formula/forcast.go create mode 100644 formula/forcast_test.go diff --git a/formula/README.md b/formula/README.md index b1e72bb..bb2c3ff 100644 --- a/formula/README.md +++ b/formula/README.md @@ -29,7 +29,7 @@ formula | 0 | CONST | 返回序列S最后的值组成常量序列 | CONST(CLOSE) | [√] | [ ] | | 0 | AVEDEV | 平均绝对差,序列与其平均值的绝对差的平均值 | AVEDEV(CLOSE,5) | [√] | [√] | | 0 | SLOPE | S序列N周期回线性回归斜率 | SLOPE(CLOSE,5) | [√] | [√] | -| 0 | FORCAST | S序列N周期回线性回归后的预测值 | FORCAST(CLOSE,5) | [X] | [X] | +| 0 | FORCAST | S序列N周期回线性回归后的预测值 | FORCAST(CLOSE,5) | [√] | [√] | | 0 | LAST | 从前A日到前B日一直满足S条件,要求A>B & A>0 & B>=0 | LAST(CLOSE>REF(CLOSE,1),LOW,HIGH) | [√] | [√] | | 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] | diff --git a/formula/forcast.go b/formula/forcast.go new file mode 100644 index 0000000..2153272 --- /dev/null +++ b/formula/forcast.go @@ -0,0 +1,23 @@ +package formula + +import ( + "gitee.com/quant1x/pandas" + "gitee.com/quant1x/pandas/stat" +) + +// FORCAST 返回S序列N周期回线性回归后的预测值 +func FORCAST(S pandas.Series, N any) any { + return S.Rolling(N).Apply(func(X pandas.Series, W stat.DType) stat.DType { + x := X.DTypes() + ws := stat.Sequence[float64](int(W)) + c := stat.PolyFit(ws, x, 1) + w1 := stat.Repeat[float64](float64(W-1), int(W)) + vs := stat.PolyVal(c, w1) + + if W > 1 { + return vs[0] + } else { + return stat.DTypeNaN + } + }).Values() +} diff --git a/formula/forcast_test.go b/formula/forcast_test.go new file mode 100644 index 0000000..2276211 --- /dev/null +++ b/formula/forcast_test.go @@ -0,0 +1,28 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas" + "testing" +) + +func TestFORCAST(t *testing.T) { + x := []float64{1.0, 0.41, 0.50, 0.61, 0.91, 2.02, 2.46} + X := pandas.NewSeriesWithoutType("x", x) + fmt.Println(FORCAST(X, 5)) + + csv := "~/.quant1x/data/cn/002528.csv" + df := pandas.ReadCSV(csv) + df.SetNames("data", "open", "close", "high", "low", "volume", "amount", "zf", "zdf", "zde", "hsl") + fmt.Println(df) + fmt.Println(df.Names()) + df.SetName("收盘", "close") + CLOSE := df.Col("close") + + c1 := FORCAST(CLOSE, 5) + C := pandas.NewSeriesWithoutType("c1", c1) + df = pandas.NewDataFrame(C) + fmt.Println(df) + + _ = X +} diff --git a/stat/polynomial_test.go b/stat/polynomial_test.go index e873d98..04094e7 100644 --- a/stat/polynomial_test.go +++ b/stat/polynomial_test.go @@ -12,7 +12,12 @@ func Test_PolyVal(t *testing.T) { fmt.Println(A) //A2 := []float64{3.131561350718812, -1.2400367769976413, 0.7355767301905694} - z2 := PolyVal(A, x) - fmt.Println(z2) + z1 := PolyVal(A, x) + fmt.Println(z1) + W := 5 + A2 := PolyFit(y, Sequence[float64](W), 1) + x2 := Repeat[float64](float64(W), W) + z2 := PolyVal(A2, x2) + fmt.Println(z2) } -- Gitee