From d76d513fc735c8a672fe75c3481c903c9d0e947d Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 9 Feb 2023 12:54:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E8=AE=A2README=E6=96=B0=E5=A2=9ES?= =?UTF-8?q?QRT=E6=B1=82=E5=B9=B3=E6=96=B9=E6=A0=B9=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/formula/README.md b/formula/README.md index 435a18f..fee1677 100644 --- a/formula/README.md +++ b/formula/README.md @@ -16,6 +16,7 @@ formula | 0 | HHVBARS | 求N周期内S最高值到当前周期数, 返回序列 | HHVBARS(HIGH,5) | [x] | [x] | | 0 | LLV | 计算N周期内最低 | LLV(HLOW,5) | [√] | [√] | | 0 | LLVBARS | 求N周期内S最低值到当前周期数, 返回序列 | LLVBARS(HLOW,5) | [x] | [x] | +| 0 | SQRT | 计算S的平方根 | SQRT(CLOSE) | [√] | [√] | | 0 | MAX | 计算AB最大值 | MAX(CLOSE,HIGH) | [√] | [√] | | 0 | MIN | 计算AB最小值 | MIN(CLOSE,HIGH) | [√] | [√] | | 0 | MA | 计算N周期的移动平均值, 简称均线 | MA(CLOSE,5) | [√] | [√] | -- Gitee From df2ffd9afcede67bc5dcc561e0182e6fd39b5cdc Mon Sep 17 00:00:00 2001 From: wangfeng Date: Thu, 9 Feb 2023 13:08:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?#I6CYPB=20=E5=AE=9E=E7=8E=B0SQRT=E6=B1=82?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=A0=B9=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formula/sqrt.go | 12 ++++++++++++ formula/sqrt_test.go | 16 ++++++++++++++++ stat/sqrt.go | 22 ++++++++++++++++++++++ stat/sqrt_test.go | 15 +++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 formula/sqrt.go create mode 100644 formula/sqrt_test.go create mode 100644 stat/sqrt.go create mode 100644 stat/sqrt_test.go diff --git a/formula/sqrt.go b/formula/sqrt.go new file mode 100644 index 0000000..5894555 --- /dev/null +++ b/formula/sqrt.go @@ -0,0 +1,12 @@ +package formula + +import ( + "gitee.com/quant1x/pandas" + "gitee.com/quant1x/pandas/stat" +) + +// SQRT 求S的平方根 +func SQRT(S pandas.Series) []stat.DType { + fs := S.DTypes() + return stat.Sqrt(fs) +} diff --git a/formula/sqrt_test.go b/formula/sqrt_test.go new file mode 100644 index 0000000..da950fe --- /dev/null +++ b/formula/sqrt_test.go @@ -0,0 +1,16 @@ +package formula + +import ( + "fmt" + "gitee.com/quant1x/pandas" + "testing" +) + +func TestSQRT(t *testing.T) { + f1 := []float32{1.1, 2.2, 1.3, 1.4} + f2 := []float64{70, 80, 75, 83, 86} + s1 := pandas.NewSeries(pandas.SERIES_TYPE_FLOAT32, "s1", f1) + s2 := pandas.NewSeries(pandas.SERIES_TYPE_FLOAT64, "s2", f2) + fmt.Println(SQRT(s1)) + fmt.Println(SQRT(s2)) +} diff --git a/stat/sqrt.go b/stat/sqrt.go new file mode 100644 index 0000000..7db7755 --- /dev/null +++ b/stat/sqrt.go @@ -0,0 +1,22 @@ +package stat + +import ( + "github.com/viterin/vek" + "github.com/viterin/vek/vek32" +) + +// Sqrt 求平方根 +func Sqrt[T StatType](v []T) []T { + var d any + var values any = v + switch fs := values.(type) { + case []float32: + d = vek32.Sqrt(fs) + case []float64: + d = vek.Sqrt(fs) + default: + panic(ErrUnsupportedType) + } + + return d.([]T) +} diff --git a/stat/sqrt_test.go b/stat/sqrt_test.go new file mode 100644 index 0000000..55884c1 --- /dev/null +++ b/stat/sqrt_test.go @@ -0,0 +1,15 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestSqrt(t *testing.T) { + f0 := []float32{1.1, 2.2, 1.3, 1.4} + f1 := []float64{70, 80, 75, 83, 86} + f2 := []float64{90, 69, 60, 88, 87} + fmt.Println(Sqrt(f0)) + fmt.Println(Sqrt(f1)) + fmt.Println(Sqrt(f2)) +} -- Gitee